Python set linux proxy

theredspy15 profile image Hunter Drum ・1 min read

So I am working on a Python project for a Raspberry Pi, that will download a list of proxies daily from a site.

However, whenever I lookup/google how to set the system proxy, I am unable to achieve anything. When I load up firefox, my IP is always the same.

The proxies in the list are http, https, socks4, and socks5.

Anyone know of a way to set the system proxy dynamically based on the type of proxy via Python on Linux (Debian)?

What I have so far:

os.environ[str(proxyData["protocol"]) + "_proxy"] = str(proxyData["ip"]) + ":" + str(proxyData["port"])

What I'm shooting for:
client -> (openvpn) -> Pi -> (Python/Proxy) -> internet


markdown guide

I assume that you use applications, that respect the environment variables like
proxy_http, proxy_https, proxy_ftp etc.
There is no single configuration file or option as you might have seen on
Windows system.
Rather, as they are environment variables, they are added to a script that is
sourced when a user logs in and by doing so settings these.
The most promiment one on Linux (or many BSD or UNIX-like OSs) is /etc/profile.
So your Python script would need to alter this script to reflect the newly
detected proxies.
For example you would need to add lines like

export http_proxy="proxy-ip:proxy-port"

These settings are only taken into account when a new login session is started or you explicitly source the file in shell like

source /etc/profile
. /etc/profile

If you only set it like you did these settings will never persist or by available outside any process you start outside of your Python process.

Hope this gives you some ideas where to go.


First, thank you for replying!

I have already attempted: export http_proxy="proxy-ip:proxy-port"

I also would like to add, I'm only trying to get openvpn to use the proxy

What I'm shooting for:
client -> (openvpn) -> Pi -> (Python/Proxy) -> internet


Then the easiest thing to do would be to start openvpn from within your python script.
Set the environment variables and do a os.system call or use the subprocess module.

That's genius! I will try that right away!

I kinda feel a little silly, as that is rather simple