【问题标题】:HTTP not supported when using GLib’s GIO APIs使用 GLib 的 GIO API 时不支持 HTTP
【发布时间】:2017-08-02 13:27:05
【问题描述】:

我正在编写一个有时使用 HTTP 下载文件的 GTK 应用程序。由于我不想阻止正常执行,我显然不能使用 urllib 或 Requests,所以我尝试使用 GIO 来代替。但是,通过 HTTP 下载文件时,我收到了 GLib.Error: g-io-error-quark: Operation not supported (15)file:// 工作正常)。

from gi.repository import Gio
print(Gio.File.new_for_uri("file:///etc/profile").load_contents(None))
print(Gio.File.new_for_uri("http://example.org").load_contents(None))

第一个语句(file://)按预期工作,但第二个语句(http://)给出了上述错误(下面的完整输出)。在我的真实程序中,我使用的是异步版本,但同步版本更短,并且报同样的错误。

(True, contents=b'# /etc/profile\n\n [-snip-]')
Traceback (most recent call last):
  File "/tmp/gio.py", line 3, in <module>
    print(Gio.File.new_for_uri("http://example.org").load_contents(None))
GLib.Error: g-io-error-quark: Operation not supported (15)

【问题讨论】:

    标签: python pygobject gio


    【解决方案1】:

    确保您已安装 GVFS 的 http 后端,并且您的程序可以访问运行 gvfsd 的 D-Bus 会话总线。

    GVFS 后端通常在分发包中提供,例如 gvfs-backends

    默认情况下,GIO 不支持加载 file:// URI 以外的 URI。对于所有其他 URI,它与 GVFS 守护进程通信,而 GVFS 守护进程又执行实际的 I/O。

    【讨论】:

    • 好像是 Arch 上的 gvfs
    • 在尝试启动应用程序以显示 https: 或 file: URI 时,可能来自 gtk.show_uri() 的相同错误消息。对于在解决该问题时在这里找到方法的任何其他人,请使用 gio mime x-scheme-handler/https firefox.desktopgio mime inode/directory file-manager.desktop 之类的命令将应用程序注册为相关 mime 类型的处理程序。 (当然,相应的 .desktop 文件必须存在于适当的位置。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多