HTTP错误504:尝试读取reddit注释pos时网关超时

2024-05-17 06:25:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试从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

Tags: comhttpurlhdrmy错误时间urllib2
1条回答
网友
1楼 · 发布于 2024-05-17 06:25:34

HTTP Error 504 Gateway timeout-服务器(不一定是Web服务器)充当网关或代理来满足客户端(例如,您的Web浏览器或我们的CheckUpDown robot)访问请求的URL的请求。此服务器没有从它访问以处理您的HTTP请求的上游服务器收到及时响应。

这通常意味着上游服务器关闭(对网关/代理没有响应),而不是上游服务器和网关/代理在数据交换协议上不一致。

问题可能出现在网络上的不同地方,没有“唯一”的解决方案。你得自己调查这个问题。

你的代码运行良好。可能的解决方案是:

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

此代码将尝试请求网页,直到它得到200个响应(成功的HTTP请求的标准响应)。当200个响应发生时,循环将中断,您可以执行下一个请求(或程序中的任何内容)

相关问题 更多 >