本文整理匯總了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