def __init__(self, url, headers=None): self.url = url self.headers = headers self.origin_req_host = urllib2.request_host(self) self.type, r = urllib.splittype(url) self.host, r = urllib.splithost(r) if self.host: self.host = urllib.unquote(self.host)
import urllib2 ''' Accept:image/webp,*/*;q=0.8 Accept-Encoding:gzip, deflate, sdch Accept-Language:zh-CN,zh;q=0.8 Cache-Control:no-cache Connection:keep-alive Cookie:Hm_lvt_e99aee90ec1b2106afe7ec3b199020a7=1449208971; Hm_lpvt_e99aee90ec1b2106afe7ec3b199020a7=1449211056 Host:staticlive.douyutv.com Pragma:no-cache Referer:http://www.douyutv.com/227764 User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36 ''' url = r'http://www.douyutv.com/9703' headers = ('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36') try: open = urllib2.request_host("119.130.184.114") except urllib2.HTTPError, e: print e.code
# -*- coding: utf-8 -*- import urllib, urllib2 a = urllib2.request_host('http://www.baidu.com') type(a)