Pyhton:FTP客户端实现

import tkinter as tk  # 使用Tkinter前需要先导入
import tkinter.messagebox
import ftplib

# 第1步,实例化object,建立窗口window
window = tk.Tk()
# 第2步,给窗口的可视化起名字
window.title('Ftp Client')
# 第3步,设定窗口的大小(长 * 宽)
window.geometry('400x400')  # 这里的乘是小x

# 第4步,加载 FTP image
window_pic = 'pic.png'
ftp_pic = 'ftp_pic.png'
canvas = tk.Canvas(window, width=400, height=245, bg='white')
image_file = tk.PhotoImage(file= window_pic)
image = canvas.create_image(200, 0, anchor='n', image=image_file)
canvas.pack(side='top')

# 第5步,用户信息
tk.Label(window, text='IP address:', font=('Arial', 14)).place(x=10, y=250)
tk.Label(window, text='User name:', font=('Arial', 14)).place(x=10, y=290)
tk.Label(window, text='Password:', font=('Arial', 14)).place(x=10, y=330)

# 第6步,用户登录输入框entry
    # IP
var_usr_ip = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_ip, font=('Arial', 14))
entry_usr_name.place(x=120, y=255)
    # 用户名
var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name, font=('Arial', 14))
entry_usr_name.place(x=120, y=295)
    # 用户密码
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, font=('Arial', 14), show='*')
entry_usr_pwd.place(x=120, y=335)

# 第8步,定义用户登录功能
def usr_login():
    # 这两行代码就是获取用户输入的usr_name,usr_pwd和usr_ip
    usr_name = var_usr_name.get()
    usr_pwd = var_usr_pwd.get()
    ip_port = var_usr_ip.get().split(':')
    usr_ip = ip_port[0]
    usr_port = ip_port[1]
    # 登录 FTP 服务,验证 IP、用户名和密码的正确性
    try:
        ftp = ftplib.FTP()
        ftp.connect(usr_ip,int(usr_port))
        try:
            ftp.login(usr_name,usr_pwd)
            usr_ftp(ftp, usr_ip)
            tkinter.messagebox.showinfo('Ftp connection succeeded')
        except:
            tkinter.messagebox.showerror('Error,', 'username or password is incorrect')
    except:
        tkinter.messagebox.showerror('Error,', 'please check the IP you entered')
    usr_ftp(usr_ip)

# 第9步,定义用户登录功能
# def usr_ftp(usr_ip):
def usr_ftp(ftp,usr_ip):
    window.destroy()
    # 定义上传功能
    def upload():
        def upload_to_Ftp():
            try:
                fname = file_path.get()
                new_name = new_filename.get()
                fd = open(fname, 'rb')
                # 以二进制的形式上传
                ftp.storbinary("STOR %s" % new_name, fd)
                fd.close()
                tkinter.messagebox.showinfo(title='Hi',message='Upload success!!!')
            except:
                tkinter.messagebox.showerror(title='Hi',message='Upload failed !!!')

        upload_window = tk.Toplevel(ftp_window)
        upload_window.geometry('300x110')
        upload_window.title('Upload')
        # 定义上传路径
        file_path = tk.StringVar()
        # 输入上传文件的完整路径,必要时可以用绝对路径
        # # 比如这里我要传入 G:\\FTP_sharing\\1.txt
        file_path.set(r'G:\\Folder_name\\File.txt1111')
        tk.Label(upload_window,text='File path:').place(x=10,y=10)
        entry_new_name = tk.Entry(upload_window,textvariable=file_path,width=25)
        entry_new_name.place(x=80,y=10)
        # 重新定义文件名
        new_filename = tk.StringVar()
        tk.Label(upload_window, text='File name:').place(x=10, y=50)
        # width 指定文本框的长度
        entry_new_name = tk.Entry(upload_window, textvariable=new_filename,width=25)
        entry_new_name.place(x=80, y=50)

        # upload按钮
        btn_upload = tk.Button(upload_window, text='Upload', command=upload_to_Ftp)
        btn_upload.place(x=130, y=76)


    def download():
        def down_to_Ftp():
            try:
                fname = new_filename.get()
                path = storage_path.get()
                # 构建下载文件的存储路径,这里用的是D盘的FTP_file 文件夹下,可以自行设置
                new_path = path + fname
                # 以写模式在本地打开文件
                file_handle = open(new_path, 'wb').write
                # 接收服务器上文件并写入本地文件
                ftp.retrbinary("RETR %s" % fname, file_handle)
                # ftp.quit()  # 退出ftp
                tkinter.messagebox.showinfo(title='Hi',message='Download success !!!')
            except:
                tkinter.messagebox.showerror(title='Hi',message='Download failed !!!')

        download_window = tk.Toplevel(ftp_window)
        download_window.geometry('300x110')
        download_window.title('Download')
        # 输入需要下载的文件名
        new_filename = tk.StringVar()
        tk.Label(download_window, text='File name:').place(x=10, y=10)
        # width 指定文本框的长度
        entry_new_name = tk.Entry(download_window, textvariable=new_filename, width=25)
        entry_new_name.place(x=100, y=10)
        # 输入文件存储路径
        storage_path = tk.StringVar()
        # # 构建下载文件的存储路径,这里用的是D盘的FTP_file 文件夹下,可以自行设置
        storage_path.set(r'D:\\FTP_file\\')
        tk.Label(download_window,text='Storage path:').place(x=10,y=50)
        entry_new_name = tk.Entry(download_window,textvariable=storage_path,width=25)
        entry_new_name.place(x=100,y=50)

        # download按钮
        btn_upload = tk.Button(download_window, text='Download', command=down_to_Ftp)
        btn_upload.place(x=130, y=76)

    def help_ftp():
        tkinter.messagebox.showinfo(title='Help', message='This is the help page' )

    def about_ftp():
        tkinter.messagebox.showinfo(title='About Ftp', message='版权所有,翻版必究!' )

    # 定义功能窗口
    ftp_window = tk.Tk()
    ftp_window.title('IP:%s' % usr_ip)
    ftp_window.geometry('300x200')
    ftp_canvas = tk.Canvas(ftp_window, width=300, height=200, bg='white')
    ftp_image_file = tk.PhotoImage(file=ftp_pic)
    # 这里的参数是 image 注意别瞎起名字
    ftp_image = ftp_canvas.create_image(150, 0, anchor='n', image=ftp_image_file)
    ftp_canvas.pack(side='top')

    menubar = tk.Menu(ftp_window)
    filemenu = tk.Menu(menubar,tearoff=0)
    menubar.add_cascade(label='File',menu=filemenu)
    filemenu.add_command(label='Upload',command=upload)
    filemenu.add_separator()
    filemenu.add_command(label='Download',command=download)

    helpmean = tk.Menu(menubar,tearoff=0)
    menubar.add_cascade(label='Help',menu = helpmean)
    helpmean.add_command(label='Help',command=help_ftp)
    helpmean.add_separator()
    helpmean.add_command(label='About',command=about_ftp)
    ftp_window.config(menu=menubar)
    ftp_window.mainloop()


# 第7步,login and sign up 按钮
btn_login = tk.Button(window, text='Login', command=usr_login)
btn_login.place(x=120, y=365)
btn_exit_up = tk.Button(window, text='Exit up', command=window.quit)
btn_exit_up.place(x=200, y=365)

# 第10步,主窗口循环显示
window.mainloop()

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南淮北安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值