#!/usr/bin/python
# encoding=utf-8
import urllib2


class RedirctHandler(urllib2.HTTPRedirectHandler):
    def http_error_301(self, req, fp, code, msg, headers):
        pass

    def http_error_302(self, req, fp, code, msg, headers):
        pass


def getUnRedirectUrl(url, timeout=10):
    debug_handler = urllib2.HTTPHandler(debuglevel=0)
    opener = urllib2.build_opener(debug_handler, RedirctHandler)
    try:
        response = opener.open(url, timeout=timeout) 
        print response.code
    except urllib2.URLError as e:
        if hasattr(e, 'code'):
            print e.code
        elif hasattr(e, 'reason'):
            print e.reason


getUnRedirectUrl('http://www.baidu.com/App_Themes/Default/css/general.css')
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐