Accessing samba shares with gio in python

混江龙づ霸主 提交于 2019-12-06 02:28:01

问题


I am trying to make a simple command line client for accessing shares via the Python bindings of gio (yes, the main requirement is to use gio).

I can see that comparing with it's predecessor gnome-vfs, it provides some means to do authentication stuff (subclassing MountOperation), and even some methods which are quite specific to samba shares, like set_domain().

But I'm stuck with this code:

import gio

fh = gio.File("smb://server_name/")

If that server needs authentication, I suppose that a call to fh.mount_enclosing_volume() is needed, as this methods takes a MountOperation as a parameter. The problem is that calling this methods does nothing, and the logical fh.enumerate_children() (to list the available shares) that comes next fails.

Anybody could provide a working example of how this would be done with gio ?


回答1:


The following appears to be the minimum code needed to mount a volume:

def mount(f):
    op = gio.MountOperation()
    op.connect('ask-password', ask_password_cb)
    f.mount_enclosing_volume(op, mount_done_cb)

def ask_password_cb(op, message, default_user, default_domain, flags):
    op.set_username(USERNAME)
    op.set_domain(DOMAIN)
    op.set_password(PASSWORD)
    op.reply(gio.MOUNT_OPERATION_HANDLED)

def mount_done_cb(obj, res):
    obj.mount_enclosing_volume_finish(res)

(Derived from gvfs-mount.)

In addition, you may need a glib.MainLoop running because GIO mount functions are asynchronous. See the gvfs-mount source code for details.



来源:https://stackoverflow.com/questions/1991206/accessing-samba-shares-with-gio-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!