1 # 编辑者:闫龙 2 import socket,json,struct 3 class MySocket: 4 with open("FtpServiceConfig","r",encoding="utf8") as f: 5 Config = json.loads(f.read()) 6 AFType= eval("socket."+Config["AFType"]) 7 KIND=eval("socket."+Config["KIND"]) 8 IPaddress= Config["IPaddress"] 9 Port=Config["Port"] 10 MaxLinks=Config["MaxLinks"] 11 def __init__(self): 12 self.SockObj = socket.socket(self.AFType,self.KIND) 13 self.SockObj.bind((self.IPaddress,self.Port)) 14 self.SockObj.listen(self.MaxLinks) 15 def Starting(self): 16 self.Clients,self.IpInfo=self.SockObj.accept() 17 def GetHeadLen(self): 18 return struct.unpack("i",self.Clients.recv(4))[0] 19 def GetHead(self,Len:int): 20 return json.loads(self.Clients.recv(Len).decode("utf8")) 21 def GetDatas(self,Len:int): 22 return self.Clients.recv(Len) 23 def SendDatas(self,SendWhat): 24 self.Clients.send(SendWhat.encode("utf8")) 25 def ClientBye(self): 26 self.Clients.close() 27 def ByeBye(self): 28 self.SockObj.close()
1 # 编辑者:闫龙 2 import socket,json,struct 3 class MyClient: 4 with open("FtpClientConfig","r",encoding="utf8") as f: 5 Config = json.loads(f.read()) 6 AFType = eval("socket."+Config["AFType"]) 7 KIND = eval("socket."+Config["KIND"]) 8 IPaddress = Config["IPaddress"] 9 Port = Config["Port"] 10 def __init__(self): 11 self.SockObj = socket.socket(self.AFType,self.KIND) 12 def ConnetService(self): 13 self.SockObj.connect((self.IPaddress, self.Port)) 14 def GetInfo(self,Len:int): 15 return self.SockObj.recv(Len) 16 def SendInfo(self,SendWhat): 17 self.SockObj.send(SendWhat) 18 def SendHeadLen(self,HeadLen): 19 HeadLen = json.dumps(HeadLen).encode("utf8") 20 self.SockObj.send(struct.pack("i",len(HeadLen))) 21 def SendHead(self,Head): 22 self.SockObj.send(json.dumps(Head).encode("utf8"))
1 # 编辑者:闫龙 2 import FtpServerClass as fsc 3 import time 4 FtpService = fsc.MySocket() 5 FtpService.Starting() 6 while True: 7 try: 8 HeadLen = FtpService.GetHeadLen() 9 Head = FtpService.GetHead(HeadLen) 10 f = open(Head["FileName"],"wb") 11 RecvSize = 0 12 RecvData = b"" 13 i = 1 14 while RecvSize < Head["DataSize"]: 15 getdata = FtpService.GetDatas(1024) 16 RecvData += getdata 17 RecvSize = len(RecvData) 18 f.write(getdata) 19 print("%s/%s"%(RecvSize,Head["DataSize"])) 20 time.sleep(0.0005) 21 except Exception as e: 22 print(e) 23 break
1 # 编辑者:闫龙 2 import FtpClientClass as fcc 3 import os 4 FtpClient = fcc.MyClient() 5 FtpClient.ConnetService() 6 while True: 7 choice = input(">>>:") 8 if not choice : continue 9 FileSize = os.path.getsize(choice) 10 ClientHead = {"DataSize":FileSize,"FileName":os.path.basename(choice)} 11 FtpClient.SendHeadLen(ClientHead) 12 FtpClient.SendHead(ClientHead) 13 SendSize = 0 14 with open(choice,"rb") as f: 15 for i in f: 16 FtpClient.SendInfo(i) 17 SendSize += len(i) 18 print(SendSize) 19 print("上传完成")
1 {"AFType":"AF_INET","KIND":"SOCK_STREAM","IPaddress":"127.0.0.1","Port":9527,"MaxLinks":5}
1 {"AFType":"AF_INET","KIND":"SOCK_STREAM","IPaddress":"127.0.0.1","Port":9527}
熬到现在头脑已经不清醒了,基本实现上传功能,未完待续
BUG太多,明天再说!
睡觉!