如何使用 libSoup 和 GI 发出 HTTP GET 请求?

How do you make a HTTP GET request with libSoup and GI?

我想使用 python 中的 GObject-introspection (gi) 使用 libSoup GLib 库向 https://httpbin.org/get 发出一个简单的 HTTP GET 请求。

你是怎么做到的?

这是一个发出请求并打印出响应的示例:

import sys

import gi
gi.require_version('Soup', '2.4')
from gi.repository import Gio, Soup

session = Soup.Session()
uri = Soup.URI.new('https://httpbin.org/get')
request = session.request_http_uri('GET', uri)
stream = request.send(cancellable=None)
out = Gio.UnixOutputStream(fd=sys.stdout.fileno())
out.splice(stream, Gio.OutputStreamSpliceFlags.CLOSE_SOURCE, cancellable=None)

这是一个部分异步的版本,可以改进(尤其是将结果放入 python 变量)。

#!/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)
            print(page)


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