python3带tkinter窗口的ftp服务器,并使用pyinstaller打包成exe
python带tkinter窗口的ftp服务器,使用python3编写,打包使用pyinstaller,命令 pyinstaller -F .\ftpserver.py
代码也可在我的github上下载
话不多说 直接贴代码:
1 from tkinter import * 2 from pyftpdlib.authorizers import DummyAuthorizer 3 from pyftpdlib.handlers import FTPHandler 4 from pyftpdlib.servers import FTPServer 5 import _thread 6 import sys 7 8 root = Tk() 9 root.title("ftpserver") 10 11 def run(): 12 _thread.start_new_thread ( ftpserver, ()) 13 14 def exitftp(): 15 sys.exit(0) 16 17 def ftpserver(): 18 19 20 #实例化虚拟用户,这是FTP验证首要条件 21 authorizer = DummyAuthorizer() 22 23 #添加用户权限和路径,括号内的参数是(用户名, 密码, 用户目录, 权限) 24 authorizer.add_user(var1.get(), var2.get(), '.', perm='elradfmw') 25 26 #添加匿名用户 只需要路径 27 authorizer.add_anonymous('.') 28 29 #初始化ftp句柄 30 handler = FTPHandler 31 handler.authorizer = authorizer 32 33 #监听ip 和 端口,因为linux里非root用户无法使用21端口,所以我使用了2121端口 34 server = FTPServer((var3.get(), var4.get()), handler) 35 36 #开始服务 37 server.serve_forever() 38 39 #下面这些是对最开始的时候创建的tk进行行列式填充 label为文本 entry为输入框 40 L1 = Label(root,text = 'UserName:').grid(column = 0,row = 0) 41 var1 = StringVar() 42 E1 = Entry(root,textvariable = var1, bd = 2).grid(column = 1,row = 0) 43 var1.set("admin") 44 user = var1.get() 45 46 L2 = Label(root,text = 'Password:').grid(column = 0,row = 1) 47 var2 = StringVar() 48 E2 = Entry(root,textvariable = var2, bd = 2).grid(column = 1,row = 1) 49 var2.set("123456") 50 password = var2.get() 51 52 L3 = Label(root,text = 'IP Address:').grid(column = 0,row = 2) 53 var3 = StringVar() 54 E3 = Entry(root,textvariable = var3, bd = 2).grid(column = 1,row = 2) 55 var3.set("0.0.0.0") 56 ipaddr = var3.get() 57 58 L4 = Label(root,text = 'PortNumber:').grid(column = 0,row = 3) 59 var4 = StringVar() 60 E4 = Entry(root,textvariable = var4, bd = 2).grid(column = 1,row = 3) 61 var4.set("2121") 62 port = var4.get() 63 64 Button1 = Button(root,text = "RunFtpserver",command=run).grid(column = 0,row = 4) 65 Button0 = Button(root,text = "Exit",command=exitftp).grid(column = 1,row = 4) 66 root.mainloop()
完成效果如下图所示:
联系我:ryan255@163.com
github:https://github.com/ryan255
github:https://github.com/ryan255