How to asynchronous GET with python/GI



Hi

I have been struggling to get a working example of an async soup-based HTTP client in python. I finally succeeded in making this work, but it doesn't feel simple. Is there a simpler way to just get the content into a python variable ?

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import gi
gi.require_version('Soup', '2.4')
from gi.repository import Soup, GLib, Gio


class HttpClient:
    def __init__(self):
        self.session = Soup.Session()

    def get(self, uri):
        print("Opening %s" % uri)
        message = Soup.Message.new("GET", uri)
        self.session.send_async(message, None, self.on_finished, message)

    def on_finished(self, session, result, message):
        input_stream = session.send_finish(result)
        status_code = message.status_code
        print(status_code)
        if input_stream:
            data_input_stream = Gio.DataInputStream.new(input_stream)
            lines = list()
            while True:
                line, length = data_input_stream.read_line_utf8()
                if line is None:
                    print("Finished")
                    break
                else:
                    lines.append(line)
            page = "".join(lines)
            return page


if __name__ == '__main__':
    c = HttpClient()
    ml = GLib.MainLoop()
    GLib.idle_add(c.get, "https://www.google.com")
    ml.run()


Also, i noticed that it fails to decode when fetching https://www.google.com; how should such cases be handled ?

Traceback (most recent call last):
  File "test_soup.py", line 29, in on_finished
    line, length = data_input_stream.read_line_utf8()
gi.repository.GLib.Error: g_convert_error: Invalid byte sequence in conversion input (1)

Thanks



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]