Python 3 HTTP Server sends headers as output

 ̄綄美尐妖づ 提交于 2019-12-11 11:54:17

问题


In my GitHub project there is currently an Issue which is caused by Python 3.3.1. The builtin HTTP server of Python I'm using seems to send the headers as a normal output. When I tested it, it worked without problems but on the users computer the problem exists. I wasn't able to reproduce the problem, so I was wondering if there was a bug in Python 3.3.1 which causes the header problems or if I have a problem in the source code.

You can find the source code here. A screenshot of the problem can be found here.

Because the headers aren't send properly the HTML etc. becomes invalid and isn't displayed as HTML because the browser doesn't get the header for content type.


回答1:


I just found the problem. It seems that Python versions after >= 3.3.x require to send the status code before the first header. Otherwise the header is handled as a normal output. So I switched it and now it works. Below just an example:

Does not work:

self.send_header('Content-type', 'text/html')
self.send_response(200)

Works:

self.send_response(200)
self.send_header('Content-type', 'text/html')

By the way: The Internet Explorer doesn't have this problem. But of course the HTTP specifications require to send the status just before the headers. But earlier Python versions were able to deal with it. This is why I weren't able to reproduce the problem when I tried it the first times.



来源:https://stackoverflow.com/questions/23321887/python-3-http-server-sends-headers-as-output

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