public
Created

Basic request - Python

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
def basic_request(request, verbose=False):
""" request is in form of a urllib2.Request()
Return (False, error) OR
(True, response, url, info)
"""
error = False
result = []
try:
response = urlopen(request,timeout=100)
except IOError, e:
if hasattr(e,'reason'):
error = ("FAILED to reach server", e.reason)
elif hasattr(e, 'code'):
error = ("FAIL - Server could not fulfill request", e.code)
except Exception as inst:
error = ("unspecified error occurred", inst.args)
else: # all good
result = (response.read(), response.geturl(), response.info())
if verbose:
print "Requested:\n %s" % request
print "Response:\n %s" % result
return (True, result)
# Failed - return error codes
if verbose:
print "Error", error
return (False, error)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.