Skip to content

Instantly share code, notes, and snippets.

@tomsteele
Created June 13, 2013 06:08
Show Gist options
  • Save tomsteele/5771559 to your computer and use it in GitHub Desktop.
Save tomsteele/5771559 to your computer and use it in GitHub Desktop.
Simple python script to look for 401s using requests. Explained for educational audiences.
import sys
import requests
# define an array to hold our hosts
hosts = []
# open the file as the variable f
# this will close the file as soon we're done
with open(sys.argv[1]) as f:
# this is called a list comprehension. it's neat way to loop over
# something and put the results into a list
hosts = [x.rstrip() for x in f.readlines()]
for host in hosts:
# python methods will throw execeptions when something bad happens
# you have to catch them with these try/except blocks
try:
r = requests.get('http://{0}/'.format(host), timeout=0.5)
print r.status_code
if r.status_code == 401:
print host
# if the request timesout it throws a 'requests.exceptions.Timeout'
except requests.exceptions.Timeout:
# pass means keep moving on, nothing to see here
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment