BadStatusLine exception raised when returning reply from server in Python 3

情到浓时终转凉″ 提交于 2019-11-29 07:21:23

After lots of wiresharking, I figured out the cause, and solution, of the problem is the way the content-length header was being set. In my Python 3 port of the script, I copied over the method that set the content-length. Which is this:

headers['Content-length']=str(len(body))

That is incorrect! The correct way would be this:

headers['Content-length']=str(len(bytes(body, 'utf-8')))

Because the payload must be a bytes object. When you bytes encode it, the length is different than the string version.

return urllib.request.Request(theurl, bytes(body, 'utf-8'), headers)

You can safely omit manually setting the content-length header when using anything that derives from http.client.HTTPConnection. It has an internal method that checks for the content-length header, and if it's missing, set it based on the length of the content body, regardless of form.

The issue was a translation but subtle difference between Python 2 and 3 and how it handles strings and encodes them. It must've been some kind of fluke that the regular ASCII version worked when the utf-8 version didn't, oh well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!