问题描述
我正在尝试使用身份验证代理配置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,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 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
解决方法
我不确定这是否是唯一的问题,但是NotImplementedError是因为Windows上的Python 3.8使用了与Tornado不兼容的不同事件循环实现。您需要在主文件/函数的开头添加asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
。
我怀疑您可能还需要使用ca_certs
参数来告诉libcurl
在哪里找到代理的受信任根证书。