python开源库requests的一个坑 Caused by ProxyError('Cannot connect to proxy.', NewConnectionError

ipython 里面

import requests  
r = requests.post("http://www.baidu.com")  

报错信息如下

/Library/Python/2.7/site-packages/requests/sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    473         }
    474         send_kwargs.update(settings)
--> 475         resp = self.send(prep, **send_kwargs)
    476
    477         return resp

/Library/Python/2.7/site-packages/requests/sessions.pyc in send(self, request, **kwargs)
    594
    595         # Send the request
--> 596         r = adapter.send(request, **kwargs)
    597
    598         # Total elapsed time of the request (approximately)

/Library/Python/2.7/site-packages/requests/adapters.pyc in send(self, request, stream, timeout, verify, cert, proxies)
    483
    484             if isinstance(e.reason, _ProxyError):
--> 485                 raise ProxyError(e, request=request)
    486
    487             raise ConnectionError(e, request=request)

ProxyError: HTTPConnectionPool(host='127.0.0.1', port=8888): Max retries exceeded with url: http://httpbin.org/post (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x108100610>: Failed to establish a new connection: [Errno 61] Connection refused',)))  

然后试了一下https的连接

import requests  
r = requests.post("https://www.baidu.com")  

返回结果正常。

非常诡异的问题

import os  
print os.environ  

看了一下,返回结果里面,没有proxy信息,本地电脑的xx-net也是自动代理模式,不会拦截百度。

只好看了一下电脑的网络偏好设置,发现是昨晚fiddler还有charles给手机抓包的时候,开启了http代理,这个设置就算是重启电脑之后还在。手动取消这个设置,一切都正常了。

requests库如下

http://docs.python-requests.org/en/master/user/advanced/

刘摸鱼

退堂鼓表演艺术家

杭州