从 Python 脚本使用 POST 发送文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/150517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:33:53  来源:igfitidea点击:

Send file using POST from a Python script

pythonencodingpost

提问by zellyn

This is an almost-duplicate of Send file using POST from a Python script, but I'd like to add a caveat: I need something that properly handles the encoding of fields and attached files. The solutions I've been able to find blow up when you throw unicode strings containing non-ascii characters into the mix. Also, most of the solutions don't base64-encode data to keep things 7-bit clean.

这是使用 Python 脚本中的 POST 发送文件的几乎重复,但我想添加一个警告:我需要一些可以正确处理字段和附加文件编码的东西。当您将包含非 ascii 字符的 unicode 字符串放入混合中时,我已经能够找到解决方案。此外,大多数解决方案不会对数据进行 base64 编码以保持 7 位整洁。

回答by Dan Lenski

Best thing I can think of is to encode it yourself. How about this subroutine?

我能想到的最好的事情就是自己编码。这个子程序怎么样?

from urllib2 import Request, urlopen
from binascii import b2a_base64

def b64open(url, postdata):
  req = Request(url, b2a_base64(postdata), headers={'Content-Transfer-Encoding': 'base64'})
  return urlopen(req)

conn = b64open("http://www.whatever.com/script.cgi", u"Liberté égalité Fraternité")
# returns a file-like object

(Okay, so this code just sends POST-data. But you apparently want multipart-encoded data, as if you clicked an "Upload File" button, right? Well, it's a pretty straightforward combination of what I have here and the answers from the question you linked.)

(好吧,所以这段代码只是发送 POST 数据。但您显然想要多部分编码的数据,就像您单击了“上传文件”按钮一样,对吗?嗯,这是我在这里的内容和来自的答案的非常简单的组合您链接的问题。)

回答by Chris

PyCURL provides an interface to CURL from Python.

PyCURL 提供了一个从 Python 到 CURL 的接口。

http://curl.haxx.se/libcurl/python/

http://curl.haxx.se/libcurl/python/

Curl will do all you need. It can transfer binary files properly, and supports many encodings. However, you have to make sure that the proper character encoding as a custom header when POSTing files.

Curl 将满足您的所有需求。它可以正确传输二进制文件,并支持多种编码。但是,您必须确保在 POST 文件时将正确的字符编码作为自定义标头。

Specifically, you may need to do a 'file upload' style POST:

具体来说,您可能需要执行“文件上传”样式的 POST:

http://curl.haxx.se/docs/httpscripting.html(Section 4.3)

http://curl.haxx.se/docs/httpscripting.html(第 4.3 节)

With curl (or any other HTTP client) you may have to set the content encoding:

使用 curl(或任何其他 HTTP 客户端),您可能需要设置内容编码:

Content-Type: text/html; charset=UTF-8

内容类型:文本/html;字符集=UTF-8

Also, be aware that the request headers must be ascii, and this includes the url (so make sure you properly escape your possibly unicode URLs. There are unicode escapes for the HTTP headers) This was recently fixed in Python:

另外,请注意请求标头必须是 ascii,这包括 url(因此请确保正确转义可能的 unicode URL。HTTP 标头有 unicode 转义) 这最近在 Python 中修复:

http://bugs.python.org/issue3300

http://bugs.python.org/issue3300

I hope this helps, there is more info on the topic, including setting your default character set on your server, etc.

我希望这会有所帮助,有关该主题的更多信息,包括在服务器上设置默认字符集等。

回答by Stattrav

Just use this library and send in files.

只需使用此库并发送文件即可。

http://github.com/seisen/urllib2_file/

http://github.com/seisen/urllib2_file/