本文整理汇总了Python中urllib.request.Request.encode方法的典型用法代码示例。如果您正苦于以下问题:Python Request.encode方法的具体用法?Python Request.encode怎么用?Python Request.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.request.Request
的用法示例。
在下文中一共展示了Request.encode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import encode [as 别名]
def download(self, request):
""" """
mimetype = 'application/x-ofx'
HTTPheaders = {'Content-type': mimetype, 'Accept': '*/*, %s' % mimetype}
# py3k - ElementTree.tostring() returns bytes not str
request = self.ofxheader + ET.tostring(request).decode()
# py3k: urllib.request wants bytes not str
request = Request(self.url, request.encode(), HTTPheaders)
try:
with contextlib.closing(urlopen(request)) as response:
# py3k: urlopen returns bytes not str
response_ = response.read().decode()
# urllib2.urlopen returns an addinfourl instance, which supports
# a limited subset of file methods. Copy response to a StringIO
# so that we can use tell() and seek().
source = StringIO()
source.write(response_)
# After writing, rewind to the beginning.
source.seek(0)
self.response = source
return source
except HTTPError as err:
# FIXME
print(err.info())
raise
示例2: download
# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import encode [as 别名]
def download(self, request):
""" """
mimetype = 'application/x-ofx'
HTTPheaders = {'Content-type': mimetype, 'Accept': '*/*, %s' % mimetype, "User-Agent": "httpclient"}
# py3k: ElementTree.tostring() returns bytes not str
request = self.ofxheader + ET.tostring(request).decode()
# py3k: urllib.request wants bytes not str
request = Request(self.url, request.encode(), HTTPheaders)
with contextlib.closing(urlopen(request)) as response:
# py3k: urlopen returns bytes not str
response_ = response.read()
# urllib2.urlopen returns an addinfourl instance, which supports
# a limited subset of file methods. Copy response to a StringIO
# so that we can use tell() and seek().
source = StringIO()
if isinstance(response_, str):
source.write(response_)
else:
response_ = response_.decode('utf-8')
source.write(response_)
# After writing, rewind to the beginning.
source.seek(0)
self.response = source
return source