在Python中用(X)处理关闭终端窗口?

时间:2018-09-02 06:20:23

标签: python

我有一个文件传输应用程序(sftp),它使用一些gui功能(Tk)来让用户从文件浏览器中选择文件,发送文件没有问题(使用pysftp),但是接收时需要我安装其他系统挂载,以便用户浏览远程计算机的文件,我使用(子进程)和挂载命令(sshfs)进行了操作,当用户单击ctrl + c时,我能够卸载和删除远程计算机的挂载点,但是如果用户单击终端窗口的(X),我将无法执行以下操作:

import pysftp
import subprocess
import os
import time

CrntDir = os.getcwd()
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
HST = "172.16.72.36"
usrnm = "username"
passwd = "P@ssw0rd!"
port = 22
mount = CrntDir+"/.mountPoint/"
sftp = pysftp.Connection(HST, username=usrnm, 
                                    password=passwd, port=port, 
                                    cnopts=cnopts)
try:
    os.makedirs(mount)
    subprocess.call('echo {2} | sshfs -o StrictHostKeychecking=no -o ro -o password_stdin {0}@{1}: {3}'.format(
                                                                    usrnm,HST,passwd,mount), shell=True)
    print("Ctrl+c to end") #<------ if u close the terminal window here the ssh connection remains and the created directory along with the mount
    time.sleep(200)

except (KeyboardInterrupt, EOFError):
    subprocess.call(["fusermount","-u", mount])
    os.rmdir(mount)
    print("Closing connection.")

0 个答案:

没有答案