2

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

你怎么做到这一点?

4

2 回答 2

4

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

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)
于 2018-05-06T17:54:55.460 回答
3

这是一个部分异步版本,可以改进(尤其是在将结果放入 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()
于 2018-10-22T18:14:47.380 回答