首页 > 代码库 > sock_server_ftp.py

sock_server_ftp.py

#

import socket
import os,hashlib
server = socket.socket()
server.bind((localhost,6971)) #绑定要监听地址和端口  如果是监听所有
server.listen(5) #监听

print("我要开始等电话了")
while True:
    conn, addr = server.accept()  # 等电话打进来      conn:服务端生成的实例,接受新实例,addr:客户端的地址
    # conn就是客户端连过来而在服务器端为其生成的一个连接实例
    print(conn, addr)
    print("电话来了")
    # count = 0
    while True:
        data = conn.recv(1024)  #8192
        print("recv:",data)
        if not data:
            print("client has lost...")
            break
        cmd,filename = data.decode().split()
        if os.path.isfile(filename):
            f = open(filename,"rb")
            m = hashlib.md5()
        file_size = os.stat(filename).st_size
        conn.send(str(file_size).encode())
        conn.recv(1024)
        for line in f:
            m.update(line)
            conn.send(line)
        f.close()
        conn.send(m.hexdigest().encode())
    print("Send done")


server.close()

 

sock_server_ftp.py