1 2 3 4 5 6 7 8 9 10 11 12 13 | def shorturl (long_url): from urllib import quote from urllib2 import Request, urlopen, URLError, HTTPError url = "http://py6.ru/rub?redirect_url="+str(quote(long_url)) try: urlh = urlopen(url) except HTTPError, e: return False except URLError, e: return False data = urlh.read() urlh.close() return data |