Socket_FTP

1. md5加密回顾:

import hashlib

m=hashlib.md5() #创建md5对象
m.update(b'abcd') #生成加密串
m.update(b'efg')
print(m.hexdigest()) #打印加密串
m2=hashlib.md5()
m2.update(b'abcdefg')
print(m2.hexdigest())

 

7ac66c0f148de9519b8bd264312c4d64
7ac66c0f148de9519b8bd264312c4d64

 

 

FTP-Server
1.读取文件名 
2.检测文件是否存在
3.打开文件
4.检测文件大小
5.发送文件大小给客户端
6.等待客户端确定,防止粘包
7.开始边读边发数据
8.发送md5
9.关闭文件

 2. FTP入门版:

服务器端:

import socket,os,time,hashlib

server=socket.socket()
server.bind(('localhost',9999))
server.listen()
while True:
    conn,addr=server.accept()
    print("new connection is :",addr)
    while True:
        data=conn.recv(1024)
        if not data:
            print("客户端已经断开")
            break
        cmd,filename=data.decode().split()  #分割get hash.py,把get赋值给cmd,把hash.py赋值给filename
        print(filename)
        if os.path.isfile(filename): #判断是否是文件
            f=open(filename,'rb')    #以二进制,读模式打开文件
            m=hashlib.md5() #创建md5对象
            file_size=os.stat(filename).st_size #获取文件大小
            conn.send(str(file_size).encode())  #给客户端发送文件大小
            conn.recv(1024) #等待客户端的确认
            for line in f:               
                conn.send(line)          
            f.close()

server.close()

 客户端:

import socket

client=socket.socket()
client.connect(('localhost',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())
        received_size=0
        filename=cmd.split()[1]
        f=open(filename+'.new','wb') #以二进制打开一个文件,只用于写入。
        while received_size<file_total_size:
            data=client.recv(1024)
            received_size=received_size+len(data)
            f.write(data)
            print(file_total_size,received_size)
        else:
            print('file recv done')
            f.close()


client.close()

 客户端的输入及运行结果:

>>:get hash.py
server response: b'211'
211 211
file recv done
>>:

 服务器端的运行结果:

new connection is : ('127.0.0.1', 65194)
hash.py

 3. FTP+MD5验证

服务器端:

import socket,os,time,hashlib

server=socket.socket()
server.bind(('localhost',9999))
server.listen()
while True:
    conn,addr=server.accept()
    print("new connection is :",addr)
    while True:
        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() #创建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())
            f.close()
            conn.send(m.hexdigest().encode('utf-8')) #send md5

server.close()

 客户端:

import socket,hashlib
import hashlib

client=socket.socket()
client.connect(('localhost',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())
        received_size=0
        filename=cmd.split()[1]
        f=open(filename+'.new','wb')
        m=hashlib.md5()

        while received_size<file_total_size:
            data=client.recv(1024)
            received_size=received_size+len(data)
            f.write(data)
            m.update(data)
            #print(file_total_size,received_size)
        else:
            new_file_md5=m.hexdigest()
            print('file recv done',received_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()

 服务器端运行结果:

new connection is : ('127.0.0.1', 65466)
hash.py
file md5 5dbf115019480585de4d39cd175184db

 客户端运行结果:

>>:get hash.py
server response: b'211'
file recv done 211 211
server file md5: b'5dbf115019480585de4d39cd175184db'
client file md5: 5dbf115019480585de4d39cd175184db

 4.FTP+MD5验证+粘包处理:

服务器端:

import socket,os,time,hashlib

server=socket.socket()
server.bind(('localhost',9999))
server.listen()
while True:
    conn,addr=server.accept()
    print("new connection is :",addr)
    while True:
        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() #创建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())
            f.close()
            conn.send(m.hexdigest().encode('utf-8')) #send md5

server.close()

 客户端:

import socket,hashlib
import hashlib

client=socket.socket()
client.connect(('localhost',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())
        received_size=0
        filename=cmd.split()[1]
        f=open(filename+'.new','wb')
        m=hashlib.md5()

        while received_size<file_total_size:
            if file_total_size-received_size>1024: #要收不止一次
                size=1024
            else: #最后一次了,剩多少收多少
                size=file_total_size-received_size
                print("last receive:",size)
            data=client.recv(size)
            received_size=received_size+len(data)
            f.write(data)
            m.update(data)
            #print(file_total_size,received_size)
        else:
            new_file_md5=m.hexdigest()
            print('file recv done',received_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()

 服务器端的输出:

new connection is : ('127.0.0.1', 56460)
hash.py
file md5 5dbf115019480585de4d39cd175184db

 客户端的输出:

>>:get hash.py
server response: b'211'
last receive: 211
last receive: 2
file recv done 211 211
server file md5: b'5dbf115019480585de4d39cd175184db'
client file md5: 5dbf115019480585de4d39cd175184db

 

posted on 2017-08-08 10:44  momo8238  阅读(172)  评论(0编辑  收藏  举报