allow http/https requests through HTTPS Proxy.
- requests >= 2.19.0
- PyOpenSSL >= 0.11
- tlslite-ng
The preferred way of using the library is importing and using the SecureProxySession. It has the exact same behavior as your usual requests.Session, but has secure HTTPS proxy support:
fromrequests_httpsproxyimportSecureProxySessionhttps_proxy='https://username:password@host:port'withSecureProxySession() ass:
print (s.get('https://httpbin.org/ip', proxies={'http':https_proxy, 'https':https_proxy}).text)In case you want to enable secure HTTPS proxy support project wise, you can patch the requests library:
importrequestsfromrequests_httpsproxyimportpatch_requestspatch_requests()
https_proxy='https://username:password@host:port'withrequests.Session() ass:
print (s.get('https://httpbin.org/ip', proxies={'http':https_proxy, 'https':https_proxy}).text)Keep in mind, that enabling the secure HTTPS proxy breaks the behavior of regular HTTPS proxies. If you want to use both, use the SecureProxySession for secure proxies and requests.Session for the regular HTTPS proxies.
An other solution would be to always use the patch or SecureProxySession, but set verify = False, which disables verifying the SSL certificate:
fromrequests_httpsproxyimportSecureProxySessionhttps_proxy='https://username:password@host:port'withSecureProxySession() ass:
s.verify=Falseprint (s.get('https://httpbin.org/ip', proxies={'http':https_proxy, 'https':https_proxy}).text)In case you don't want to verify the secure proxy's SSL certificate (for example, when making requests using proxy IP),
you can disable the verification by passing insecure_requests=True:
fromrequests_httpsproxyimportSecureProxySessionhttps_proxy='https://username:password@host:port'withSecureProxySession(insecure_requests=True) ass:
print (s.get('https://httpbin.org/ip', proxies={'http':https_proxy, 'https':https_proxy}).text)If the proxy credentials contain symbols, that can't be present in a url (causing a parse error), try encoding them with quote_plus:
importurllib.parsefromrequests_httpsproxyimportSecureProxySessionusername_encoded=urllib.parse.quote_plus(username)
password_encoded=urllib.parse.quote_plus(password)
https_proxy='https://{}:{}@host:port'.format(username_encoded, password_encoded)
withSecureProxySession() ass:
s.verify=Falseprint (s.get('https://httpbin.org/ip', proxies={'http':https_proxy, 'https':https_proxy}).text)MIT
- https://github.com/kennethreitz/requests/issues/1182
- https://github.com/kennethreitz/requests/issues/1622
- https://github.com/kennethreitz/requests/issues/1903
- https://github.com/kennethreitz/requests/issues/3468
- https://github.com/kennethreitz/requests/issues/3806
- https://github.com/kennethreitz/requests/issues/3882