首页 > 代码库 > FTP服务器之上传
FTP服务器之上传
1.SERVER端
__author__ = ‘alex‘#coding:utf-8import socketimport osimport sysimport jsonip_port = (‘127.0.0.1‘,8009)sk = socket.socket()sk.bind(ip_port)sk.listen(5)# print (os.path.dirname(__file__))print ("服务端启动...")while True: conn,addr = sk.accept() file_info_bytes = conn.recv(1024) file_info_str = str(file_info_bytes,"gbk") file_info = json.loads(file_info_str) client_file_name = file_info.get(‘file_name‘) client_file_size = file_info.get(‘file_size‘) print (file_info) if os.path.exists(client_file_name): print ("文件存在!") server_file_size = os.stat(client_file_name).st_size print (server_file_size) conn.sendall(bytes(str(server_file_size),"gbk")) server_file = open(client_file_name,‘ab‘) while server_file_size < client_file_size: try: data=conn.recv(1024) if not data: raise Exception except Exception: break server_file.write(data) server_file_size += len(data) server_file.close() else: print ("文件不存在!") server_file_size = 0 print (server_file_size) conn.sendall(bytes(str(server_file_size),"gbk")) # has_received = 0 server_file = open(client_file_name,‘wb‘) while server_file_size < client_file_size: try: data=conn.recv(1024) if not data: raise Exception except Exception: break server_file.write(data) server_file_size += len(data) server_file.close()
2.CLIENT端
__author__ = ‘alex‘#coding:utf-8import socketimport osimport sysimport jsondef process_bar(num,total): percent = float(num)/float(total) per_int = int(percent*100) # print (percent,per_int) # print (">"*per_int + "%d%%" %(per_int+1)) temp = "\r%d%%" %(per_int) sys.stdout.write(temp) sys.stdout.flush()FILE_PATH = "D:\\ftp\\Rio.explorer.rmvb"FILE_NAME = FILE_PATH.split(‘\\‘)[-1]ip_port = (‘127.0.0.1‘,8009)sk = socket.socket()sk.connect(ip_port)file_info = {}print ("客户端启动...")while True: if os.path.exists(FILE_PATH): file_size = os.stat(FILE_PATH).st_size file_info[‘file_name‘] = FILE_NAME file_info[‘file_size‘] = file_size file_info_json = json.dumps(file_info) sk.sendall(bytes(file_info_json,"gbk")) server_file_size = sk.recv(1024) server_file_size_str = str(server_file_size,"gbk") server_file_size_int = int(server_file_size_str) print (server_file_size_str) if int(server_file_size_str) != 0: inp = input("服务器上存在此文件,是否续传?") if inp == ‘y‘: file_obj = open(FILE_PATH,‘rb‘) file_obj.seek(server_file_size_int) # print (file_seek) print ("开始续传...") while server_file_size_int < file_size: file_read = file_obj.read(1024) sk.sendall(file_read) server_file_size_int += len(file_read) process_bar(server_file_size_int,file_size) file_obj.close() else: break else: print ("服务器端没有这个文件,要从开头开始发送...") file_obj = open(FILE_PATH,‘rb‘) while server_file_size_int < file_size: file_read = file_obj.read(1024) sk.sendall(file_read) server_file_size_int += len(file_read) process_bar(server_file_size_int,file_size) file_obj.close() else: print ("本地文件不存在!")sk.close()
FTP服务器之上传
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。