day10博客
Posted Bruce.yin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day10博客相关的知识,希望对你有一定的参考价值。
服务端: #!/usr/bin/env python # -*- coding: utf-8 -*- # import socketserver # bufsize = 1024 # host = ‘127.0.0.1‘ # port = 8100 # address = (host,port) # # class MYtcphandler(socketserver.BaseRequestHandler): # def handle(self): # while True: # data=self.request.recv(bufsize) # if not data:break # self.request.sendall(data.upper()) # if __name__ == ‘__main__‘: # sever=socketserver.ThreadingTCPServer((address),MYtcphandler) # sever.serve_forever() import socketserver,os,socket class MYtcphandler(socketserver.BaseRequestHandler): def handle(self): while True: client_data=self.request.recv(bufsize).strip() if not client_data: break client_data=client_data.split(‘|‘) if hasattr(self,client_data[0]): func=getattr(self,client_data[0]) func(client_data) def filetrans(self,msg): print(‘filetrans‘,msg) if msg[1] == ‘get‘: print(‘client wants to download file‘,msg[2]) if os.path.isfile(msg[2]): file_size=os.path.getsize(msg[2]) res="ready|%s" %file_size else: res="file not exit" sen_conirmation="filetrans|get|%s" %res self.request.send(sen_conirmation) feedback=self.request.recv(100) if feedback == ‘filetrans|get|recv_ready‘: f=open(msg[2],‘rb‘) send_size=0 while not file_size==file_size: if file_size-send_size > 1024: data=f.read(1024) send_size+=1024 else: data=f.read(file_size-send_size) send_size+=(file_size-send_size) self.request.send(data) print(file_size,send_size) else: print(‘##############send file:%s done‘%msg[2]) elif msg[1] == ‘put‘: print(‘client wants to download file‘,msg[2]) #sendmessage feedback = self.request.recv(100) if feedback == ‘filetrans|put|recv_ready‘: file_size = int(feedback.split()[-1]) self.request.send("recv_ready") recv_size = 0 put_file_name = msg[2] f = open(os.path.basename(put_file_name), ‘wb‘) print(‘#########‘,put_file_name) while not file_size == recv_size: if file_size - recv_size > bufsize: data = self.request.recv(bufsize) recv_size += len(data) else: data = self.request.recv(file_size - recv_size) recv_size += (file_size - recv_size) # 接收就写入 f.write(data) print(file_size,recv_size) print(‘put %sdone‘,put_file_name) # put_file_name=msg[2] # if os.path.isfile(msg[2]): # print(‘pass‘) # put_size = os.path.getsize(msg[2]) # put_conirmation = "filetrans|put|%s" % put_size # else: # print(‘faile‘) #put_size=os.path.getsize(msg[2]) #put_conirmation="filetrans|put|%s" %put_size if __name__ == ‘__main__‘: bufsize = 1024 host = ‘127.0.0.1‘ port = 8090 address = (host,port) server=socketserver.ThreadingTCPServer((address),MYtcphandler) server.serve_forever() 客户端: #!/usr/bin/env python # -*- coding: utf-8 -*- import socketserver,os,socket class ftpclient(object): def __init__(self,address): self.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.sock.connect((address)) def start(self): self.useractive() def useractive(self): while True: user_input=input(‘>>:‘).strip() if len(user_input)==0:continue user_input=user_input.split() if hasattr(self,user_input[0]): func=getattr(self,user_input[0]) func(user_input) else: print(‘wrong cmd usage‘) def get(self,msg): print(‘##get func###‘,msg) if len(msg)==2: file_name=msg[1] client_data=‘filetrans|get|%s‘ % file_name self.sock.send(client_data) feedback=self.sock.recv(100) print(‘server confirm msg‘,feedback) if feedback.startswith(‘filetrans|get|ready‘): file_size=int(feedback.split(‘|‘)[-1]) self.sock.send(‘filetrans|get|recv_ready‘) recv_size=0 f=open(‘client_recv/%s‘ %os.path.basename(file_name),‘wb‘) print(‘new file_name‘,file_name) while not file_size==recv_size: if file_size-recv_size>bufsize: data=self.sock.recv(bufsize) recv_size+=len(data) else: data=self.sock.recv(file_size-recv_size) recv_size+=(file_size-recv_size) f.write(data) print(file_size,recv_size) else: print(‘###########recv file:%s#################‘%file_name) f.close() print(feedback) else: print(‘wrong cmd usage‘) def put(self,msg): print(‘##put func###‘, msg) if len(msg) == 2: file_name = msg[1] client_data = ‘filetrans|put|%s‘ %file_name self.sock.send(client_data.encode(‘utf-8‘)) feedback = self.sock.recv(100) if os.path.isfile(file_name): print(‘pass‘) file_size=os.path.getsize(file_name) res = "ready %s" %file_size print(‘########‘,res) self.sock.send(res) if feedback == ‘recv_ready‘: f=open(file_name,‘wb‘) send_size = 0 while not file_size == send_size: if file_size - send_size > 1024: data = f.read(1024) send_size += 1024 else: data = f.read(file_size - send_size) send_size += (file_size - send_size) print(file_size,send_size) self.sock.send(data) else: print(‘##########send file:%s done############‘ %file_name) f.close() else: print(‘faile‘) res="file doesn‘s exist" print(‘##########‘,res) self.sock.send(res) def ls(self): pass def cd(self): pass def delete(self): pass if __name__ == ‘__main__‘: bufsize = 1024 host = ‘127.0.0.1‘ port = 8090 address = (host,port) p=ftpclient(address) p.start()
以上是关于day10博客的主要内容,如果未能解决你的问题,请参考以下文章