Created
May 31, 2020 01:43
-
-
Save samlovescoding/a65d6385ef1460a7a4a0c8ab04479d8a to your computer and use it in GitHub Desktop.
Request Session with Tor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from stem import Signal | |
from stem.control import Controller | |
def get_tor_session(): | |
session = requests.session() | |
session.proxies = {'http': 'socks5://127.0.0.1:9050', | |
'https': 'socks5://127.0.0.1:9050'} | |
return session | |
def renew_connection(): | |
with Controller.from_port(port = 9051) as controller: | |
controller.authenticate(password="password") | |
controller.signal(Signal.NEWNYM) | |
renew_connection() | |
session = get_tor_session() | |
print(session.get("http://httpbin.org/ip").text) | |
print(requests.get("http://httpbin.org/ip").text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment