Python 3 HTTP Server将标头作为输出发送 [英] Python 3 HTTP Server sends headers as output

查看:133
本文介绍了Python 3 HTTP Server将标头作为输出发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的GitHub项目中,目前存在一个由Python 3.3.1引起的问题。我正在使用的Python内置HTTP服务器似乎将标头作为普通输出发送。当我测试它时,它没有问题,但在用户计算机上存在问题。我无法重现这个问题,所以我想知道Python 3.3.1中是否存在导致标题问题或者源代码中存在问题的错误。

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.

由于标题未正确发送,因此HTML等无效并且不会显示为HTML,因为浏览器无法获取内容标题类型。

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.

推荐答案

我刚发现问题。似乎> = 3.3.x之后的Python版本需要在第一个标头之前发送状态代码。否则,标头将作为正常输出处理。所以我切换它现在它的工作原理。下面只是一个例子:

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:

不起作用:

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

作品:

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

顺便说一句:Internet Explorer没有这个问题。但当然HTTP规范要求在标头之前发送状态。但早期的Python版本能够处理它。这就是我第一次尝试时无法重现问题的原因。

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.

这篇关于Python 3 HTTP Server将标头作为输出发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆