本文介绍了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请求发布文件
发布评论