close

使用wxPython 模組視窗程式設計-簡單ftp client 程式
讀者可以這個為基礎,變化成較複雜的ftp client 程式,例如加入
wx.Gauge元件以傳輸較大的檔案。

# -*- coding: big5 -*-
from ftplib import FTP, all_errors
import wx
import os

class MyStatusBar(wx.StatusBar):

    def __init__(self, parent):
        super(MyStatusBar, self).__init__(parent)

        self.SetFieldsCount(2)
        self.SetStatusText('Welcome to ftp client', 0)
        self.SetStatusWidths([-1, 120])

        self.Bind(wx.EVT_SIZE, self.OnSize)

    def OnSize(self, e):
        e.Skip()

class MyFtp(wx.Frame):

    def __init__(self, *args, **kw):
        super(MyFtp, self).__init__(*args, **kw)

        self.InitUI()

    def InitUI(self):

        wx.StaticText(self, label='Ftp site', pos=(10, 20))             #設定 label 資料
        wx.StaticText(self, label='Login', pos=(10, 60))
        wx.StaticText(self, label='Password', pos=(10, 100))
        wx.StaticText(self, label='本機檔案', pos=(10, 140))
        wx.StaticText(self, label='遠端目錄', pos=(10, 180))

        self.ftpsite = wx.TextCtrl(self, pos=(100, 15),size=(160, -1))      #設定輸入 TXT 資料
        self.login = wx.TextCtrl(self,  pos=(100, 55),size=(160, -1))
        self.password = wx.TextCtrl(self, pos=(100, 95),size=(160, -1), style=wx.TE_PASSWORD)
        self.local =  wx.TextCtrl(self,  pos=(100, 135),size=(160, -1))
        self.remote = wx.TextCtrl(self,  pos=(100, 175),size=(160, -1))

        self.ftpsite.SetValue("XX.XX.XX.XX")                                           #設定預設值
        self.login.SetValue("XXXXX")
        self.password.SetValue("XXX")
        self.local.SetValue("c:/tmp/XXX.txt")
        self.remote.SetValue("/XXX/XXX/")

        self.ftp = None

        con = wx.Button(self, label='連線', pos=(10, 250))                       #設定連線按鈕
        discon = wx.Button(self, label='斷線', pos=(120, 250))
        transfer = wx.Button(self, label='上傳資料', pos=(230, 250))

        self.Bind(wx.EVT_BUTTON, self.OnConnect, con)                         #設定連線bind
        self.Bind(wx.EVT_BUTTON, self.OnDisConnect, discon)
        transfer.Bind(wx.EVT_BUTTON, self.ToggleTrans)

        self.sb = MyStatusBar(self)
        self.SetStatusBar(self.sb)

        self.SetSize((350,380))
        self.SetTitle('FTP Client')
        self.Centre()
        self.Show()

    def OnConnect(self, e):                                                                      #連線事件動作
        if not self.ftp:

            ftpsite = self.ftpsite.GetValue()
            login = self.login.GetValue()
            password = self.password.GetValue()
            self.localFile =  self.local.GetValue()
            self.remote = self.remote.GetValue()

            try:
                self.ftp = FTP(ftpsite)
                var = self.ftp.login(login, password)
                self.sb.SetStatusText('使用者連線成功')

            except AttributeError:

                self.sb.SetStatusText('連線失敗(主機或帳號或密碼錯誤)')
                self.ftp = None

            except all_errors, err:

                self.sb.SetStatusText(str(err))
                self.ftp = None

    def OnDisConnect(self, e):        #離線事件動作

        if self.ftp:

            self.ftp.quit()
            self.ftp = None

            self.sb.SetStatusText('使用者已離線')

    def ToggleTrans(self, e):         #檔案上傳事件動作
        self.ftp.cwd(self.remote)
        upFile = self.localFile
        baseName = os.path.basename(upFile)
        dirName = os.path.dirname(upFile)
        os.chdir(dirName)
        upFile = baseName
        print(dirName+'/'+upFile)
        # ----- asc mode -----------
        file = open(upFile, "r")
        self.ftp.storlines("STOR " + upFile , file)
        #------ binary mode ---------
        #fh = open(upFile, 'rb')
        #self.ftp.storbinary('STOR ' + "id.txt" , fh)
        #--------------------
        print("檔案上傳成功!")
        self.sb.SetStatusText('檔案上傳成功!')
        wx.MessageBox('檔案上傳成功!', '上傳結果通知',wx.OK | wx.ICON_INFORMATION)
        self.Destroy()

#---主程式----------------

def main():

    ex = wx.App()
    MyFtp(None)
    ex.MainLoop()

if __name__ == '__main__':
    main()

回主目錄

arrow
arrow

    stanley 發表在 痞客邦 留言(0) 人氣()