带有https身份验证代理的AsyncHTTPClient

2024-09-29 01:33:02 发布

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

我正在尝试使用身份验证代理配置AsyncHTTPClient以访问https网站。可以使用经过身份验证的代理吗

from tornado import httpclient, ioloop

config = {
    'proxy_host': proxy_host,
    'proxy_port': proxy_post,
    "proxy_username": proxy_username,
    "proxy_password": proxy_password
}


httpclient.AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")

def handle_request(response):
    if response.error:
        print("Error:", response.error)
    else:
        print(response.body)
    ioloop.IOLoop.instance().stop()

http_client = httpclient.AsyncHTTPClient()
http_client.fetch("https://twitter.com/",
    handle_request, **config)
ioloop.IOLoop.instance().start()

我在运行上面的代码之后得到了这些错误

Traceback (most recent call last):
  File "C:\Users\Adam\Anaconda3\envs\sizeer\lib\site-packages\tornado\curl_httpclient.py", line 130, in _handle_socket
    self.io_loop.add_handler(fd, self._handle_events, ioloop_event)
  File "C:\Users\Adam\Anaconda3\envs\sizeer\lib\site-packages\tornado\platform\asyncio.py", line 103, in add_handler
    self.asyncio_loop.add_writer(fd, self._handle_events, fd, IOLoop.WRITE)
  File "C:\Users\Adam\Anaconda3\envs\sizeer\lib\asyncio\events.py", line 507, in add_writer
    raise NotImplementedError
NotImplementedError
Traceback (most recent call last):
  File "C:\Users\Adam\Anaconda3\envs\sizeer\lib\site-packages\tornado\curl_httpclient.py", line 130, in _handle_socket
    self.io_loop.add_handler(fd, self._handle_events, ioloop_event)
  File "C:\Users\Adam\Anaconda3\envs\sizeer\lib\site-packages\tornado\platform\asyncio.py", line 97, in add_handler
    raise ValueError("fd %s added twice" % fd)
ValueError: fd 700 added twice
ERROR:asyncio:Future exception was never retrieved
future: <Future finished exception=HTTP 599: SSL certificate problem: unable to get local issuer certificate>
tornado.curl_httpclient.CurlError: HTTP 599: SSL certificate problem: unable to get local issuer certificate

Process finished with exit code -1

Tags: selfaddlibuserstornadofileproxyhandle
1条回答
网友
1楼 · 发布于 2024-09-29 01:33:02

我不确定这是否是这里唯一的问题,但NotImplementedError是因为Windows上的Python 3.8使用了与Tornado不兼容的不同事件循环实现。您需要将asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())添加到主文件/函数的开头

我怀疑您可能还需要使用ca_certs参数来告诉libcurl在哪里可以找到代理的受信任根证书

相关问题 更多 >