Search code examples
pythonpygobjectgio

HTTP not supported when using GLib’s GIO APIs


I'm writing a GTK application that sometimes downloads files with HTTP. Since I don't want to block normal execution, I obviously can't use urllib or Requests, so I'm trying to use GIO instead. However, I'm getting a GLib.Error: g-io-error-quark: Operation not supported (15) when downloading a file over HTTP (file:// works fine).

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

The first statement (the file://) works as expected, but the second one (http://) gives the aforementioned error (full output below). In my real program, I use the async version, but the sync version is shorter and gives the same error.

(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)

Solution

  • Ensure you have the http backend for GVFS installed, and that your program has access to the D-Bus session bus which gvfsd is running on.

    The GVFS backends are normally provided in a distribution package like gvfs-backends.

    GIO does not include support for loading URIs other than file:// URIs by default. For all other URIs, it communicates with the GVFS daemon, which in turn does the actual I/O.