今天想要访问部署在服务器上的api,却发现怎么也访问不到o(╥﹏╥)o 无奈死了。最后知道了方法,记录于此:
首先,看看报的错
requests.exceptions.ProxyError: HTTPConnectionPool(host='xxx', port=8080): Max retries exceeded with url: xxxx (Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(104, 'Connection reset by peer')))
看起来是连接的问题,但是可以ping通啊,解决方法是要绕过系统设置的代理就好(不知道为啥莫名其妙的就需要绕过代理了,明明之前访问的时候还好好的)
原来的访问方式是:
r = requests.request("get", api_url, data=json.dumps(data), headers=headers)
现在再加一个 proxies,即可:
proxies = { "http": None, "https": None}
r = requests.request("get", api_url, data=json.dumps(data), headers=headers, proxies=proxies)
据说这种方式也有效的:
session = requests.Session()
session.trust_env = False
response = session.get('http://ff2.pw')