#Python#Xftp需要花錢注冊(cè)怎么辦?用Python做個(gè)簡單的Xftp

先提一下用的的第三方模塊paramiko,做自動(dòng)化運(yùn)維用的比較多的一個(gè)模塊,我也是初次接觸它,網(wǎng)上有N多關(guān)于這個(gè)模塊的教程,用paramiko做出xftp的功能的示例也有,所以在這我就用另一種形式來實(shí)現(xiàn)這個(gè)功能——GUI,當(dāng)然也是參考其他教程簡單學(xué)習(xí)paramiko這個(gè)模塊的。用到的另外一個(gè)模塊是tkinter,用的不太熟,摸索著總算把功能實(shí)現(xiàn)了。
上代碼:

# -*- coding: utf-8 -*-
import os
import subprocess
import tkinter as tk
try:
    import paramiko
except ImportError:
    subprocess.call('pip install paramiko', shell=True)


class TkFunction:

    def __init__(self, window):
        self.window = window
        self.window.title('SFTP')
        self.window.geometry('400x300')
        tk.Label(self.window, text='IP: ').place(x=40, y=40)
        self.t_ip = tk.Entry(window)
        self.t_ip.place(x=140, y=40)

        tk.Label(self.window, text='Username: ').place(x=40, y=70)
        self.u_name = tk.Entry(window)
        self.u_name.place(x=140, y=70)

        tk.Label(self.window, text='Password: ').place(x=40, y=100)
        self.p_word = tk.Entry(window)
        self.p_word.place(x=140, y=100)

        tk.Label(self.window, text='FileName: ').place(x=40, y=130)
        self.f_name = tk.Entry(window)
        self.f_name.place(x=140, y=130)

        tk.Label(self.window, text='From: ').place(x=40, y=170)
        tk.Label(self.window, text='To: ').place(x=40, y=200)

        self.from_path = tk.Entry(self.window)
        self.from_path.place(x=140, y=170)

        self.to_path = tk.Entry(window)
        self.to_path.place(x=140, y=200)

        tk.Button(self.window, text='Put', command=self.put).place(x=160, y=230)
        tk.Button(self.window, text='Get', command=self.get).place(x=210, y=230)

    def connect(self):
        transport = paramiko.Transport((self.t_ip.get(), 22))
        transport.connect(username=self.u_name.get(), password=self.p_word.get())
        s_trans = paramiko.SFTPClient.from_transport(transport)
        return s_trans

    def put(self):
        filename = str(self.f_name.get())
        f_path = str(self.from_path.get())
        to_path = str(self.to_path.get())
        fm = os.path.join(f_path, filename)
        if to_path[-1] == '/':
            to = to_path + filename
        else:
            to = to_path + r'/' + filename
        s_trans = self.connect()
        s_trans.put(fm, to)
        s_trans.close()

    def get(self):
        filename = str(self.f_name.get())
        f_path = str(self.from_path.get())
        to_path = str(self.to_path.get())
        to = os.path.join(to_path, filename)
        if f_path[-1] == '/':
            fm = f_path + filename
        else:
            fm = f_path + r'/' + filename
        s_trans = self.connect()
        s_trans.get(fm, to)
        s_trans.close()


if __name__ == '__main__':
    window = tk.Tk()
    TkFunction(window)
    window.mainloop()

做出來的GUI界面是這個(gè)樣子的:

SFTP

Put和Get兩個(gè)按鈕對(duì)應(yīng)著類里面put和get兩個(gè)函數(shù)。
編輯完后把后綴改為pyw格式,運(yùn)行為不帶命令行的GUI界面。
上面代碼有不妥之處請(qǐng)大神們指出~ 個(gè)人覺得init里的tkinter部分太臃腫,還需更改以更好的形式來表達(dá)。


。。。在編這篇簡書時(shí)發(fā)現(xiàn)代碼里有個(gè)小Bug,明天跟組內(nèi)更新下。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容