1

I am writing following decorator for Stackexchange API:

    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),
    ])

As response I get stream of bytes:

b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x04\x00\xb5\\\x0b\x93\xa3F\x92\xfe+u\xe...

The question is who encode response? What is the correct Unicode to decode this? I checked utf-8, utf-16, zlib.decompress, etc.. it doesn't help.

2

1 Answer 1

2

The relevant portion of the answer linked to by Daniel Roseman is this:

if response.info().get('Content-Encoding') == 'gzip':
    buf = StringIO( response.read())
    f = gzip.GzipFile(fileobj=buf)
    data = f.read()

In other words, the encoding should be available as response.info().get('Content-Encoding')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.