首页 > 代码库 > FTP

FTP

class Ftp_server(socketserver.BaseRequestHandler):
    code="utf-8"
    def handle(self):
        while True:
            recv_head=self.request.recv(4) #收报头的长度
            recv_data_len=struct.unpack("i",recv_head)
            print(recv_data_len)
            recv_head_data=self.request.recv(recv_data_len[0])  #收报头信息
            print(recv_head_data)
            data_size=json.loads(recv_head_data)["data_size"]

            #---------------------------------------------------------------------------------------------
            recv_size=0
            with open("s",wb) as f:
                while recv_size < data_size:         #开写数据
                    data=http://www.mamicode.com/self.request.recv(1024)
                    f.write(data)
                    recv_size+=len(data)
            self.request.send("传输完成".encode("utf-8"))
    def put(self):
        print("alex草你妈")

if __name__ == __main__:
    obj=socketserver.ThreadingTCPServer(("127.0.0.1",8080),Ftp_server)
    obj.serve_forever()

 

 

 

 

 

 

 

class Client():
    def __init__(self,Addr_server):
        self.addr_server=Addr_server
        self.socket = socket.socket()
        self.client_connect()

    def client_connect(self):
        self.socket.connect(self.addr_server)

    def run(self):
        while True:
            name_inpu = input("请输入用户名: ")
            flie_inpu = input("请输入要传输的文件路径:  ").strip()
            filesize = os.path.getsize(flie_inpu)
            dic = {"name": name_inpu, "file": flie_inpu, "data_size": filesize}
            bytes_dic=json.dumps(dic).encode("utf-8")
            len_dic = len(bytes_dic)
            struct_len = struct.pack("i", len_dic)
            self.socket.send(struct_len)
            self.socket.send(bytes_dic)
            with open(‘c‘,‘rb‘) as f:
                    for i in f:
                        self.socket.send(i)
            print(self.socket.recv(1023).decode("utf-8"))

c=Client(("127.0.0.1",8080))
c.run()

  

 

FTP