Tornado脚本在实时hos上获得响应代码500

2024-04-20 09:26:12 发布

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

这个使用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()

Tags: 代码fromimportclienthttpurlresponserequest
1条回答
网友
1楼 · 发布于 2024-04-20 09:26:12

尽管存在不可信的证书。起初我怀疑对机器人程序来说是安全的,但是这个网站在错误处理方面有问题,它需要Accept-Language。即使是卷发也会失败。要开始工作,只需传递标题:

    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) 

我建议添加更常见的浏览器标题

  • 接受
  • 接受编码
  • 接受语言
  • 连接
  • 用户代理(某些浏览器不被视为bot)

相关问题 更多 >