Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 444
habedi Avatar asked Jul 26 '15 17:07

habedi


People also ask

How do I run a Python script from proxy?

To use a proxy in Python, first import the requests package. Next create a proxies dictionary that defines the HTTP and HTTPS connections. This variable should be a dictionary that maps a protocol to the proxy URL. Additionally, make a url variable set to the webpage you're scraping from.

How do I create an HTTP proxy server in Python?

Creating an incoming socket We create a socket serverSocket in the __init__ method of the Server Class. This creates a socket for the incoming connections. We then bind the socket and then wait for the clients to connect.

What is Python proxy?

Proxy is a structural design pattern that provides an object that acts as a substitute for a real service object used by a client. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object.


1 Answers

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

like image 121
efirvida Avatar answered Sep 26 '22 21:09

efirvida