下面是Stackexchange的装饰器:
class StackOverflowHandler(tornado.web.RequestHandler):
def get(self, look_up_pattern):
url = "https://api.stackexchange.com/2.2/search?order=desc&sort=votes&intitle=%s&site=stackoverflow"
with urllib.request.urlopen(url % look_up_pattern) as so_response:
response = so_response.read()
print(response)
self.write(response)
application = tornado.web.Application([
(r"/search/(.*)", StackOverflowHandler),
])作为response,我得到字节流:
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x04\x00\xb5\\\x0b\x93\xa3F\x92\xfe+u\xe...问题是谁对回应进行编码?对此进行解码的正确Unicode是什么?我查过utf-8,utf-16,zlib.decompress等等。没什么用。
发布于 2016-06-08 16:15:20
丹尼尔·罗斯曼( Daniel Roseman )的答案的相关部分如下:
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO( response.read())
f = gzip.GzipFile(fileobj=buf)
data = f.read()换句话说,编码应该以response.info().get('Content-Encoding')的形式提供。
https://stackoverflow.com/questions/37657992
复制相似问题