本文整理汇总了Python中urllib.response.addinfourl方法的典型用法代码示例。如果您正苦于以下问题:Python response.addinfourl方法的具体用法?Python response.addinfourl怎么用?Python response.addinfourl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.response
的用法示例。
在下文中一共展示了response.addinfourl方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_response
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def get_response(self):
"""Returns a copy of the current response."""
return addinfourl(BytesIO(self.data), self._response.info(), self._response.geturl())
示例2: _make_response
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def _make_response(self, result, url):
data = "\r\n".join(["%s: %s" % (k, v) for k, v in result.header_items])
if PY2:
headers = HTTPMessage(BytesIO(data))
else:
import email
headers = email.message_from_string(data)
response = addinfourl(BytesIO(result.data), headers, url)
code, msg = result.status.split(None, 1)
response.code, response.msg = int(code), msg
return response
示例3: data_open
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def data_open(self, req):
# data URLs as specified in RFC 2397.
#
# ignores POSTed data
#
# syntax:
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
# mediatype := [ type "/" subtype ] *( ";" parameter )
# data := *urlchar
# parameter := attribute "=" value
url = req.get_full_url()
scheme, data = url.split(':', 1)
mediatype, data = data.split(',', 1)
# even base64 encoded data URLs might be quoted so unquote in any case:
data = compat_urllib_parse_unquote_to_bytes(data)
if mediatype.endswith(';base64'):
data = binascii.a2b_base64(data)
mediatype = mediatype[:-7]
if not mediatype:
mediatype = 'text/plain;charset=US-ASCII'
headers = email.message_from_string(
'Content-type: %s\nContent-length: %d\n' % (mediatype, len(data)))
return compat_urllib_response.addinfourl(io.BytesIO(data), headers, url)
示例4: open_local_file
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def open_local_file(self, req):
import email.utils
import mimetypes
host = req.host
filename = req.selector
localfile = url2pathname(filename)
try:
stats = os.stat(localfile)
size = stats.st_size
modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
mtype = mimetypes.guess_type(filename)[0]
headers = email.message_from_string(
'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 _safe_gethostbyname(host) in self.get_names()):
if host:
origurl = 'file://' + host + filename
else:
origurl = 'file://' + filename
return addinfourl(open(localfile, 'rb'), headers, origurl)
except OSError as exp:
# users shouldn't expect OSErrors coming from urlopen()
raise URLError(exp)
raise URLError('file not on local host')
示例5: data_open
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def data_open(self, req):
# data URLs as specified in RFC 2397.
#
# ignores POSTed data
#
# syntax:
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
# mediatype := [ type "/" subtype ] *( ";" parameter )
# data := *urlchar
# parameter := attribute "=" value
url = req.full_url
scheme, data = url.split(":",1)
mediatype, data = data.split(",",1)
# even base64 encoded data URLs might be quoted so unquote in any case:
data = unquote_to_bytes(data)
if mediatype.endswith(";base64"):
data = base64.decodebytes(data)
mediatype = mediatype[:-7]
if not mediatype:
mediatype = "text/plain;charset=US-ASCII"
headers = email.message_from_string("Content-type: %s\nContent-length: %d\n" %
(mediatype, len(data)))
return addinfourl(io.BytesIO(data), headers, url)
# Code move from the old urllib module
示例6: open
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def open(self, fullurl, data=None):
"""Use URLopener().open(file) instead of open(file, 'r')."""
fullurl = unwrap(to_bytes(fullurl))
fullurl = quote(fullurl, safe="%/:=&?~#+!$,;'@()*[]|")
if self.tempcache and fullurl in self.tempcache:
filename, headers = self.tempcache[fullurl]
fp = open(filename, 'rb')
return addinfourl(fp, headers, fullurl)
urltype, url = splittype(fullurl)
if not urltype:
urltype = 'file'
if urltype in self.proxies:
proxy = self.proxies[urltype]
urltype, proxyhost = splittype(proxy)
host, selector = splithost(proxyhost)
url = (host, fullurl) # Signal special case to open_*()
else:
proxy = None
name = 'open_' + urltype
self.type = urltype
name = name.replace('-', '_')
if not hasattr(self, name):
if proxy:
return self.open_unknown_proxy(proxy, fullurl, data)
else:
return self.open_unknown(fullurl, data)
try:
if data is None:
return getattr(self, name)(url)
else:
return getattr(self, name)(url, data)
except (HTTPError, URLError):
raise
except OSError as msg:
raise OSError('socket error', msg).with_traceback(sys.exc_info()[2])
示例7: open_data
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def open_data(self, url, data=None):
"""Use "data" URL."""
if not isinstance(url, str):
raise URLError('data error: proxy support for data protocol currently not implemented')
# ignore POSTed data
#
# syntax of data URLs:
# dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
# mediatype := [ type "/" subtype ] *( ";" parameter )
# data := *urlchar
# parameter := attribute "=" value
try:
[type, data] = url.split(',', 1)
except ValueError:
raise OSError('data error', 'bad data URL')
if not type:
type = 'text/plain;charset=US-ASCII'
semi = type.rfind(';')
if semi >= 0 and '=' not in type[semi:]:
encoding = type[semi+1:]
type = type[:semi]
else:
encoding = ''
msg = []
msg.append('Date: %s'%time.strftime('%a, %d %b %Y %H:%M:%S GMT',
time.gmtime(time.time())))
msg.append('Content-type: %s' % type)
if encoding == 'base64':
# XXX is this encoding/decoding ok?
data = base64.decodebytes(data.encode('ascii')).decode('latin-1')
else:
data = unquote(data)
msg.append('Content-Length: %d' % len(data))
msg.append('')
msg.append(data)
msg = '\n'.join(msg)
headers = email.message_from_string(msg)
f = io.StringIO(msg)
#f.fileno = None # needed for addinfourl
return addinfourl(f, headers, url)
示例8: http_error_default
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def http_error_default(self, url, fp, errcode, errmsg, headers):
"""Default error handling -- don't raise an exception."""
return addinfourl(fp, headers, "http:" + url, errcode)
示例9: open_local_file
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def open_local_file(self, req):
import email.utils
import mimetypes
host = req.host
filename = req.selector
localfile = url2pathname(filename)
try:
stats = os.stat(localfile)
size = stats.st_size
modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
mtype = mimetypes.guess_type(filename)[0]
headers = email.message_from_string(
'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 _safe_gethostbyname(host) in self.get_names()):
if host:
origurl = 'file://' + host + filename
else:
origurl = 'file://' + filename
return addinfourl(open(localfile, 'rb'), headers, origurl)
except OSError as exp:
raise URLError(exp)
raise URLError('file not on local host')
示例10: http_error_302
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def http_error_302(self, req, fp, code, msg, headers):
infourl = urllib.addinfourl(fp, headers, headers["Location"])
infourl.status = code
infourl.code = code
return infourl
示例11: get_stream
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def get_stream(self, uri: _Uri) -> Tuple[Union[HTTPResponse, addinfourl], _Uri]:
file, actual_uri = self._get_file_uri(uri)
response, _ = super().get_stream(file)
return response, actual_uri
示例12: get_stream
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def get_stream(self, uri: _Uri) -> Tuple[Union[HTTPResponse, addinfourl], _Uri]:
if not self._valid_uri(uri):
raise ValueError(f'Not remote: {uri!r}')
_log('debug', 'Fetch remote %r in %ss with %r', uri, self._timeout, self._headers)
request = Request(uri, headers=self._headers)
try:
response: Union[HTTPResponse, addinfourl] = urlopen(request, timeout=self._timeout)
except Exception as ex: # pylint: disable=broad-except
_log('debug', 'Caught error on fetch remote %r', uri, exc_info=ex)
raise RemoteFetchError(str(ex), uri) from ex
actual_uri = response.geturl() # after following redirects ...
return response, actual_uri
示例13: test_get_stream
# 需要导入模块: from urllib import response [as 别名]
# 或者: from urllib.response import addinfourl [as 别名]
def test_get_stream(self) -> None:
# arrange
filefetcher = FileFetcher(
dict(
test_file='test.txt',
),
base_dir=self._TESTDATA_DIR
)
# act
stream, _ = filefetcher.get_stream('test_file')
# assert
assert isinstance(stream, addinfourl)