我一直在研究这个问题近20个小时。
我在我的机器上通过Fiddler路由所有网络流量,然后连接到我们的公司代理。一切正常,除了试图使用https访问远程服务器的Python应用程序(http总是正常工作)。
我导出了公司证书并将其粘贴到文件中:C:\ anaconda2 \ envs \ py36 \ Lib \ site-packages \ certifi \ cacert.pem。我还使用verify =在我的requests.get调用中明确设置了它。行为没有区别。
我将本地fiddler代理信息设置为环境变量。 Fiddler还配置为自动进行身份验证。使用http工作没有任何问题。
我似乎只能通过https连接到远程服务器,如果我先去http://www.google.com然后快速尝试使用https进行连接。随后的尝试会产生以下错误
requests.get('http://www.google.com') # always works for any website
<Response [200]>
requests.get('https://www.anaconda.com') # works after visiting http://www.google.com
<Response [200]>
requests.get('https://www.anaconda.com') # always fails, unless visiting http://www.google.com first
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
C:\anaconda2\envs\py36\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
593 if is_new_proxy_conn:
--> 594 self._prepare_proxy(conn)
595
C:\anaconda2\envs\py36\lib\site-packages\urllib3\connectionpool.py in _prepare_proxy(self, conn)
804 conn.set_tunnel(self._proxy_host, self.port, self.proxy_headers)
--> 805 conn.connect()
806
C:\anaconda2\envs\py36\lib\site-packages\urllib3\connection.py in connect(self)
307 # self._tunnel_host below.
--> 308 self._tunnel()
309 # Mark this connection as not reusable
C:\anaconda2\envs\py36\lib\http\client.py in _tunnel(self)
918 raise OSError("Tunnel connection failed: %d %s" % (code,
--> 919 message.strip()))
920 while True:
OSError: Tunnel connection failed: 407 Proxy Authentication Required
During handling of the above exception, another exception occurred:
MaxRetryError Traceback (most recent call last)
C:\anaconda2\envs\py36\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
448 retries=self.max_retries,
--> 449 timeout=timeout
450 )
C:\anaconda2\envs\py36\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
637 retries = retries.increment(method, url, error=e, _pool=self,
--> 638 _stacktrace=sys.exc_info()[2])
639 retries.sleep()
C:\anaconda2\envs\py36\lib\site-packages\urllib3\util\retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
397 if new_retry.is_exhausted():
--> 398 raise MaxRetryError(_pool, url, error or ResponseError(cause))
399
MaxRetryError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',)))
During handling of the above exception, another exception occurred:
ProxyError Traceback (most recent call last)
<ipython-input-49-df48f2544f7e> in <module>
----> 1 requests.get('https://www.google.com')
C:\anaconda2\envs\py36\lib\site-packages\requests\api.py in get(url, params, **kwargs)
73
74 kwargs.setdefault('allow_redirects', True)
---> 75 return request('get', url, params=params, **kwargs)
76
77
C:\anaconda2\envs\py36\lib\site-packages\requests\api.py in request(method, url, **kwargs)
58 # cases, and look like a memory leak in others.
59 with sessions.Session() as session:
---> 60 return session.request(method=method, url=url, **kwargs)
61
62
C:\anaconda2\envs\py36\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
531 }
532 send_kwargs.update(settings)
--> 533 resp = self.send(prep, **send_kwargs)
534
535 return resp
C:\anaconda2\envs\py36\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
644
645 # Send the request
--> 646 r = adapter.send(request, **kwargs)
647
648 # Total elapsed time of the request (approximately)
C:\anaconda2\envs\py36\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
508
509 if isinstance(e.reason, _ProxyError):
--> 510 raise ProxyError(e, request=request)
511
512 if isinstance(e.reason, _SSLError):
ProxyError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',)))
当我提出请求时,其中一个网络人员正在观看公司代理日志。当发出失败的https请求时,他没有在其日志中看到与公司代理的连接。
其他事情尝试过:
谢谢。
对于那些有同样问题的人。
进一步的研究使我下载了Python应用程序Px(px.exe)Px on GitHub and turf Fiddler,它只是间歇性地用于试图上网的Python应用程序。
在我的情况下,PX本身需要ZERO配置。我只需设置http_proxy和https_proxy环境变量,以便任何Python应用程序都知道在哪里汇集其流量。然后我就跑了Px,一切正常。
希望这可以帮助人们。