Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的python套接字会出现404错误?_Python_Sockets_Python 2.7_Networking_Web - Fatal编程技术网

为什么我的python套接字会出现404错误?

为什么我的python套接字会出现404错误?,python,sockets,python-2.7,networking,web,Python,Sockets,Python 2.7,Networking,Web,服务器响应 得到 HTTP/1.0 HTTP/1.0 400错误请求服务器:AkamaiGHost Mime版本: 1.0内容类型:文本/html内容长度:200过期时间:2015年2月7日星期六17:49:11 GMT日期:2015年2月7日星期六17:49:11 GMT连接: 接近 无效URL无效 URL请求的URL“/优惠券”无效。 参考#9.bf254b8.1423331351.64ef899d 其他网站也是如此:我只能从索引页面得到回复 我的插座怎么了 我的插座怎么了? 没什么。您使

服务器响应

得到 HTTP/1.0 HTTP/1.0 400错误请求服务器:AkamaiGHost Mime版本: 1.0内容类型:文本/html内容长度:200过期时间:2015年2月7日星期六17:49:11 GMT日期:2015年2月7日星期六17:49:11 GMT连接: 接近

无效URL无效 URL请求的URL“/优惠券”无效。 参考#9.bf254b8.1423331351.64ef899d

其他网站也是如此:我只能从索引页面得到回复

我的插座怎么了

我的插座怎么了?

没什么。您使用太低级的工具处理问题

测试用例:

socket = socket(AF_INET, SOCK_STREAM)
socket.connect(("www.groupon.com", 80))
file = socket.makefile('r', 0)
file.write("GET " + "/coupons"  + " HTTP/1.0\n\nHost: " + "www.groupon.com" + "\n\nConnection: close\n\n")
buff = file.readlines()
答复:

import socket

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect(("www.groupon.com", 80))
file = socket.makefile('r', 0)
file.write("GET " + "/coupons"  + "HTTP/1.0\r\nHost: " + "www.groupon.com" + "\r\nConnection: close\r\n")
buf = file.readlines()
print buf
['HTTP/1.0 408请求超时\r\n','Server:AkamaiGHost\r\n','Mime版本:1.0\r\n','Date:Sat,07 Feb 2015 19:17:53 GMT\r\n','Content Type:text/html\r\n','Content Length:218\r\n','Expires:Sat,07 Feb 2015 19:17:53 GMT\r\n','r\n','r\n',',“服务器在等待浏览器请求时超时。

\n”,'Reference ;#;2.;63d77a5c.;1423336673.;0\n','\n']


访问
http://www.groupon.com/coupons
使用普通浏览器时,确实会将您重定向到
httpS://…
。错误消息“服务器在等待浏览器请求时超时”表示web应用程序等待未发生的某个客户端触发事件(可能是通过JavaScript触发的某个请求)。在任何情况下,您的低级方法都不像浏览器那样。浏览器做复杂的事情,HTTP协议的实现只是其中的一部分。

我使用的是\n\n而不是\r\n。因为如果我这样做,每一页都会出现404错误。为什么不添加该注释作为对您自己问题的回答呢。您可以这样做吗我甚至不接受你自己的答案。我的意思是,无论哪种方式,我都会出错。如果你只是执行
…“get watch?v=MsFohjWLKdI\r\n”
,会发生什么情况?我仍然会遇到同样的错误。
['HTTP/1.0 408 Request Time-out\r\n', 'Server: AkamaiGHost\r\n', 'Mime-Version: 1.0\r\n', 'Date: Sat, 07 Feb 2015 19:17:53 GMT\r\n', 'Content-Type: text/html\r\n', 'Content-Length: 218\r\n', 'Expires: Sat, 07 Feb 2015 19:17:53 GMT\r\n', '\r\n', '<HTML><HEAD>\n', '<TITLE>Request Timeout</TITLE>\n', '</HEAD><BODY>\n', '<H1>Request Timeout</H1>\n', "The server timed out while waiting for the browser's request.<P>\n", 'Reference&#32;&#35;2&#46;63d77a5c&#46;1423336673&#46;0\n', '</BODY></HTML>\n']