我正在尝试编写一些 python 代码,这些代码可以在客户端创建多部分 mime http 请求,然后在服务器上进行适当的解释。我认为,我已经在客户端部分取得了成功:
from email.mime.multipart import MIMEMultipart, MIMEBase
import httplib
h1 = httplib.HTTPConnection('localhost:8080')
msg = MIMEMultipart()
fp = open('myfile.zip', 'rb')
base = MIMEBase("application", "octet-stream")
base.set_payload(fp.read())
msg.attach(base)
h1.request("POST", "http://localhost:8080/server", msg.as_string())
唯一的问题是电子邮件库还包含 Content-Type 和 MIME-Version header ,我不确定它们将如何与 httplib 包含的 HTTP header 相关:
Content-Type: multipart/mixed; boundary="===============2050792481=="
MIME-Version: 1.0
--===============2050792481==
Content-Type: application/octet-stream
MIME-Version: 1.0
这可能是当我的 web.py 应用程序收到此请求时,我只收到一条错误消息的原因。 web.py POST 处理程序:
class MultipartServer:
def POST(self, collection):
print web.input()
抛出这个错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/web.py-0.34-py2.6.egg/web/application.py", line 242, in process
return self.handle()
File "/usr/local/lib/python2.6/dist-packages/web.py-0.34-py2.6.egg/web/application.py", line 233, in handle
return self._delegate(fn, self.fvars, args)
File "/usr/local/lib/python2.6/dist-packages/web.py-0.34-py2.6.egg/web/application.py", line 415, in _delegate
return handle_class(cls)
File "/usr/local/lib/python2.6/dist-packages/web.py-0.34-py2.6.egg/web/application.py", line 390, in handle_class
return tocall(*args)
File "/home/richard/Development/server/webservice.py", line 31, in POST
print web.input()
File "/usr/local/lib/python2.6/dist-packages/web.py-0.34-py2.6.egg/web/webapi.py", line 279, in input
return storify(out, *requireds, **defaults)
File "/usr/local/lib/python2.6/dist-packages/web.py-0.34-py2.6.egg/web/utils.py", line 150, in storify
value = getvalue(value)
File "/usr/local/lib/python2.6/dist-packages/web.py-0.34-py2.6.egg/web/utils.py", line 139, in getvalue
return unicodify(x)
File "/usr/local/lib/python2.6/dist-packages/web.py-0.34-py2.6.egg/web/utils.py", line 130, in unicodify
if _unicode and isinstance(s, str): return safeunicode(s)
File "/usr/local/lib/python2.6/dist-packages/web.py-0.34-py2.6.egg/web/utils.py", line 326, in safeunicode
return obj.decode(encoding)
File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 137-138: invalid data
我的代码行由大约一半的错误行表示:
File "/home/richard/Development/server/webservice.py", line 31, in POST
print web.input()
它即将到来,但我不确定从这里到哪里去。这是我的客户端代码的问题,还是 web.py 的限制(也许它只是不能支持多部分请求)?任何关于替代代码库的提示或建议都将不胜感激。
编辑
上面的错误是由于数据没有自动进行base64编码造成的。添加
encoders.encode_base64(base)
去掉了这个错误,现在问题就清楚了。 HTTP 请求在服务器中没有被正确解释,大概是因为电子邮件库在正文中包含了应该是 HTTP header 的内容:
<Storage {'Content-Type: multipart/mixed': u'',
' boundary': u'"===============1342637378=="\n'
'MIME-Version: 1.0\n\n--===============1342637378==\n'
'Content-Type: application/octet-stream\n'
'MIME-Version: 1.0\n'
'Content-Transfer-Encoding: base64\n'
'\n0fINCs PBk1jAAAAAAAAA.... etc
所以有些地方不对。
谢谢
理查德
最佳答案
我使用了 Will Holcomb 的这个包 http://pypi.python.org/pypi/MultipartPostHandler/0.1.0使用 urllib2 发出多部分请求,它可能会帮助你。
关于python - 在 Python 中创建和解析多部分 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4434170/