Skip to content

Instantly share code, notes, and snippets.

@jefftriplett
Last active May 22, 2023 09:10
Show Gist options
  • Star 52 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save jefftriplett/9748036 to your computer and use it in GitHub Desktop.
Save jefftriplett/9748036 to your computer and use it in GitHub Desktop.
Python Requests + Tor (Socks5)
"""
setup:
pip install requests
pip install requests[socks]
super helpful:
- http://packetforger.wordpress.com/2013/08/27/pythons-requests-module-with-socks-support-requesocks/
- http://docs.python-requests.org/en/master/user/advanced/#proxies
"""
import requests
proxies = {
'http': 'socks5://127.0.0.1:9150',
'https': 'socks5://127.0.0.1:9150'
}
def main():
url = 'http://ifconfig.me/ip'
response = requests.get(url)
print('ip: {}'.format(response.text.strip()))
response = requests.get(url, proxies=proxies)
print('tor ip: {}'.format(response.text.strip()))
if __name__ == '__main__':
main()
@doomzhou
Copy link

doomzhou commented Mar 4, 2015

hi buddy how python3 do it ?

@PegasusWang
Copy link

I got this error:
requesocks.exceptions.ConnectionError: HTTPConnectionPool(host='icanhazip.com', port=None): Max ret
ries exceeded with url: http://icanhazip.com/.

@karelbilek
Copy link

yep, me too

@ydaniels
Copy link

not working never connects to any url

@AmineCherrai
Copy link

Try to change port to 9050

session.proxies = {
'http': 'socks5://127.0.0.1:9050',
'https': 'socks5://127.0.0.1:9050'
}

@deweydb
Copy link

deweydb commented Mar 22, 2016

very old but still better solution:http://dae.me/blog/1959/using-pythons-urllib2-or-requests-with-a-socks5-proxy/

as requesocks is something like 2800 commits behind requests.

@PegasusWang
Copy link

now requests support socks proxy. version > 2.10.0

@jefftriplett
Copy link
Author

Sorry I didn't see these comments until today. I updated the snippet to work with Python3 + requests + requests[socks]. I'm still using port 9150 which works out of the box on the Mac TorBrowser. Please use this responsibly.

@dznet
Copy link

dznet commented Oct 3, 2017

How to open .onion domain?

Traceback (most recent call last):
  File "tor.py", line 31, in <module>
    main()
  File "tor.py", line 23, in main
    response = requests.get(url)
  File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/requests/adapters.py", line 508, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError:
    HTTPConnectionPool(host='zqktlwi4fecvo6ri.onion', port=80):
    Max retries exceeded with url: /
    (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0a70d62470>:
    Failed to establish a new connection: [Errno -2] Name or service not known',))

@itJunky
Copy link

itJunky commented Oct 4, 2017

I have same error like in dzNET post when try to open .onion sites. =(
How to fix it?

@siddiquim-vmware
Copy link

(Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fd84813fc10>: Failed to establish a new connection: [Errno -2] Name or service not known',))

@denzuko
Copy link

denzuko commented Dec 16, 2017

@dznet, @itJunky, @siddiquim-vmware add a .to so your hiddenwiki host value becomes zqktlwi4fecvo6ri.onion.to

Now, time for the red pill:

The request module is not looking up the .onion domain via the socks proxy but via standard dns. Since there is no root onion domain provided by icann or other NIC operators your lookup goes off to the root dns servers then dies.

The solution is to use the socks5h:// protocol in order to enable remote DNS resolving via the socks proxy in case the local DNS resolving process fails. Use The Source Luke.

@kendalvictor
Copy link

$ sudo apt-get install tor
$ sudo netstat -tlnp
and search local addres of TOR

@rootVIII
Copy link

rootVIII commented Aug 8, 2018

For what it’s worth... check out ProxyRequests.... may not be as untraceable as tor but definitely works: https://github.com/rootVIII/proxy_requests

@ice1x
Copy link

ice1x commented Dec 6, 2018

Thank you very much good man!

@MikeyPPPPPPPP
Copy link

Try port 9150 with tor browser open if that didn't work

@black15
Copy link

black15 commented Jun 19, 2020

1- Tor Should be Installed
sudo apt install tor
2- Tor Service must be opened
sudo service tor start
3- install requirements
pip install requests
pip install requests[socks]
4 - change the port in the tool from 9150 to 9050
5 - RUN :)

@cj-praveen
Copy link

its works, thank a lot...

@OkkarMin
Copy link

@dznet, @itJunky, @siddiquim-vmware add a .to so your hiddenwiki host value becomes zqktlwi4fecvo6ri.onion.to

Now, time for the red pill:

The request module is not looking up the .onion domain via the socks proxy but via standard dns. Since there is no root onion domain provided by icann or other NIC operators your lookup goes off to the root dns servers then dies.

The solution is to use the socks5h:// protocol in order to enable remote DNS resolving via the socks proxy in case the local DNS resolving process fails. Use The Source Luke.

This worked for me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment