A simple way to send your requests with tor using tor-proxy.It doesn’t interfere with other tor processes on your computer, so you can use the Tor Browser or the system tor on their own.
Use it only for educational purpose.
- No need root permission
- Multiple instances
Python 3.6+ is required.
pip install tor-proxy
- Import with
from tor_proxy import tor_proxy
. - call function
tor_proxy()
, store as variable and give it as port argument in proxies.
# tor_proxy_example.py
from tor_proxy import tor_proxy
import requests
port = tor_proxy()
http_proxy = f"socks5h://127.0.0.1:{port}"
https_proxy = f"socks5h://127.0.0.1:{port}"
proxies = {
"http" : http_proxy,
"https" : https_proxy,
}
url = "https://api.ipify.org"
r = requests.get(url, proxies=proxies)
print(r.text)
After installing, you can run the Tor proxy service directly from the command line:
tor-proxy
This will start the Tor SOCKS proxy and print the port it is running on.
$ tor-proxy
connecting_to_tor: 100% - Done
Tor SOCKS proxy running on port: 47863
-
Run as a background (detached) process:
tor-proxy --detach
This will start the Tor proxy in the background.
-
Kill all running Tor processes started with
/usr/bin/tor -f /tmp/...
:tor-proxy --kill
This will find and terminate all matching Tor processes.
Now you can use this port as proxy in code.
# tor_proxy_example.py
import requests
port = 47863
http_proxy = f"socks5h://127.0.0.1:{port}"
https_proxy = f"socks5h://127.0.0.1:{port}"
proxies = {
"http" : http_proxy,
"https" : https_proxy,
}
url = "https://api.ipify.org"
r = requests.get(url, proxies=proxies)
print(r.text)