#FTPconfsettings.py
import os import sys BASE_DIR=os.path.dirname(os.path.dirname(__file__)) BASE_CLIENT=os.path.join(BASE_DIR,"db","client") BASE_SERVER=os.path.join(BASE_DIR,"db","server") BASE_USE=os.path.join(BASE_DIR,'db','user') BASE_admin=os.path.join(BASE_DIR,'db','admin') sys.path.append(BASE_DIR)
FTPinhead.py
import json import os import struct from conf.settings import * def recv_head(client): head_len=client.recv(4) if not head_len: raise Exception("对方已下线") lens= struct.unpack("i",head_len)[0] # print(lens) rep= json.loads(client.recv(lens)) return rep def send_rep(client,rep): jdata=json.dumps(rep).encode("utf-8") # print(jdata) client.send(struct.pack('i',len(jdata))) client.send(jdata) # 判断服务端文件 def choice(path): name=os.path.split(path)[1] # print(name) if name in os.listdir(BASE_SERVER): return True return False # 判断客户端文件 def clientfile(path): name = os.path.split(path)[1] # print(name) if name in os.listdir(BASE_CLIENT): return True return False
FTPinFTPserver.py
mport os import socket import struct import json from bin.head import * from conf.settings import * server = socket.socket() server.bind(("127.0.0.1",8081)) server.listen(5) def upload(client): # 接收json数据 rep = recv_head(client) file_path = os.path.join(BASE_SERVER, rep['filename']) datasunm = 0 while int(rep["filesize"]) >= datasunm: if choice(rep['filename']): file_size = os.path.getsize(file_path) client.send(str(file_size).encode("utf-8")) with open(file_path, "ab") as rb: client_data = client.recv(2048) rb.write(client_data) datasunm += len(client_data) print(len(client_data)) else: client.send("F".encode("utf-8")) with open(file_path, "wb") as rb: client_data = client.recv(2048) rb.write(client_data) datasunm += len(client_data) print(len(client_data)) client.close() # 下载 def download(client): # 获取文件信息 reps = recv_head(client) print(reps) # 服务端文件地址 file_path = os.path.join(BASE_SERVER, reps['filename']) # 本地文件大小 file_size = os.path.getsize(file_path) # 发送系统文件大小 dic = {"filesize": file_size} send_rep(client, dic) requ = recv_head(client) print(requ['filesize']) index = int(requ['filesize']) with open(file_path, "rb") as rf: rf.seek(index) while index <= file_size: data = rf.read(2048) client.send(data) rf.flush() index += len(data) print(index) if index == file_size: break rf.close() print("发送完成")
def recv_heads(server): while True: try: client,addr=server.accept() # 上传 upload(client) download(client) except ConnectionResetError as e: print("客户端断开")
FTPinFTPclient.py
from conf.settings import * import socket from bin.head import * client=socket.socket() client.connect(('127.0.0.1',8081)) def upload(client,size,path): file_size = client.recv(1028).decode("utf-8") # print(file_size) if file_size == "F": index = 0 print(index) else: index = int(file_size) print(index) with open(path, "rb") as rf: rf.seek(index) while index <= size: data = rf.read(2048) client.send(data) rf.flush() index += len(data) print(index) if index == size: break rf.close() print("下载完成") # 上传端 def sender(client,path): if not os.path.exists(path): print("文件不存在") return name=os.path.split(path)[1] size=os.path.getsize(path) # print(size) rep={"filename":name,"filesize": size} send_rep(client,rep) # 上传端 upload(client, size,path) # 下载端 def download(client,name): dic = {"filename": name} send_rep(client, dic) file_size=recv_head(client) # 系统文件大小 size=int(file_size['filesize']) # 存储文件地址 filepath = os.path.join(BASE_CLIENT, name) # # 如果有文件不覆写 datasunm = 0 while datasunm < size: # 如果目录内存在文件 if clientfile(filepath): filesize=os.path.getsize(filepath) dic={"filesize":filesize} send_rep(client,dic) with open(filepath,"ab") as rf: data=client.recv(2048) rf.write(data) datasunm+=len(data) print(datasunm) else: # 如果目录内文件不存在 filesize=0 dic = {"filesize": filesize} send_rep(client, dic) with open(filepath, "wb") as rf: data = client.recv(2048) rf.write(data) datasunm += len(data) print(datasunm) client.close()