Python request with Socks5 proxy

Jun 15, 2018

I have to request some APIs behind the gateway, and all I have is ssh access to API server. So it can be good to use ssh proxy.

First, create the socks5 proxy:

$ ssh  -N -D 127.0.0.1:9090 <user>@<app-server> -p <port>

Then use it in our python code like this:

$ pip install -U requests[socks]
import requests
resp = requests.get(
    'https://url', 
    proxies={
        'http': 'socks5h://127.0.0.1:9090',
        'https': "socks5h://127.0.0.1:9090"
    }
)

socks5h: the h means requests should resolve DNS queries on remote side.