Python urllib2 代码 returns "HTTP error 503" 在一台机器上但不是另一台机器
Python urllib2 code returns "HTTP error 503" on one machine but not another
我可以在 ubuntu 桌面上用 Python 2.7 的 urllib2 库打开一个网页,例如 nike 的 page。但是,当我将该代码移动到 google 计算引擎服务器(具有相同的 O.S)时,它开始返回 HTTP Error 503: Service Unavailable
.
从一个地方而不是另一个地方导致此错误的原因是什么?如果可能的话,我将如何使我的机器运行一致?
那个服务器 returns urllib2.HTTPError: HTTP Error 403: Forbidden
除非你传递一个 'Accept' header。当我尝试时仅使用 'User-Agent' header 失败。这是工作代码; 'User-Agent'和'Connection'header我都注释掉了,留着参考:
import urllib2
user_agent = {'User-Agent': 'Mozilla/5.0'}
req_headers = {
# 'User-Agent': user_agent,
# 'Connection': 'Keep-Alive',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
}
request = urllib2.Request('http://www.nike.com/us/en_us/c/men', headers=req_headers)
response = urllib2.urlopen(request)
data = response.read()
print data
另请参阅其他 Whosebug answer,我将其用作 'Accept' 字符串的参考。
HTTP Status 503 表示,我引用 RFC 2612:"The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header."
所以,这根本不是关于请求来自哪里:这完全是关于服务器暂时超载,或者正在维护。检查响应中的 Retry-After header 并应用它;或者,如果缺少,"retry later" 更笼统。
如果持续存在(它不应该是:503 意味着服务器正在遭受 临时 条件),请联系网站系统管理员并获得对发生的情况的解释。重复一遍,这完全是关于您正在联系的网络服务器的,应该是临时情况;完全不关心你的客户。
我可以在 ubuntu 桌面上用 Python 2.7 的 urllib2 库打开一个网页,例如 nike 的 page。但是,当我将该代码移动到 google 计算引擎服务器(具有相同的 O.S)时,它开始返回 HTTP Error 503: Service Unavailable
.
从一个地方而不是另一个地方导致此错误的原因是什么?如果可能的话,我将如何使我的机器运行一致?
那个服务器 returns urllib2.HTTPError: HTTP Error 403: Forbidden
除非你传递一个 'Accept' header。当我尝试时仅使用 'User-Agent' header 失败。这是工作代码; 'User-Agent'和'Connection'header我都注释掉了,留着参考:
import urllib2
user_agent = {'User-Agent': 'Mozilla/5.0'}
req_headers = {
# 'User-Agent': user_agent,
# 'Connection': 'Keep-Alive',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
}
request = urllib2.Request('http://www.nike.com/us/en_us/c/men', headers=req_headers)
response = urllib2.urlopen(request)
data = response.read()
print data
另请参阅其他 Whosebug answer,我将其用作 'Accept' 字符串的参考。
HTTP Status 503 表示,我引用 RFC 2612:"The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header."
所以,这根本不是关于请求来自哪里:这完全是关于服务器暂时超载,或者正在维护。检查响应中的 Retry-After header 并应用它;或者,如果缺少,"retry later" 更笼统。
如果持续存在(它不应该是:503 意味着服务器正在遭受 临时 条件),请联系网站系统管理员并获得对发生的情况的解释。重复一遍,这完全是关于您正在联系的网络服务器的,应该是临时情况;完全不关心你的客户。