How to pass all Python’s traffics through a http proxy?

Linux system first export the environment variables like this

$ export http_proxy="http://<user>:<pass>@<proxy>:<port>"
$ export HTTP_PROXY="http://<user>:<pass>@<proxy>:<port>"

$ export https_proxy="http://<user>:<pass>@<proxy>:<port>"
$ export HTTPS_PROXY="http://<user>:<pass>@<proxy>:<port>"

or in the script that you want to pass through the proxy

import os

proxy = 'http://<user>:<pass>@<proxy>:<port>'

os.environ['http_proxy'] = proxy 
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy

#your code goes here.............

then run python script

$ python my_script.py

UPDATE

And also you can use redsocks
With this tool you can redirect silently all your TCP connections to a PROXY with or without authentication. But you have to be carefull because it’s for all connections not only for the python.

Windows systems you can use tools like freecap, proxifier, proxycap, and configure to run behind the python executable

Leave a Comment