Last active
December 3, 2015 16:08
-
-
Save PaulSec/568cd34b65c9271e219c to your computer and use it in GitHub Desktop.
Quick snippet to start Tor and send a HTTP request
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 io | |
import socks | |
import socket | |
import urllib2 | |
import stem.process | |
from stem.util import term | |
SOCKS_PORT = 7000 | |
def create_connection(address, timeout=None, source_address=None): | |
sock = socks.socksocket() | |
sock.connect(address) | |
return sock | |
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", SOCKS_PORT) | |
socket.socket = socks.socksocket | |
socket.create_connection = create_connection | |
def query(url): | |
return urllib2.urlopen(url).read() | |
# Start an instance of Tor configured to only exit through Russia. This prints | |
# Tor's bootstrap information as it starts. Note that this likely will not | |
# work if you have another Tor instance running. | |
def print_bootstrap_lines(line): | |
if "Bootstrapped " in line: | |
print(term.format(line, term.Color.BLUE)) | |
print(term.format("Starting Tor:\n", term.Attr.BOLD)) | |
tor_process = stem.process.launch_tor_with_config( | |
config = { | |
'SocksPort': str(SOCKS_PORT), | |
'ExitNodes': '{ru}', | |
}, | |
init_msg_handler = print_bootstrap_lines, | |
) | |
print(term.format("\nChecking our endpoint:\n", term.Attr.BOLD)) | |
print(term.format(query("https://www.atagar.com/echo.php"), term.Color.BLUE)) | |
tor_process.kill() # stops tor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment