Python请求发布文件

编程入门 行业动态 更新时间:2024-10-07 16:22:32
本文介绍了Python请求发布文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用 CURL 我可以发布类似的文件

Using CURL I can post a file like

CURL -X POST -d "pxeconfig=`cat boot.txt`" ip:8443/tftp/syslinux

我的文件看起来像

$ cat boot.txt line 1 line 2 line 3

我正在尝试使用 python 中的 requests 模块来实现相同的目的

I am trying to achieve the same thing using requests module in python

r=requests.post(url, files={'pxeconfig': open('boot.txt','rb')})

当我在服务器端打开文件时,文件包含

When I open the file on server side, the file contains

{:filename=>"boot.txt", :type=>nil, :name=>"pxeconfig", :tempfile=>#<Tempfile:/tmp/RackMultipart20170405-19742-1cylrpm.txt>, :head=>"Content-Disposition: form-data; name=\"pxeconfig\"; filename=\"boot.txt\"\r\n"}

请建议我如何实现这一目标.

Please suggest how I can achieve this.

推荐答案

您的 curl 请求将文件内容作为表单数据发送,而不是实际文件!你可能想要像

Your curl request sends the file contents as form data, as opposed to an actual file! You probably want something like

with open('boot.txt', 'rb') as f: r = requests.post(url, data={'pxeconfig': f.read()})

更多推荐

Python请求发布文件

本文发布于:2023-10-10 18:09:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!