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 <user>@<app-server> -p <port>

Then use it in our python code like this:

$ pip install -U requests[socks]
import requests
resp = requests.get(
        'http': 'socks5h://',
        'https': "socks5h://"

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