本文整理汇总了Python中urllib2.addinfourl方法的典型用法代码示例。如果您正苦于以下问题:Python urllib2.addinfourl方法的具体用法?Python urllib2.addinfourl怎么用?Python urllib2.addinfourl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2
的用法示例。
在下文中一共展示了urllib2.addinfourl方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: http_error_default
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import addinfourl [as 别名]
def http_error_default(self, req, fp, code, msg, hdrs):
infourl = urllib.addinfourl(fp, hdrs, req.get_full_url())
infourl.status = code
infourl.code = code
return infourl
示例2: http_error_302
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import addinfourl [as 别名]
def http_error_302(self, req, fp, code, msg, headers):
infourl = urllib.addinfourl(fp, headers, req.get_full_url())
infourl.status = code
infourl.code = code
return infourl
示例3: http_error_303
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import addinfourl [as 别名]
def http_error_303(self, req, fp, code, msg, headers):
infourl = addinfourl(fp, headers, req.get_full_url())
infourl.status = code
infourl.code = code
return infourl
示例4: http_response
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import addinfourl [as 别名]
def http_response(self, req, resp):
"""Handle encodings in the order that they are encountered."""
encodings = []
headers = resp.headers
for header in headers:
if header.lower() == "content-encoding":
for encoding in headers.get(header, "").split(","):
encoding = encoding.strip()
if encoding:
encodings.append(encoding)
break
if not encodings:
return resp
del headers[header]
fp = resp
while encodings and encodings[-1].lower() == "gzip":
fp = cStringIO.StringIO(fp.read())
fp = gzip.GzipFile(fileobj=fp, mode="r")
encodings.pop()
if encodings:
headers[header] = ", ".join(encodings)
logger.warning("Unrecognized Content-Encoding: %s", encodings[-1])
msg = resp.msg
if sys.version_info >= (2, 6):
resp = urllib2.addinfourl(fp, headers, resp.url, resp.code)
else:
response_code = resp.code
resp = urllib2.addinfourl(fp, headers, resp.url)
resp.code = response_code
resp.msg = msg
return resp
示例5: s3_open
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import addinfourl [as 别名]
def s3_open(self, req):
# The implementation was inspired mainly by the code behind
# urllib.request.FileHandler.file_open().
#
# recipe copied from:
# http://code.activestate.com/recipes/578957-urllib-handler-for-amazon-s3-buckets/
# converted to boto3
if version_info[0] < 3:
bucket_name = req.get_host()
key_name = url2pathname(req.get_selector())[1:]
else:
bucket_name = req.host
key_name = url2pathname(req.selector)[1:]
if not bucket_name or not key_name:
raise URLError('url must be in the format s3://<bucket>/<key>')
s3 = boto3.resource('s3')
key = s3.Object(bucket_name, key_name)
client = boto3.client('s3')
obj = client.get_object(Bucket=bucket_name, Key=key_name)
filelike = _FileLikeKey(obj['Body'])
origurl = 's3://{}/{}'.format(bucket_name, key_name)
if key is None:
raise URLError('no such resource: {}'.format(origurl))
headers = [
('Content-type', key.content_type),
('Content-encoding', key.content_encoding),
('Content-language', key.content_language),
('Content-length', key.content_length),
('Etag', key.e_tag),
('Last-modified', key.last_modified),
]
headers = email.message_from_string(
'\n'.join('{}: {}'.format(key, value) for key, value in headers
if value is not None))
return addinfourl(filelike, headers, origurl)
示例6: http_response
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import addinfourl [as 别名]
def http_response(self, req, resp):
"""Handle encodings in the order that they are encountered."""
encodings = []
headers = resp.headers
encoding_header = None
for header in headers:
if header.lower() == "content-encoding":
encoding_header = header
for encoding in headers[header].split(","):
encoding = encoding.strip()
if encoding:
encodings.append(encoding)
break
if not encodings:
return resp
del headers[encoding_header]
fp = resp
while encodings and encodings[-1].lower() == "gzip":
fp = cStringIO.StringIO(fp.read())
fp = gzip.GzipFile(fileobj=fp, mode="r")
encodings.pop()
if encodings:
headers[encoding_header] = ", ".join(encodings)
logger.warning("Unrecognized Content-Encoding: %s", encodings[-1])
msg = resp.msg
if sys.version_info >= (2, 6):
resp = urllib2.addinfourl(fp, headers, resp.url, resp.code)
else:
response_code = resp.code
resp = urllib2.addinfourl(fp, headers, resp.url)
resp.code = response_code
resp.msg = msg
return resp