python套接字错误请求400

python套接字错误请求400,python,http,Python,Http,我使用python(2.7.6)socket运行我的程序,该程序用于请求网站上的文本文件 然后,我得到了输出: HTTP/1.1 400 Bad Request\r\n Date: Mon, 13 Oct 2014 02:46:15 GMT\r\n Server: Apache/2.2.3 (CentOS)\r\n Content-Length: 300\r\nConnection: close\r\n Content-Type: text/html; charset=iso-8859-1\r\

我使用python(2.7.6)socket运行我的程序,该程序用于请求网站上的文本文件

然后,我得到了输出:

HTTP/1.1 400 Bad Request\r\n
Date: Mon, 13 Oct 2014 02:46:15 GMT\r\n
Server: Apache/2.2.3 (CentOS)\r\n
Content-Length: 300\r\nConnection: close\r\n
Content-Type: text/html; charset=iso-8859-1\r\n
\r\n
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0 //EN">\n
<html><head>\n
<title>400 Bad Request</title>\n
</head><body>\n
<h1>Bad Request</h1>\n
<p>Your browser sent a request that this server could not understand.<br/>\n
</p>\n<hr>\n
<address>Apache/2.2.3 (CentOS) Server at 127.0.0.1 Port 80</address>\n
</body></html>\n
HTTP/1.1 400错误请求\r\n
日期:2014年10月13日星期一02:46:15 GMT\r\n
服务器:Apache/2.2.3(CentOS)\r\n
内容长度:300\r\n连接:关闭\r\n
内容类型:text/html;字符集=iso-8859-1\r\n
\r\n
\n
\n
400错误的请求\n
\n
错误的请求\n
您的浏览器发送了此服务器无法理解的请求。
\n

\n
\n 位于127.0.0.1端口80的Apache/2.2.3(CentOS)服务器\n \n
问题: 我的代码有什么问题?
如何修复一个好的请求?

添加
Host
标题:

m = 'GET / HTTP/1.1\r\nHost: www.cnn.com\r\n\r\n'

将www.cnn.com替换为您的ip地址

HTTP 1.1要求您传输带有所有请求的
主机
头。发件人:

客户端必须在所有HTTP/1.1请求中包含主机头字段 信息。[…]所有 基于Internet的HTTP/1.1服务器必须响应400(错误请求) 任何缺少主机头的HTTP/1.1请求消息的状态代码 场

使用
Host
头的原因是,如果在同一IP地址上服务多个网站,则允许服务器消除访问哪个网站的歧义


或者,您可以使用HTTP 1.0而不是HTTP 1.1。HTTP 1.0不需要主机头,因此,如果您连接的服务器上只有一个网站,它可能会工作,但如果它承载多个网站,您可能仍然会收到400个错误。

我尝试使用HTTP 1.0,它工作了:

m = 'GET / HTTP/1.0\r\nHost: www.cnn.com\r\n\r\n'

非常感谢。你的回答很有用。
m = 'GET / HTTP/1.0\r\nHost: www.cnn.com\r\n\r\n'