这个使用 tornado 的脚本在某些实时主机上获得 HTTP 响应代码 500。请不要介意循环。这是由于过度简化了我的代码块。我试过使用 ip 但无济于事。
#!/usr/bin/python
import tornado
from tornado import httpclient
from tornado import gen
from tornado.httpclient import AsyncHTTPClient
gloop = tornado.ioloop.IOLoop.instance()
@gen.engine
def process(url):
print url
try:
http_client = httpclient.AsyncHTTPClient()
request = tornado.httpclient.HTTPRequest(url=str(url), connect_timeout=5.0, validate_cert = False, request_timeout=5.0, follow_redirects=True)
response = yield tornado.gen.Task(http_client.fetch, request)
print url
print response.code
if response.error: raise Exception(response.error)
except Exception as e:
print e
gloop.add_callback(process, 'http://www.dhlsameday.com')
tornado.httpclient.AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
gloop.start()
最佳答案
尽管不信任证书。起初我怀疑是机器人的一些安全措施,但这个网站只是在错误处理方面存在问题,它需要 Accept-Language
。甚至 curl 都失败了。要开始工作,只需传递该 header :
headers = {"Accept-Language": "en-US;q=0.7,en;q=0.3"}
request = tornado.httpclient.HTTPRequest(url=str(url), headers=headers, connect_timeout=5.0, validate_cert = False, request_timeout=5.0, follow_redirects=True)
我建议添加更多常见的浏览器 header
- 接受
- 接受编码
- 接受语言
- 连接
- User-Agent(某些浏览器不被视为机器人)
关于python - Tornado 脚本在实时主机上获取响应代码 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33758376/