如何使用python请求上传文件? [英] How to upload file with python requests?

查看:91
本文介绍了如何使用python请求上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个使用 Python 请求库上传文件的简单任务.我查了 Stack Overflow 好像没有人遇到同样的问题,就是服务器收不到文件:

导入请求url='http://nesssi.cacr.caltech.edu/cgi-bin/getmulticonedb_release2.cgi/post'files={'files': open('file.txt','rb')}values={'upload_file' : 'file.txt' , 'DB':'photcat' , 'OUT':'csv' , 'SHORT':'short'}r=requests.post(url,files=files,data=values)

我正在用我的文件名填充 'upload_file' 关键字的值,因为如果我将其留空,它会说

错误 - 您必须选择要上传的文件!

现在我明白了

大小字节的文件file.txt上传成功!查询服务结果:有0行.

只有在文件为空时才会出现.所以我不知道如何成功发送我的文件.我知道该文件有效,因为如果我访问该网站并手动填写表格,它会返回一个很好的匹配对象列表,这正是我所追求的.我真的很感激所有的提示.

其他一些相关的线程(但没有回答我的问题):

解决方案

如果 upload_file 是文件,请使用:

files = {'upload_file': open('file.txt','rb')}values = {'DB':'photcat','OUT':'csv','SHORT':'short'}r = requests.post(网址,文件=文件,数据=值)

requests 将发送一个多部分表单 POST 正文,其中 upload_file 字段设置为 file.txt 文件的内容.

文件名将包含在特定字段的 MIME 标头中:

<预><代码>>>>进口请求>>>open('file.txt', 'wb') # 创建一个空的演示文件<_io.BufferedWriter name='file.txt'>>>>files = {'upload_file': open('file.txt', 'rb')}>>>打印(requests.Request('POST', 'http://example.com', files=files).prepare().body.decode('ascii'))--c226ce13d09842658ffbd31e0563c6bd内容配置:表单数据;名称=上传_文件";文件名=文件.txt"--c226ce13d09842658ffbd31e0563c6bd--

注意 filename="file.txt" 参数.

如果您需要更多控制,您可以使用元组作为 files 映射值,包含 2 到 4 个元素.第一个元素是文件名,然后是内容,以及可选的内容类型标头值和附加标头的可选映射:

files = {'upload_file': ('foobar.txt', open('file.txt','rb'), 'text/x-spam')}

这将设置替代文件名和内容类型,省略可选标题.

如果您的意思是从文件中获取整个 POST 正文(没有指定其他字段),则不要使用 files 参数,只需发布文件直接作为data.然后,您可能还想设置 Content-Type 标头,否则将不会设置任何标头.请参阅 Python 请求 - 从文件中 POST 数据.

I'm performing a simple task of uploading a file using Python requests library. I searched Stack Overflow and no one seemed to have the same problem, namely, that the file is not received by the server:

import requests
url='http://nesssi.cacr.caltech.edu/cgi-bin/getmulticonedb_release2.cgi/post'
files={'files': open('file.txt','rb')}
values={'upload_file' : 'file.txt' , 'DB':'photcat' , 'OUT':'csv' , 'SHORT':'short'}
r=requests.post(url,files=files,data=values)

I'm filling the value of 'upload_file' keyword with my filename, because if I leave it blank, it says

Error - You must select a file to upload!

And now I get

File  file.txt  of size    bytes is  uploaded successfully!
Query service results:  There were 0 lines.

Which comes up only if the file is empty. So I'm stuck as to how to send my file successfully. I know that the file works because if I go to this website and manually fill in the form it returns a nice list of matched objects, which is what I'm after. I'd really appreciate all hints.

Some other threads related (but not answering my problem):

解决方案

If upload_file is meant to be the file, use:

files = {'upload_file': open('file.txt','rb')}
values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'}

r = requests.post(url, files=files, data=values)

and requests will send a multi-part form POST body with the upload_file field set to the contents of the file.txt file.

The filename will be included in the mime header for the specific field:

>>> import requests
>>> open('file.txt', 'wb')  # create an empty demo file
<_io.BufferedWriter name='file.txt'>
>>> files = {'upload_file': open('file.txt', 'rb')}
>>> print(requests.Request('POST', 'http://example.com', files=files).prepare().body.decode('ascii'))
--c226ce13d09842658ffbd31e0563c6bd
Content-Disposition: form-data; name="upload_file"; filename="file.txt"


--c226ce13d09842658ffbd31e0563c6bd--

Note the filename="file.txt" parameter.

You can use a tuple for the files mapping value, with between 2 and 4 elements, if you need more control. The first element is the filename, followed by the contents, and an optional content-type header value and an optional mapping of additional headers:

files = {'upload_file': ('foobar.txt', open('file.txt','rb'), 'text/x-spam')}

This sets an alternative filename and content type, leaving out the optional headers.

If you are meaning the whole POST body to be taken from a file (with no other fields specified), then don't use the files parameter, just post the file directly as data. You then may want to set a Content-Type header too, as none will be set otherwise. See Python requests - POST data from a file.

这篇关于如何使用python请求上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆