Reputation: 900
I want to pass all Python's traffics through a http proxy server, I checked urlib2 and requests packages for instance, they can be configured to use proxies but how could I use something like a system-wide proxy for Python to proxy all the data out?
Upvotes: 46
Views: 137452
Reputation: 4855
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
Upvotes: 86
Reputation: 894
Admittedly, this isn't exactly what you are looking for, but if you know the programmatic source of all your network traffic in your python code, you can do this proxy wrapping in the code itself. A pure python solution is suggested using the PySocks
module in the following link:
https://github.com/httplib2/httplib2/issues/22
import httplib2
# detect presense of proxy and use env varibles if they exist
pi = httplib2.proxy_info_from_environment()
if pi:
import socks
socks.setdefaultproxy(pi.proxy_type, pi.proxy_host, pi.proxy_port)
socks.wrapmodule(httplib2)
# now all calls through httplib2 should use the proxy settings
httplib2.Http()
Now every call made using httplib2
will use those proxy settings. You should be able to wrap any networking module that uses sockets to use this proxy.
https://github.com/Anorov/PySocks
They mention there that this isn't recommended, but it seems to be working fine for me.
Upvotes: 6