2020年2月,疫情严重被困家中,自学python写了一个简单地ftp上传文件功能
代码很low,随手写写
server端:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Author : Effort
# @Time : 2020/2/20 0020 15:52
import socket
import json
import struct
import hashlib
server = socket.socket()
server.bind(('127.0.0.1',8888))
server.listen(5)
while True:
conn,addr=server.accept()
while True:
"""
data = conn.recv(1024) # 阻塞
监听的套接字对象conn断开连接:
针对window:异常报错,使用try捕获异常
针对linux:接收一个空的data。进行判断如果data==0,则break,回到第一层while,等待客户端再次发送数据
"""
try:
data = conn.recv(4)
if data =="exit":
break
#先获取客户端传来的pack压缩包然后进行unpack解压
data_json_unpack = struct.unpack("i",data)[0]
print(data_json_unpack)
#根据unpack获取的元组中的数字长度,再去内存缓冲区取data_json_unpack长度的数据,即客户端第二次send过来的json字符串
data_json_unpack_recv = conn.recv(data_json_unpack)
file_info = json.loads(data_json_unpack_recv)
print('file_info',file_info)
#返回客户端状态码代表接收成功
conn.send(b'200')
#进行接收处理客户端上传的文件
action = file_info.get('action')
filename = file_info.get('filename')
data_fize = file_info.get('data_fize')
client_md5 = file_info.get('client_md5')
print(action,filename,data_fize,client_md5)
if action=="put":
obj = hashlib.md5(b'yanglongyue')
with open("put/"+filename,'wb') as f:
data_fize_len = 0
while data_fize_len < data_fize:
data_len = conn.recv(1024)
data_fize_len+=len(data_len)
f.write(data_len)
obj.update(data_len)
print("总大小%s,已接收%s"%(data_fize,data_fize_len))
server_md5 = obj.hexdigest()
print(server_md5)
print("已传送完毕,下一步进行文件一致性比对")
if server_md5 == client_md5:
print("文件经过比对完全一致!!!")
conn.send(b'203')
else:
print("经过比对服务端与客户端上传的文件不一致!!!")
elif action == "down":
pass
else:
print("请正确输入命令参数")
except Exception as e:
print(e)
break
conn.close()
client端:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Author : Effort
# @Time : 2020/2/20 0020 15:54
import socket
import os
import json
import struct
import hashlib
sock = socket.socket()
sock.connect(('127.0.0.1',8888))
while True:
cmd = input("请输入要操作的命令/put-上传/down-下载>>>>:")
if cmd == "exit":
sock.send(cmd.encode('utf-8'))
break
try:
action,filename= cmd.strip().split(" ")
print(action,filename)
#获取文件的大小
data_fize = os.path.getsize(filename)
#获取文件的MD5值以便进行文件上传后一致性的校验
obj = hashlib.md5(b'yanglongyue')#加盐
with open(filename, 'rb') as f:
for i in f:
obj.update(i)
client_md5 = obj.hexdigest()
print("client_md5:"+client_md5)
data_dict = {'action':action,'filename':filename,'data_fize':data_fize,"client_md5":client_md5}
#转换成json字符串进行传递
data_dict_json = json.dumps(data_dict)
#为了防止发生黏包,将data_dict_json的大小使用struct压包传到服务端,再传送数据过去
data_dict_json_pack = struct.pack("i",len(data_dict_json))
sock.send(data_dict_json_pack)
sock.send(data_dict_json.encode('utf-8'))
#接收服务端返回的状态码
code= sock.recv(1024).decode('utf-8')
print(code)
if code =='200':
print("连接服务端成功,进行上传")
with open(filename,'rb') as f:
for i in f:
sock.send(i)
code_md5 = sock.recv(1024).decode('utf-8')
if code_md5 =="203":
print("文件经过比对完全一致!!!,客户端文件上传完毕!!!")
else:
print("文件经过比对不一致!!!,客户端文件上传有缺失!!!")
else:
print("服务器异常")
except Exception as e:
print(e)
声明:师从老男孩太白金星,不对代码做任何保证,如有问题请自携兵刃直奔沙河