How to pass all Python traffic through http proxy?

I want to transfer all Python traffic through an HTTP proxy server, I checked urlib2 and, for example, requests packets, they can be configured to use proxy servers, but how can I use something like a proxy server system for Python for proxy server data?

+15
source share
3 answers

Linux system first exports 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 you want to go 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 the python script

 $ python my_script.py 

UPDATE

You can also use redsocks with this tool, you can silently redirect all your TCP connections to PROXY with or without authentication. But you have to take care, because this is for all compounds, not just python.

Windows systems , you can use tools such as FreeCap , Proxifier , ProxyCap and configure to work with python executable

+22
source

Admittedly, this is not exactly what you are looking for, but if you know the programmatic source of all network traffic in your Python code, you can do this proxy packaging in the code itself. A clean python solution is proposed 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 with httplib2 will use these proxy settings. You should be able to wrap any network module that uses sockets to use this proxy.

https://github.com/Anorov/PySocks

It is mentioned here that this is not recommended, but it seems to work fine.

+4
source

enter image description here

Can someone help me deal with the above problem, I tried everything that could not pass the username and password.

0
source

All Articles