Python 3.4 - 多部分 Post 使用标准库
Python 3.4 - Multipart Post Using Standard Library
有没有人有关于如何在 Python 3.4 中执行多部分 post 而不使用像请求这样的第三方库的示例?
我在将我的旧 Python 2 代码移植到 Python 3.4 时遇到问题。
这里是python2编码代码:
def _encode_multipart_formdata(self, fields, files):
boundary = mimetools.choose_boundary()
buf = StringIO()
for (key, value) in fields.iteritems():
buf.write('--%s\r\n' % boundary)
buf.write('Content-Disposition: form-data; name="%s"' % key)
buf.write('\r\n\r\n' + self._tostr(value) + '\r\n')
for (key, filepath, filename) in files:
if os.path.isfile(filepath):
buf.write('--%s\r\n' % boundary)
buf.write('Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename))
buf.write('Content-Type: %s\r\n' % (self._get_content_type3(filename)))
file = open(filepath, "rb")
try:
buf.write('\r\n' + file.read() + '\r\n')
finally:
file.close()
buf.write('--' + boundary + '--\r\n\r\n')
buf = buf.getvalue()
content_type = 'multipart/form-data; boundary=%s' % boundary
return content_type, buf
我发现我可以将 mimetools.choose_boundary() 替换为以下内容:
import email.generator
print (email.generator._make_boundary())
对于 _get_content_type3() 方法,我正在执行以下操作:
def _get_content_type(self, filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
当我使用 Python3.4 将 StringIO 更改为 BytesIO 时,数据似乎从未被放入 POST 方法中。
有什么建议吗?
是的,email.generator._make_boundary()
可以:
import email.generator
import io
import shutil
def _encode_multipart_formdata(self, fields, files):
boundary = email.generator._make_boundary()
buf = io.BytesIO()
textwriter = io.TextIOWrapper(
buf, 'utf8', newline='', write_through=True)
for (key, value) in fields.items():
textwriter.write(
'--{boundary}\r\n'
'Content-Disposition: form-data; name="{key}"\r\n\r\n'
'{value}\r\n'.format(
boundary=boundary, key=key, value=value))
for (key, filepath, filename) in files:
if os.path.isfile(filepath):
textwriter.write(
'--{boundary}\r\n'
'Content-Disposition: form-data; name="{key}"; '
'filename="{filename}"\r\n'
'Content-Type: {content_type}\r\n\r\n'.format(
boundary=boundary, key=key, filename=filename,
content_type=self._get_content_type3(filename)))
with open(filepath, "rb") as f:
shutil.copyfileobj(f, buf)
textwriter.write('\r\n')
textwriter.write('--{}--\r\n\r\n'.format(boundary))
content_type = 'multipart/form-data; boundary={}'.format(boundary)
return content_type, buf.getvalue()
这使用 io.TextIOWrapper()
object 使 header 格式化和编码更容易(bytes
objects 不支持格式化操作;您必须等待 Python 3.5 添加了 %
支持)。
如果您坚持在整个作业中使用 email
包,请考虑到您需要两倍的内存;一次保持 email.mime
objects,再次保持书面结果:
from email.mime import multipart, nonmultipart, text
from email.generator import BytesGenerator
from email import policy
from io import BytesIO
def _encode_multipart_formdata(self, fields, files):
msg = multipart.MIMEMultipart('form-data')
for (key, value) in fields.items():
part = text.MIMEText(value)
part['Content-Disposition'] = 'form-data; name="{}"'.format(key)
msg.attach(part)
for (key, filepath, filename) in files:
if os.path.isfile(filepath):
ct = self._get_content_type3(filename)
part = nonmultipart.MIMENonMultipart(*ct.split('/'))
part['Content-Disposition'] = (
'form-data; name="{}"; filename="{}"'.format(
key, filename))
with open(filepath, "rb") as f:
part.set_payload(f.read())
msg.attach(part)
body = BytesIO()
generator = BytesGenerator(
body, mangle_from_=False, policy=policy.HTTP)
generator.flatten(msg)
return msg['content-type'], body.getvalue().partition(b'\r\n\r\n')[-1]
结果基本相同,只是增加了一些MIME-Version
和Content-Transfer-Encoding
headers.
有没有人有关于如何在 Python 3.4 中执行多部分 post 而不使用像请求这样的第三方库的示例?
我在将我的旧 Python 2 代码移植到 Python 3.4 时遇到问题。
这里是python2编码代码:
def _encode_multipart_formdata(self, fields, files):
boundary = mimetools.choose_boundary()
buf = StringIO()
for (key, value) in fields.iteritems():
buf.write('--%s\r\n' % boundary)
buf.write('Content-Disposition: form-data; name="%s"' % key)
buf.write('\r\n\r\n' + self._tostr(value) + '\r\n')
for (key, filepath, filename) in files:
if os.path.isfile(filepath):
buf.write('--%s\r\n' % boundary)
buf.write('Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename))
buf.write('Content-Type: %s\r\n' % (self._get_content_type3(filename)))
file = open(filepath, "rb")
try:
buf.write('\r\n' + file.read() + '\r\n')
finally:
file.close()
buf.write('--' + boundary + '--\r\n\r\n')
buf = buf.getvalue()
content_type = 'multipart/form-data; boundary=%s' % boundary
return content_type, buf
我发现我可以将 mimetools.choose_boundary() 替换为以下内容:
import email.generator
print (email.generator._make_boundary())
对于 _get_content_type3() 方法,我正在执行以下操作:
def _get_content_type(self, filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
当我使用 Python3.4 将 StringIO 更改为 BytesIO 时,数据似乎从未被放入 POST 方法中。
有什么建议吗?
是的,email.generator._make_boundary()
可以:
import email.generator
import io
import shutil
def _encode_multipart_formdata(self, fields, files):
boundary = email.generator._make_boundary()
buf = io.BytesIO()
textwriter = io.TextIOWrapper(
buf, 'utf8', newline='', write_through=True)
for (key, value) in fields.items():
textwriter.write(
'--{boundary}\r\n'
'Content-Disposition: form-data; name="{key}"\r\n\r\n'
'{value}\r\n'.format(
boundary=boundary, key=key, value=value))
for (key, filepath, filename) in files:
if os.path.isfile(filepath):
textwriter.write(
'--{boundary}\r\n'
'Content-Disposition: form-data; name="{key}"; '
'filename="{filename}"\r\n'
'Content-Type: {content_type}\r\n\r\n'.format(
boundary=boundary, key=key, filename=filename,
content_type=self._get_content_type3(filename)))
with open(filepath, "rb") as f:
shutil.copyfileobj(f, buf)
textwriter.write('\r\n')
textwriter.write('--{}--\r\n\r\n'.format(boundary))
content_type = 'multipart/form-data; boundary={}'.format(boundary)
return content_type, buf.getvalue()
这使用 io.TextIOWrapper()
object 使 header 格式化和编码更容易(bytes
objects 不支持格式化操作;您必须等待 Python 3.5 添加了 %
支持)。
如果您坚持在整个作业中使用 email
包,请考虑到您需要两倍的内存;一次保持 email.mime
objects,再次保持书面结果:
from email.mime import multipart, nonmultipart, text
from email.generator import BytesGenerator
from email import policy
from io import BytesIO
def _encode_multipart_formdata(self, fields, files):
msg = multipart.MIMEMultipart('form-data')
for (key, value) in fields.items():
part = text.MIMEText(value)
part['Content-Disposition'] = 'form-data; name="{}"'.format(key)
msg.attach(part)
for (key, filepath, filename) in files:
if os.path.isfile(filepath):
ct = self._get_content_type3(filename)
part = nonmultipart.MIMENonMultipart(*ct.split('/'))
part['Content-Disposition'] = (
'form-data; name="{}"; filename="{}"'.format(
key, filename))
with open(filepath, "rb") as f:
part.set_payload(f.read())
msg.attach(part)
body = BytesIO()
generator = BytesGenerator(
body, mangle_from_=False, policy=policy.HTTP)
generator.flatten(msg)
return msg['content-type'], body.getvalue().partition(b'\r\n\r\n')[-1]
结果基本相同,只是增加了一些MIME-Version
和Content-Transfer-Encoding
headers.