Skip to content

Instantly share code, notes, and snippets.

@russau
Created December 5, 2014 23:03
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 russau/63f8d8dcbfbcfc2a8418 to your computer and use it in GitHub Desktop.
Save russau/63f8d8dcbfbcfc2a8418 to your computer and use it in GitHub Desktop.
s3 redirect diagnose
import urllib2
import datetime
import time
import socket
domain = "201412051453-oregon-cats.s3.amazonaws.com"
# http://stackoverflow.com/questions/17202364/urllib2-urlopen-adding-host-header
while 1:
with open("hitter_log", "a") as myfile:
try:
ip = socket.gethostbyname(domain)
url = 'http://' + ip + "/cat1.jpeg"
headers = { 'Host' : domain }
req = urllib2.Request(url, None, headers) # make POST request to url with data and headers
response = urllib2.urlopen(req) # get the response from the server
log = "{:%Y-%m-%d %H:%M:%S} {} {} \n".format(datetime.datetime.now(), ip, response.geturl());
print(log)
myfile.write(log)
except socket.gaierror as e:
log = "socket error({0}): {1}\n".format(e.errno, e.strerror)
print log
myfile.write(log)
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment