Using SSH Socks Proxy with requests

  1. Install the optional socks proxy
pip install requests[socks]
  1. Connect your ssh tunnel:
ssh username@host.example -D 1234

(this will create a SOCKS5 proxy on localhost:1234)

  1. 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

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()