目录结构
FTP_server/bin/ftp_server.pyimport os,sysPATH=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))sys.path.append(PATH)# print(PATH)# print(sys.path)# print(os.path.dirname(os.path.abspath(__file__)))# print(os.path.abspath(__file__))from core import mainif __name__ == '__main__': main.ArgvHandler()复制代码
FTP_server/conf/settings.py
IP='127.0.0.1'PORT=8080复制代码
FTP_server/core/main.py
#解析命令行的命令import optparseimport socketserverfrom conf import settings#这个文件不是启动文件from core import serverclass ArgvHandler(): def __init__(self): self.op=optparse.OptionParser() # self.op.add_option('-s','--server',dest='server') # self.op.add_option('-P','--port',dest='port') options,args=self.op.parse_args() self.verify_args(options,args) # print(options,args) # print(options.server)#ip地址 # print(options.port)#端口 def verify_args(self,options,args): cmd=args[0] if hasattr(self,cmd): func =getattr(self,cmd) func() def start(self): print('the server is working....') s = socketserver.ThreadingTCPServer((settings.IP,settings.PORT),server.ServerHandle) s.serve_forever() def help(self): pass ``` ***FTP_server/core/server.py*** ``` import socketserverimport jsonclass ServerHandle(socketserver.BaseRequestHandler): def handle(self): while 1 : data = self.request.recv(1024).strip() data = json.loads(data.decode('utf8')) if data.get('action'): if hasattr(self,data.get('action')): func = getattr(self,data.get('action')) func(**data) else: print('error') else: print('error') def auth(self,**data): print('data:',data) def put(self,**data): pass复制代码
FTP_client/ftp_client.py
import optparseimport socketimport jsonimport configparserclass ClientHandler(): def __init__(self): self.op = optparse.OptionParser() self.op.add_option('-s','--server',dest='server') self.op.add_option('-P','--p',dest='port') self.op.add_option('-u', '--username', dest='username') self.op.add_option('-p', '--password', dest='password') self.options,self.args = self.op.parse_args() #对端口和ip地址进行安全校验 self.verify_args(self.options,self.args) #连接服务端 self.make_connection() def verify_args(self,options,args): server = options.server port=options.port # username=options.username # password=options.password if int(port)>0 and int(port)<65535: return True else: exit('the port is in 0-65535') def make_connection(self): self.sock=socket.socket() self.sock.connect((self.options.server,int(self.options.port))) #和服务端进行交互 def interactive(self): self.authenticate() def authenticate(self): if self.options.username is None or self.options.password is None: username = input('username:') password = input('password:') return self.getauth_result(username,password) return self.getauth_result(self.options.username,self.options.password) def respones(self): data = self.sock.recv(1024).decode('utf8') data = json.loads(data) return data def getauth_result(self,user,pwd): data = { 'action':'auth', 'username':user, 'password':pwd } self.sock.send(json.dumps(data).encode('utf8')) response=self.respones() print(response)ch = ClientHandler()#交互ch.interactive()复制代码
运行结果