Stackexchange API 编码
Stackexchange API encoding
我正在为 Stackexchange API 编写以下装饰器:
class WhosebugHandler(tornado.web.RequestHandler):
def get(self, look_up_pattern):
url = "https://api.stackexchange.com/2.2/search?order=desc&sort=votes&intitle=%s&site=Whosebug"
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/(.*)", WhosebugHandler),
])
作为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 等。它没有帮助。
丹尼尔罗斯曼链接到的答案的相关部分是这样的:
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO( response.read())
f = gzip.GzipFile(fileobj=buf)
data = f.read()
换句话说,编码应该是 response.info().get('Content-Encoding')
我正在为 Stackexchange API 编写以下装饰器:
class WhosebugHandler(tornado.web.RequestHandler):
def get(self, look_up_pattern):
url = "https://api.stackexchange.com/2.2/search?order=desc&sort=votes&intitle=%s&site=Whosebug"
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/(.*)", WhosebugHandler),
])
作为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 等。它没有帮助。
丹尼尔罗斯曼链接到的答案的相关部分是这样的:
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO( response.read())
f = gzip.GzipFile(fileobj=buf)
data = f.read()
换句话说,编码应该是 response.info().get('Content-Encoding')