Skip to content

Instantly share code, notes, and snippets.

@PinoCookie
Created May 3, 2019 15:36
Show Gist options
  • Save PinoCookie/e097cf8db5e3e83a606bbedc48732191 to your computer and use it in GitHub Desktop.
Save PinoCookie/e097cf8db5e3e83a606bbedc48732191 to your computer and use it in GitHub Desktop.
Code snippet on how to use tor with python
import requests
session = requests.session()
# Tor controller runs on localhost:9050
session.proxies = {
"http":"socks5h://localhost:9050",
"https":"socks5h://localhost:9050"
}
# Tor browsers default headers
session.headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-us,en;q=0.5",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0"
}
url = input("Url: ")
try:
r = session.get(url, timeout=None, )
except():
print("Error")
print(r.text)
'''
Tor Check
--------------
r = requests.get("https://check.torproject.org/", proxies=proxies) # Sends request to url to see if Tor is working
if("Congratulations" in r.text):
print("Connected to Tor!")
else:
print("Something went wrong, because you aren't connected to Tor")
--------------
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment