Skip to content

Instantly share code, notes, and snippets.

@baatout
Created July 12, 2018 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baatout/5d8b3628744dcea10b019b2ce9a48650 to your computer and use it in GitHub Desktop.
Save baatout/5d8b3628744dcea10b019b2ce9a48650 to your computer and use it in GitHub Desktop.
A part of the crawler tutorial
...
wait_time = 2
number_of_ip_rotations = 3
tor_handler = TorHandler()
ip = tor_handler.open_url('http://icanhazip.com/')
print('My first IP: {}'.format(ip))
# Cycle through the specified number of IP addresses via TOR
for i in range(0, number_of_ip_rotations):
old_ip = ip
seconds = 0
tor_handler.renew_connection()
# Loop until the 'new' IP address is different than the 'old' IP address,
# It may take the TOR network some time to effect a different IP address
while ip == old_ip:
time.sleep(wait_time)
seconds += wait_time
print('{} seconds elapsed awaiting a different IP address.'.format(seconds))
# http://icanhazip.com/ is a site that returns your IP address
ip = tor_handler.open_url('http://icanhazip.com/')
print('My new IP: {}'.format(ip))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment