Using SSH Socks Proxy with requests
- Install the optional socks proxy
pip install requests[socks]
- Connect your ssh tunnel:
ssh username@host.example -D 1234
(this will create a SOCKS5 proxy on localhost:1234
)
- Make requests:
import requests
# Make the POST request
response = requests.post(
url,
headers=headers,
json=data,
proxies={"https": "socks5h://localhost:1234", "http": "socks5h://localhost:1234"},
)
We have to pass a proxies map to the requests post method with options for http and https so that it knows how to proxy each type of request.
The h
in socks5h
makes the server resolve DNS requests to the resource which may be useful if on the client side you are running behind some kind of proxy like SQUID.
Reference
- https://stackoverflow.com/questions/12601316/how-to-make-python-requests-work-via-socks-proxy
- https://docs.brightdata.com/proxy-networks/socks5#difference-between-socks5-and-socks5h
Suppress Warnings about Self-Signed Certificates
Be aware that this is very bad practice in a production environment - you want to know when certificates are not working properly because that could be a sign that your system is compromised.
Make the request with requests.post("https://example.com/.../" verify=False)
You can disable warning output with:
import requests
requests.packages.urllib3.disable_warnings()