socketserver实现多用户同时在线ftp

Posted 与君同悦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了socketserver实现多用户同时在线ftp相关的知识,希望对你有一定的参考价值。

socketserver服务端

import socketserver, hashlib, os

class MyTCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        #打印客户端地址
        print("{} wrote:".format(self.client_address))
        while True:
            self.data = self.request.recv(1024).decode("utf-8", "ignore")
            cmd, filename = self.data.split()
            if cmd == "get":
                if os.path.isfile(filename):
                    file_size = os.stat(filename).st_size
                    self.request.send(str(file_size).encode())
                    #接收客户端大小确认信息
                    size_ack = self.request.recv(1024).decode("utf-8", "ignore")
                    #打印数据大小确认,开始发送数据
                    print(size_ack)
                    f = open(filename, "rb")
                    m = hashlib.md5()
                    for line in f:
                        m.update(line)
                        self.request.send(line)
                    self.request.send(m.hexdigest().encode())
                    print("send done!")
                    f.close()
            elif cmd == "put":
                self.request.send("服务端正在请求文件大小信息...".encode())
                file_size = self.request.recv(1024).decode("utf-8", "ignore")
                self.request.send("上传文件大小为{}".format(file_size).encode())
                f = open(filename+".put", "wb")
                m = hashlib.md5()
                recv_size = 0
                recv_data = b""
                while recv_size < int(file_size):    
                    if int(file_size) - recv_size > 1024:
                        size = 1024
                    else:
                        size = int(file_size) - recv_size
                    recv_data = self.request.recv(size)
                    f.write(recv_data)
                    m.update(recv_data)
                    recv_size += len(recv_data)
                else:
                    recv_md5 = self.request.recv(1024).decode("utf-8", "ignore")
                    print("before:", recv_md5)
                    print("after:", m.hexdigest())
                    print("recvieved done!")
                    f.close()
                


if __name__ == "__main__":
    HOST, PORT = "localhost", 9999
    #执行自己创建的请求处理类
    server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler)
    #处理多个请求,永远执行
    server.serve_forever()

  

socketserver客户端1

#ftp 客户端
import socket, hashlib, os

client = socket.socket()

client.connect(("localhost", 9999))
while True:
    data = input(">>:").strip()
    if not data:continue
    filename = data.split()[1]
    client.send(data.encode())
    if data.startswith("get"):
        #接收文件大小信息
        file_total_size = client.recv(1024).decode("utf-8", "ignore")
        #发送文件大小接收确认
        client.send("客户端已得知文件大小为{0}".format(file_total_size).encode())
        f = open(filename+".get", "wb")
        m = hashlib.md5()
        recv_size = 0
        recv_data = b""
        while recv_size < int(file_total_size):
            #此处为了防止MD5和最后一次发送的数据粘包
            if int(file_total_size) - recv_size > 1024:
                size = 1024
            else:
                #计算出最后一次剩余的数据大小,只接收此大小的数据
                size = int(file_total_size) - recv_size
            recv_data = client.recv(size)
            f.write(recv_data)
            m.update(recv_data)
            recv_size += len(recv_data)
            print("{0}/{1}".format(str(recv_size), file_total_size))
        else:
            print("数据已经接受完。")
            print("开始验证md5")
            print("client md5:", m.hexdigest())
            f.close()
        print("server md5:", client.recv(1024).decode("utf-8", "ignore"))
    elif data.startswith("put"):
        if os.path.isfile(filename):
            file_total_size = os.stat(filename).st_size
            file_size_request = client.recv(1024).decode("utf-8", "ignore")
            print(file_size_request)
            #发送数据大小到服务端
            client.send(str(file_total_size).encode())
            #服务器已经接收到文件大小信息,可以开始传送
            print(client.recv(1024).decode("utf-8", "ignore"))
            f = open(filename, "rb")
            m = hashlib.md5()
            for line in f:
                client.send(line)
                m.update(line)
            client.send(m.hexdigest().encode())
            print("upload done!")
            f.close()
        else:
            pass
client.close()

  

socketserver客户端2

#ftp 客户端
import socket, hashlib, os

client = socket.socket()

client.connect(("localhost", 9999))
while True:
    data = input(">>:").strip()
    if not data:continue
    filename = data.split()[1]
    client.send(data.encode())
    if data.startswith("get"):
        #接收文件大小信息
        file_total_size = client.recv(1024).decode("utf-8", "ignore")
        #发送文件大小接收确认
        client.send("客户端已得知文件大小为{0}".format(file_total_size).encode())
        f = open(filename+".get", "wb")
        m = hashlib.md5()
        recv_size = 0
        recv_data = b""
        while recv_size < int(file_total_size):
            #此处为了防止MD5和最后一次发送的数据粘包
            if int(file_total_size) - recv_size > 1024:
                size = 1024
            else:
                #计算出最后一次剩余的数据大小,只接收此大小的数据
                size = int(file_total_size) - recv_size
            recv_data = client.recv(size)
            f.write(recv_data)
            m.update(recv_data)
            recv_size += len(recv_data)
            print("{0}/{1}".format(str(recv_size), file_total_size))
        else:
            print("数据已经接受完。")
            print("开始验证md5")
            print("client md5:", m.hexdigest())
            f.close()
        print("server md5:", client.recv(1024).decode("utf-8", "ignore"))
    elif data.startswith("put"):
        if os.path.isfile(filename):
            file_total_size = os.stat(filename).st_size
            file_size_request = client.recv(1024).decode("utf-8", "ignore")
            print(file_size_request)
            #发送数据大小到服务端
            client.send(str(file_total_size).encode())
            #服务器已经接收到文件大小信息,可以开始传送
            print(client.recv(1024).decode("utf-8", "ignore"))
            f = open(filename, "rb")
            m = hashlib.md5()
            for line in f:
                client.send(line)
                m.update(line)
            client.send(m.hexdigest().encode())
            print("upload done!")
            f.close()
        else:
            pass
client.close()

以上是关于socketserver实现多用户同时在线ftp的主要内容,如果未能解决你的问题,请参考以下文章

socketserver实现FTP

socketserver版FTP

基于socketserver开发多线程ftp

day8--socketserver作业

Python-Socketserver实现FTP,文件上传下载

支持多用户在线的Ftp程序