【问题标题】:Why does my python socket get 404 error?为什么我的 python 套接字会出现 404 错误?
【发布时间】:2015-02-07 16:47:46
【问题描述】:
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()

服务器响应

获取https://www.groupon.com/coupons 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“/coupons”无效。

参考#9.bf254b8.1423331351.64ef899d

这也发生在其他网站上:我只能从索引页面获得响应。

我的插座有什么问题?

【问题讨论】:

  • 我使用的是 \n\n 而不是 \r\n。因为如果我这样做,每个页面都会出现 404 错误。
  • 为什么不将该评论添加为您自己问题的答案。你甚至可以接受你自己的答案。
  • 我的意思是无论哪种方式我都会出错。
  • 如果你只做... "GET watch?v=MsFohjWLKdI\r\n"会发生什么?
  • 我仍然遇到同样的错误。

标签: python sockets python-2.7 networking web


【解决方案1】:

我的插座有什么问题?

什么都没有。您使用太低级的工具来解决问题。

测试用例:

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 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']

使用普通浏览器访问http://www.groupon.com/coupons 会将您重定向到httpS://...。错误消息“服务器在等待浏览器请求时超时”表示 Web 应用程序等待了一些未发生的客户端触发事件(可能是通过 JavaScript 触发的一些请求)。无论如何,您的低级方法不仅类似于浏览器的行为。浏览器做复杂的事情,HTTP协议的实现只是其中的一部分。

【讨论】:

    猜你喜欢
    • 2021-06-01
    • 2016-01-25
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    相关资源
    最近更新 更多