码迷,mamicode.com
首页 > 其他好文 > 详细

ftp_socket

时间:2017-11-07 23:00:34      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:input   line   ==   lin   print   ...   soc   new   import   

ftp_socket_client

import socket,os,hashlib
client = socket.socket()
client.connect(("192.168.171.129",9999))

while True:
    cmd = input(">>:").strip()
    if len(cmd) == 0:continue
    if cmd.startswith("get"):
        client.send(cmd.encode())
        server_response = client.recv(1024)
        print("server response:",server_response)
        client.send(b"ready to recv file")
        file_total_size = int(server_response.decode())
        recived_size= 0
        filename = cmd.split()[1]
        f = open(filename + "new","wb")
        m = hashlib.md5()
        while recived_size < file_total_size:
            if file_total_size - recived_size > 1024:
                size = 1024
            else:
                size = file_total_size - recived_size
                print("lsat size:",size)
            data = client.recv(size)
            recived_size += len(data)
            m.update(data)
            f.write(data)
        else:
            new_file_md5 = m.hexdigest()
            print("file recv done",recived_size,file_total_size)
            f.close()
        server_file_md5 = client.recv(1024)
        print("server file md5:",server_file_md5)
        print("client file md5:",new_file_md5)
client.close()

ftp_socket_server

import socket,os,hashlib
server = socket.socket()
server.bind(("0.0.0.0",9999))
server.listen(5)

while True:
    print("start waiting for calling...")
    conn,addr = server.accept()
    print("conn:",addr)
    while True:
        print("waiting for new calling...")
        data = conn.recv(1024)
        if not data:
            print("服务器已断开!")
            break
        cmd,filename = data.decode().split()
        print(filename)
        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)
            print("file md5",m.hexdigest())
            conn.send(m.hexdigest().encode())
            f.close()
            print("send done")
server.close()

ftp_socket

标签:input   line   ==   lin   print   ...   soc   new   import   

原文地址:http://www.cnblogs.com/whz0215/p/7801515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!