本文整理汇总了Python中urllib2.addinfourl函数的典型用法代码示例。如果您正苦于以下问题:Python addinfourl函数的具体用法?Python addinfourl怎么用?Python addinfourl使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了addinfourl函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_zip
def check_zip(self, resp):
"""
@Def: check_zip
@Description: check web page if it's ziped
@Input: resp #response of web
@Output: data #web page data
@Date: 2015-8-10
"""
if resp.headers.get("content-encoding") == "gzip":
lib.log_print.log_print_debug("Zip page.")
data = resp.read()
gz = gzip.GzipFile(fileobj=StringIO.StringIO(data))
new_resp = urllib2.addinfourl(gz, resp.headers, resp.url, resp.code)
new_resp.msg = resp.msg
return new_resp
elif resp.headers.get("content-encoding") == "deflate":
lib.log_print.log_print_debug(" Deflate page. url: %s" % (resp.geturl()))
data = resp.read()
gz = StringIO.StringIO(self._deflate(data))
new_resp = urllib2.addinfourl(gz, resp.headers, resp.url, resp.code)
new_resp.msg = resp.msg
return new_resp
else:
lib.log_print.log_print_debug(
" Unzip page. Encoding type: %s, url: %s"
% (str(resp.headers.get("content-encoding")), resp.geturl())
)
return resp
示例2: open_local_file
def open_local_file(self, req):
import email.Utils
host = req.get_host()
file = req.get_selector()
localfile = url2pathname(file)
stats = os.stat(localfile)
size = stats.st_size
modified = email.Utils.formatdate(stats.st_mtime, usegmt=True)
mtype = mimetypes.guess_type(file)[0]
headers = mimetools.Message(StringIO(
'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
(mtype or 'text/plain', size, modified)))
if host:
host, port = splitport(host)
if not host or \
(not port and socket.gethostbyname(host) in self.get_names()):
try:
file_list = dircache.listdir(localfile)
s = StringIO()
s.write('<html><head><base href="%s"/></head><body>' % ('file:' + file))
s.write('<p>Directory Content:</p>')
for f in file_list:
s.write('<p><a href="%s">%s</a></p>\n' % (urllib.quote(f), f))
s.write('</body></html>')
s.seek(0)
headers = mimetools.Message(StringIO(
'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
('text/html', size, modified)))
return addinfourl(s, headers, 'file:' + file)
except OSError:
return addinfourl(open(localfile, 'rb'),
headers, 'file:'+file)
raise URLError('file not on local host')
示例3: GetLastUpdateTime
def GetLastUpdateTime():
last_update = None
url = 'http://hq.sinajs.cn/rn=1389255083862&list=gb_dji'
data = ''
user_agent = 'Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0'
headers = { 'User-Agent' : user_agent,
'Host': 'hq.sinajs.cn',
'Accept': '*/*',
'Accept-Language': 'en-us,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
#'Connection': 'keep-alive',
'Referer': 'http://finance.sina.com.cn/stock/usstock/sector.shtml',
}
req = urllib2.Request(url, data, headers)
resp = urllib2.urlopen(req)
old_resp = resp
if resp.headers.get("content-encoding") == "gzip":
gz = GzipFile(
fileobj=StringIO(resp.read()),
mode="r"
)
#resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url)
resp.msg = old_resp.msg
#json_html = gz.read()
#print 'xxx'
# deflate
if resp.headers.get("content-encoding") == "deflate":
gz = StringIO( deflate(resp.read()) )
#resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code) # 'class to add info() and
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url) # 'class to add info() and
resp.msg = old_resp.msg
#json_html = gz.read()
#print 'YY'
js_script = resp.read()
#print js_script
last_update_time = js_script.split(',')[25]
#print last_update_time
#Jan 08 04:13PM EST
ridx = last_update_time.rfind('EST')
if ridx == -1:
#try:
raise ValueError('Timezone EST value not set')
#except ValueError, e:
# print e
else:
last_update_time = last_update_time[:ridx].strip()
print last_update_time
last_update = datetime.datetime.strptime(last_update_time, "%b %d %I:%M%p")
last_update = last_update.replace(datetime.date.today().year)
print last_update
return last_update
示例4: http_response
def http_response(self, request, response):
old_response = response
if response.headers.get("content-encoding") == "gzip":
gzipFile = gzip.GzipFile(fileobj=StringIO.StringIO(response.read()),mode="r")
response = urllib2.addinfourl(gzipFile, old_response.headers, old_response.url, old_response.code)
response.msg = old_response.msg
if response.headers.get("content-encoding") == "deflate":
gzipFile = StringIO.StringIO(deflate(response.read()) )
response = urllib2.addinfourl(gzipFile, old_response.headers, old_response.url, old_response.code)
response.msg = old_response.msg
return response
示例5: http_response
def http_response(self, request, response):
old_response = response
if response.headers.get('Content-Encoding') == 'gzip':
gz = gzip.GzipFile(fileobj=StringIO.StringIO(response.read() ), mode='r')
response = urllib2.addinfourl(gz, old_response.headers, old_response.url, old_response.code)
response.msg = old_response.msg
if response.headers.get('Content-Encoding') == 'deflate':
gz = StringIO.StringIO(zlib.decompressobj(-zlib.MAX_WBITS).decompress(response.read() ) )
response = urllib2.addinfourl(gz, old_response.headers, old_response.url, old_response.code)
response.msg = old_response.msg
return response
示例6: http_response
def http_response(self, req, resp):
old_resp = resp
if resp.headers.get("content-encoding") == "gzip":
gz = gzip.GzipFile( fileobj=BytesIO(resp.read()), mode="r" )
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
# deflate
if resp.headers.get("content-encoding") == "deflate":
gz = BytesIO( self.deflate(resp.read()) )
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
return resp
示例7: http_response
def http_response(self, req, resp):
old_resp = resp
# gzip
if resp.headers.get('content-encoding') == 'gzip':
gz = GzipFile(fileobj=BytesIO(resp.read()), mode='r')
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
# deflate
if resp.headers.get('content-encoding') == 'deflate':
gz = BytesIO( deflate(resp.read()) )
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code) # 'class to add info() and
resp.msg = old_resp.msg
return resp
示例8: http_response
def http_response(self, req, resp):
old_resp = resp
# gzip
if resp.headers.get("content-encoding") == "gzip":
gz = GzipFile(fileobj=StringIO(resp.read()), mode="r")
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
# deflate
if resp.headers.get("content-encoding") == "deflate":
gz = StringIO(deflate(resp.read()))
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code) # 'class to add info() and
resp.msg = old_resp.msg
return resp
示例9: http_open
def http_open(self, req):
url = req.get_full_url()
try:
content = self.urls[url]
except KeyError:
resp = urllib2.addinfourl(StringIO(""), None, url)
resp.code = 404
resp.msg = "OK"
else:
resp = urllib2.addinfourl(StringIO(content), None, url)
resp.code = 200
resp.msg = "OK"
return resp
示例10: http_response
def http_response(self, req, resp):
old_resp = resp
if resp.headers.get("content-encoding") == "gzip":
data = gzip.GzipFile(fileobj=StringIO(resp.read()), mode="r")
resp = urllib2.addinfourl(data, old_resp.headers,
old_resp.url, old_resp.code)
resp.msg = old_resp.msg
if resp.headers.get("content-encoding") == "deflate":
data = zlib.decompress(resp.read(), -zlib.MAX_WBITS)
resp = urllib2.addinfourl(data, old_resp.headers,
old_resp.url, old_resp.code)
resp.msg = old_resp.msg
return resp
示例11: mock_response
def mock_response(self, req):
if req.get_full_url() == "http://localhost/test":
resp = urllib2.addinfourl(StringIO("mock file"), "mock message", req.get_full_url())
resp.code = 200
resp.msg = "OK"
return resp
if req.get_full_url() == "http://localhost/typo":
resp = urllib2.addinfourl(StringIO("mock file"), "mock message", req.get_full_url())
resp.code = 404
resp.msg = "Not Found"
return resp
else:
raise SystemExit, 2
示例12: test_ignore_non_index_etag
def test_ignore_non_index_etag(self):
""" Etags are ignored for non-index requests """
req = urllib2.Request('http://foo.com/')
res = urllib2.addinfourl(StringIO('foo'), {'Etag': 'whatevs'}, 'http://foo.com/')
self.cache_handler.http_response(req, res)
self.assertFalse(os.path.exists(self.metadata_path))
self.assertFalse(os.path.exists(self.index_path))
示例13: test_no_etag
def test_no_etag(self):
""" No Etag header == no cache written """
req = urllib2.Request('http://foo.com/index.json')
res = urllib2.addinfourl(StringIO('foo'), {}, 'http://foo.com/index.json')
self.cache_handler.http_response(req, res)
self.assertFalse(os.path.exists(self.metadata_path))
self.assertFalse(os.path.exists(self.index_path))
示例14: http_response
def http_response(self, req, resp):
old_resp = resp
if not req.has_header('Accept-encoding'):
return resp
if req.has_header('Range'):
return resp
if resp.headers.get("content-encoding") == "gzip":
gz = _GzipFile(fileobj=StringIO(resp.read()), mode="r")
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
# deflate
if resp.headers.get("content-encoding") == "deflate":
gz = StringIO(self.deflate(resp.read()))
resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
return resp
示例15: http_open
def http_open(self, req):
req_url = req.get_full_url()
file_path = req_url.replace(base_http_url, base_file_path)
resp = urllib2.addinfourl(open(file_path), "redirected to file", req.get_full_url())
resp.code = 200
resp.msg = "OK"
return resp