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

随声附和 提交于 2019-12-18 12:14:55

问题


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?


回答1:


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

An 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 take care 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




回答2:


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.



来源:https://stackoverflow.com/questions/31639742/how-to-pass-all-pythons-traffics-through-a-http-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!