Last active
June 22, 2017 13:18
-
-
Save jkotra/63ef3be71502d98176c59f6d494c02c4 to your computer and use it in GitHub Desktop.
tor with py
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 urllib2 | |
import os | |
import socks | |
import socket | |
import string | |
#SOCKS_PORT = 9050 | |
print "Tor port:%d.Make sure tor is running!" % (SOCKS_PORT,) | |
print("\n") | |
# Set socks proxy and wrap the urllib module | |
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT) | |
socket.socket = socks.socksocket | |
# Perform DNS resolution through the socket | |
def getaddrinfo(*args): | |
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))] | |
socket.getaddrinfo = getaddrinfo | |
def ohd(url): | |
""" | |
Uses urllib to fetch a site using SocksiPy for Tor over the SOCKS_PORT. | |
""" | |
try: | |
return urllib2.urlopen(url).read() | |
except: | |
return "Unable to reach %s" % url | |
print "Attained IP" | |
print("\n") | |
print "################################################################" | |
print ohd('http://my-ip.herokuapp.com') | |
print "################################################################" | |
print("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment