Python - HTTP multipart/form-data POST请求

Wil*_*iam 5 python python-3.x

我想将文件上传到Web服务器.根据我的阅读,最好的方法是在HTTP POST请求中使用multipart/form-data编码类型.

我的研究似乎表明,使用Python标准库没有简单的方法可以做到这一点.我正在使用Python 3.

(注意:查看一个名为requests的请求(PyPI Link)来轻松完成此操作)

我目前正在使用这种方法:

import mimetypes, http.client
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T' # Randomly generated
for fileName in fileList:
    # Add boundary and header
    dataList.append('--' + boundary)
    dataList.append('Content-Disposition: form-data; name={0}; filename={0}'.format(fileName))

    fileType = mimetypes.guess_type(fileName)[0] or 'application/octet-stream'
    dataList.append('Content-Type: {}'.format(fileType))
    dataList.append('')

    with open(fileName) as f: 
        # Bad for large files
        dataList.append(f.read())

dataList.append('--'+boundary+'--')
dataList.append('')
contentType = 'multipart/form-data; boundary={}'.format(boundary)

body = '\r\n'.join(dataList)
headers = {'Content-type': contentType}

conn = http.client.HTTPConnection('http://...')
req = conn.request('POST', '/test/', body, headers)

print(conn.getresponse().read())
Run Code Online (Sandbox Code Playgroud)

这适用于发送文本.

有两个问题:这只是文本,整个文本文件必须作为一个巨大的字符串存储在内存中.

如何上传任何二进制文件?有没有办法在不将整个文件读入内存的情况下执行此操作?

Use*_*ser 2

我看了一下这个模块

class HTTPConnection:
    # ...
    def send(self, data): # line 820
        """Send `data' to the server.
        ``data`` can be a string object, a bytes object, an array object, a
        file-like object that supports a .read() method, or an iterable object.
        """
Run Code Online (Sandbox Code Playgroud)

数据就是正文。您可以传递这样的迭代器:(我没有尝试过)

def body():
  for fileName in fileList:
    # Add boundary and header
    yield('--' + boundary) + '\r\n'
    yield('Content-Disposition: form-data; name={0}; filename=    {0}'.format(fileName)) + '\r\n'

    fileType = mimetypes.guess_type(fileName)[0] or 'application/octet-stream'
    yield('Content-Type: {}'.format(fileType)) + '\r\n'
    yield('\r\n')

    with open(fileName) as f: 
        # Bad for large files
        yield f.read()
    yield('--'+boundary+'--') + '\r\n'
    yield('') + '\r\n'
Run Code Online (Sandbox Code Playgroud)