In [1]:
import urllib2
import httplib
import requests

In [ ]:
response = requests.get("http://fb.me/4FiCLm7fx", timeout=10)
response.url


Out[ ]:
u'http://www.judicialwatch.org/blog/2016/08/dod-lets-workers-use-govt-plastic-strip-clubs-casinos-potential-national-security-vulnerabilities/'

In [2]:
TESTING_URL="http://gvwy.io/alh1b4o"

In [3]:
response = urllib2.urlopen("http://bit.ly/AoifeMcL_ID3") # Some shortened url
response.url


Out[3]:
'https://www.youtube.com/user/IgniteDublin'

In [4]:
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
response = opener.open('http://fb.me/7z2JmTFOc')
response.url


Out[4]:
'http://thinkingmomsrevolution.com/rolled-eyes-parents-said-vaccines-caused-kids-autism/'

In [5]:
opener.handlers


Out[5]:
[<urllib2.UnknownHandler instance at 0x7fe5ec15c950>,
 <urllib2.HTTPHandler instance at 0x7fe5ec151170>,
 <urllib2.HTTPDefaultErrorHandler instance at 0x7fe5ec16c9e0>,
 <urllib2.HTTPRedirectHandler instance at 0x7fe5ec16c560>,
 <urllib2.FTPHandler instance at 0x7fe5ec16cb00>,
 <urllib2.FileHandler instance at 0x7fe5ec160488>,
 <urllib2.HTTPSHandler instance at 0x7fe5ec160200>,
 <urllib2.HTTPErrorProcessor instance at 0x7fe5ec160710>]

In [6]:
try:
    response = opener.open('http://fb.me/7z2JmTFOc', timeout=100)
    response.url
except urllib2.HTTPError as e:
    print "HTTPError"
    print e
except urllib2.URLError as e:
    print "URLError"
    print e
except Exception as e:
    print "Other exception"
    print e
print response.url


http://thinkingmomsrevolution.com/rolled-eyes-parents-said-vaccines-caused-kids-autism/

In [7]:
class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_301(self, req, fp, code, msg, headers):
        print "Error 301"
        print headers
        result = urllib2.HTTPRedirectHandler.http_error_301(
            self, req, fp, code, msg, headers)              
        result.status = code
        result.extra_headers = headers
        return result                                       

    def http_error_302(self, req, fp, code, msg, headers):
        print "Error 302"
        print headers
        result = urllib2.HTTPRedirectHandler.http_error_302(
            self, req, fp, code, msg, headers)              
        result.status = code                                
        return result

In [8]:
opener = urllib2.build_opener(SmartRedirectHandler)
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
response = opener.open('http://fb.me/7z2JmTFOc')
response.url


Error 301
Location: http://thinkingmomsrevolution.com/rolled-eyes-parents-said-vaccines-caused-kids-autism/
X-Frame-Options: DENY
X-XSS-Protection: 0
Access-Control-Allow-Credentials: true
Vary: Origin
Pragma: no-cache
Access-Control-Allow-Origin: http://fb.me
Access-Control-Expose-Headers: X-FB-Debug, X-Loader-Length
access-control-allow-method: OPTIONS
Expires: Sat, 01 Jan 2000 00:00:00 GMT
X-Content-Type-Options: nosniff
Cache-Control: private, no-cache, no-store, must-revalidate
Vary: Accept-Encoding
Content-Type: text/html
X-FB-Debug: w3MWW/cx0IZ7EWegby5C+jXwHbwMDSaR/fBDPY5AW1e/ot80+slgxytP45X6tMHm3H290DAU+i5J5hikF1VwsQ==
Date: Tue, 21 Feb 2017 05:36:42 GMT
Connection: close
Content-Length: 0

Out[8]:
'http://thinkingmomsrevolution.com/rolled-eyes-parents-said-vaccines-caused-kids-autism/'

In [9]:
response.extra_headers["LOCATION"]


Out[9]:
'http://thinkingmomsrevolution.com/rolled-eyes-parents-said-vaccines-caused-kids-autism/'

In [10]:
response.org_loc = 10

In [11]:
opener.handlers


Out[11]:
[<urllib2.UnknownHandler instance at 0x7fe5ec1511b8>,
 <urllib2.HTTPHandler instance at 0x7fe5ec151128>,
 <urllib2.HTTPDefaultErrorHandler instance at 0x7fe5ec1510e0>,
 <urllib2.FTPHandler instance at 0x7fe5ec160cf8>,
 <urllib2.FileHandler instance at 0x7fe5ec160c20>,
 <urllib2.HTTPSHandler instance at 0x7fe5ec160f80>,
 <__main__.SmartRedirectHandler instance at 0x7fe5ec160f38>,
 <urllib2.HTTPErrorProcessor instance at 0x7fe5ec160fc8>]

In [13]:
response = requests.get('http://fb.me/7z2JmTFOc')

In [14]:
response.url


Out[14]:
u'http://thinkingmomsrevolution.com/rolled-eyes-parents-said-vaccines-caused-kids-autism/'

In [15]:
response.history[0].headers["Location"]


Out[15]:
'http://thinkingmomsrevolution.com/rolled-eyes-parents-said-vaccines-caused-kids-autism/'

In [16]:
try:
    response = requests.get(TESTING_URL, timeout=10)
except requests.RequestException as e:
    print e


HTTPConnectionPool(host='mabelsaveforschool.com', port=80): Max retries exceeded with url: /contest-entry (Caused by ConnectTimeoutError(<requests.packages.urllib3.connection.HTTPConnection object at 0x7fe5ddf71cd0>, 'Connection to mabelsaveforschool.com timed out. (connect timeout=10)'))

In [17]:
e.request.url


Out[17]:
'http://mabelsaveforschool.com/contest-entry'

In [18]:
try:
    e = None
    a = None
    a.append(1)
except IndexError as e:
    print e, type(e)
except Exception as e:
    print "General exp block"
    print e, type(e)
else:
    print "Else block"
    print e, type(e)


General exp block
'NoneType' object has no attribute 'append' <type 'exceptions.AttributeError'>

In [19]:
response = requests.get("http://fb.me/7z2JmTFOc")
response.url


Out[19]:
u'http://thinkingmomsrevolution.com/rolled-eyes-parents-said-vaccines-caused-kids-autism/'

In [21]:
TESTING_URL = "http://fb.me/PeRMrk16"
try:
    response = requests.get(TESTING_URL, timeout=10)
except requests.RequestException as e:
    print e


HTTPConnectionPool(host='personalhealthdiary.co', port=80): Max retries exceeded with url: /fda-announce-that-dtap-vaccine-causes-autism/ (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fe5dded1990>: Failed to establish a new connection: [Errno -2] Name or service not known',))

In [22]:
e.request.url


Out[22]:
'http://personalhealthdiary.co/fda-announce-that-dtap-vaccine-causes-autism/'

In [ ]: