ftp_client.py
import socket
client = socket.socket()
client.connect(('localhost',8080))
while True:
cmd = input('>>>').strip()#input filename for download
if not cmd:
continue
if cmd.startswith('get'):
client.send(cmd.encode())
file_total_size = int(client.recv(1024))
print('文件的大小是',file_total_size)
client.send('客户端以准备好了'.encode())
received_size = 0
filename = cmd.split()[1]
f = open(filename,'wb')
while received_size < file_total_size:
data = client.recv(1024)
received_size += len(data)
print('已经接受大小:',received_size)
f.write(data)
else:
print('接受到的文件总大小是:',received_size)
f.close()
client.close()
ftp_server.py
import socket
import os
server = socket.socket()
server.bind(('0.0.0.0',8080))
server.listen()
while True:
conn,addr = server.accept()#wait for connect
print('the new connect is',addr)
while True:
print('等待新指令')
data = conn.recv(1024)
if not data:
print('disconenct with client ')
break
cmd,filename = data.decode().split()
print('文件名是:',filename)
if os.path.isfile(filename):
file_size = os.stat(filename).st_size
conn.send(str(file_size).encode())#send file size to client
client_ack = conn.recv(1024)
print('来自客户端的确认信息:',client_ack.decode())
f = open(filename,'rb')
for line in f:#send file
conn.send(line)
f.close()
print('send done')
server.close()
对传输文件进行md5加密检验
md5加密小代码回顾
import hashlib
m1 = hashlib.md5()
m1.update('admin'.encode())
print(m1.hexdigest())
print('*'*30)
m2 = hashlib.md5()
m2.update('adm'.encode())
m2.update('in'.encode())
print(m2.hexdigest())
ftp_client_md5.py
import socket
import hashlib
client = socket.socket()
client.connect(('localhost',8080))
while True:
cmd = input('>>>').strip()#input filename for download
if not cmd:
continue
if cmd.startswith('get'):
client.send(cmd.encode())
file_total_size = int(client.recv(1024))
print('文件的大小是',file_total_size)
client.send('客户端以准备好了'.encode())
received_size = 0
filename = cmd.split()[1]
f = open(filename,'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('最后收到文件大小:',size)
data = client.recv(size)
m.update(data)
received_size += len(data)
f.write(data)
else:
print('接受到的文件总大小是:',received_size)
f.close()
server_file_md5 = client.recv(1024)
print('server file md5:',server_file_md5.decode())
print('client file md5:',m.hexdigest())
client.close()
ftp_server_md5.py
import socket
import os
import hashlib
server = socket.socket()
server.bind(('0.0.0.0',8080))
server.listen()
while True:
conn,addr = server.accept()#wait for connect
print('the new connect is',addr)
while True:
print('等待新指令')
data = conn.recv(1024)
if not data:
print('disconenct with client ')
break
cmd,filename = data.decode().split()
print('文件名是:',filename)
if os.path.isfile(filename):
file_size = os.stat(filename).st_size
conn.send(str(file_size).encode())#send file size to client
client_ack = conn.recv(1024)
print('来自客户端的确认信息:',client_ack.decode())
m = hashlib.md5()
f = open(filename,'rb')
for line in f:#send file
conn.send(line)
m.update(line)
f.close()
print('file md5:',m.hexdigest())
conn.send(m.hexdigest().encode())
print('send done')
server.close()