HTTP错误504:尝试读取Reddit评论帖子时网关超时 [英] HTTP Error 504: Gateway Time-out when trying to read a reddit comments post

查看:125
本文介绍了HTTP错误504:尝试读取Reddit评论帖子时网关超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从reddit获取评论的http时遇到错误.各种URL都发生了这种情况(并非所有URL都带有特殊字符),这就是其中之一.在一个小时的时间范围内,可能会对reddit.com域发出1000个或更多请求.

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.

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

推荐答案

HTTP错误504网关超时-服务器(不一定是Web服务器)充当网关或代理来满足客户端(例如,Web浏览器或我们的CheckUpDown机械手)访问请求的URL的请求.该服务器没有收到来自其处理您的HTTP请求的上游服务器的及时响应.

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.

这通常意味着上游服务器已关闭(对网关/代理无响应),而不是上游服务器和网关/代理未就交换数据的协议达成共识.

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

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

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)

这篇关于HTTP错误504:尝试读取Reddit评论帖子时网关超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆