从Python脚本使用POST发送文件

时间:2021-01-12 02:32:58

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字符串放入混合中时,我能够找到解决方案。此外,大多数解决方案不会对数据进行64位编码,以保持7位清洁。

3 个解决方案

#1


5  

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数据。但是你显然想要多部分编码的数据,好像你点击了一个“上传文件”按钮,对吧?嗯,这是我在这里和来自的答案非常直接的组合你联系的问题。)

#2


1  

PyCURL provides an interface to CURL from Python.

PyCURL为Python提供了一个CURL接口。

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

Content-Type:text / 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

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

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

#3


1  

Just use this library and send in files.

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

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

#1


5  

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数据。但是你显然想要多部分编码的数据,好像你点击了一个“上传文件”按钮,对吧?嗯,这是我在这里和来自的答案非常直接的组合你联系的问题。)

#2


1  

PyCURL provides an interface to CURL from Python.

PyCURL为Python提供了一个CURL接口。

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

Content-Type:text / 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

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

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

#3


1  

Just use this library and send in files.

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

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