Python HTTP 错误 504:尝试阅读 reddit 评论帖子时网关超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15786421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP Error 504: Gateway Time-out when trying to read a reddit comments post
提问by Giannis H.
I am encountering an error when trying to get a comments' http from reddit. This has happened to various URLs (not all of them with special characters) and this is one of them. In one hour time frame, there may be 1000 or more requests to the reddit.com domain.
尝试从 reddit 获取评论的 http 时遇到错误。这发生在各种 URL 上(并非所有 URL 都带有特殊字符),这就是其中之一。在一小时的时间范围内,可能会有 1000 个或更多的请求发送到 reddit.com 域。
hdr = {"User-Agent": "My Agent"}
try:
req = urllib2.Request("http://www.reddit.com/r/gaming/"
"comments/1bjuee/when_pokΓ?mon_was_good", headers=hdr)
htmlSource = urllib2.urlopen(req).read()
except Exception as inst:
print inst
Output>>HTTP Error 504: Gateway Time-out
采纳答案by 4d4c
HTTP Error 504 Gateway timeout- A server (not necessarily a Web server) is acting as a gateway or proxy to fulfil the request by the client (e.g. your Web browser or our CheckUpDown robot) to access the requested URL. This server did not receive a timely response from an upstream server it accessed to deal with your HTTP request.
HTTP 错误 504 网关超时- 服务器(不一定是 Web 服务器)充当网关或代理来满足客户端(例如您的 Web 浏览器或我们的 CheckUpDown 机器人)访问请求的 URL 的请求。该服务器没有收到它访问的上游服务器的及时响应以处理您的 HTTP 请求。
This usually means that the upstream server is down (no response to the gateway/proxy), rather than that the upstream server and the gateway/proxy do not agree on the protocol for exchanging data.
这通常意味着上游服务器宕机(对网关/代理没有响应),而不是上游服务器和网关/代理不同意交换数据的协议。
Problem can appear in different places on the network and there is no "unique" solution for it. You will have to investigative the problem by your own.
问题可能出现在网络的不同位置,并且没有“唯一”的解决方案。您将不得不自己调查问题。
Your code works fine. Possible solution for you problem would be:
你的代码工作正常。您的问题可能的解决方案是:
import urllib2
hdr = {"User-Agent": "My Agent"}
while True:
try:
req = urllib2.Request("http://www.reddit.com/", headers=hdr)
response = urllib2.urlopen(req)
htmlSource = response.read()
if response.getcode() == 200:
break
except Exception as inst:
print inst
This code will try to request webpage until it gets 200 response (standard response for successful HTTP requests). When 200 response will occur while loop will break and you can do next request (or whatever you have in your program)
此代码将尝试请求网页,直到获得 200 响应(成功 HTTP 请求的标准响应)。当循环中断时将发生 200 响应,您可以执行下一个请求(或您程序中的任何请求)

