Search code examples
pythontkintersshfs

Combining Tkinter and SSHFS in order to use tkFileDialog on remote server


I would like to provide a simple GUI for selecting files on the remote server for my application. The Tkinter widgets do not support this inherently, so I am looking for some workarounds. My local machine is a Mac, and the remote machine is CentOS 6.9.

The type of process I need is this: user selects a remote file. A python script is used to extract information from this file, and writes this to a new file in the same directory.

A much simpler thing would perhaps be for the user to provide the filename via a filename = input("Path to file: "), but that requires the user to get the path from the remote server first. Ideally I would like the application to be self-consistent.

Could the SSHFS be used in combination with the tkFileDialog module? The user would walk the mounted file system, select the file, actions would be performed, and the new file would be stored in the mounted file system and automatically transferred to the remote system. Possible?

Or, crazy idea: I would recreate the remote $HOME file structure locally, but just creating a temporary directory and a bunch of correctly named empty files. The user can walk this "fake" file system, select the file of interest, and then the application can use this path to retrieve the file, perform the actions, and send back the new file... Okay, perhaps not the most elegant solution....


Solution

  • I tested sshfs on notebook (Linux Mint) to access remote folder on Raspberry Pi (Raspbian based on Debian) and it can mount remote folder as local folder and then I can use Python and other programs to work with remote files.

    To mount it:

    mkdir local_pi
    sshfs pi@192.168.1.100:/home/pi/ local_pi
    
    # here ask for password of user `pi` 
    

    It mounts remote folder /home/pi/ as local folder local_pi and it block access to /home/ so it can restrict access only to some folder and its subfolders.


    I tested in Python and it worked:

    • tkFileDialog.askopenfilename() to select file
    • standard open(), read(), write() to read and write file
    • os.makedirs(dirname) to create folder
    • os.listdir(dirname) to get filenames in folder