文件传输功能函数版本
服务端:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import struct
import json
import os
dir_share = r'D:\project\menu\ftp_file'
def get(connt,cmds):
filename = cmds[1]
# 3.返回命令结果给客户端:
# 第一步先制作报头
header_dic = {
'filename': filename,
'md5': 'adv88889992',
'file_size': os.path.getsize('%s/%s' % (dir_share, cmds[1]))
}
# 第二步 序列号header_dic
header_json = json.dumps(header_dic)
header_bytes = header_json.encode('utf-8')
# 第三步 先发送包头的长度
res = len(header_bytes)
connt.send(struct.pack('i', res))
# 第四步 再发送报头
connt.send(header_bytes)
# 第五步再发送真实数据
with open('%s\%s' % (dir_share, filename), 'rb') as f:
for line in f:
connt.send(line)
def put(coont):
# 2.下载文件
# 第一步先收报头长度
obj = coont.recv(4)
# 第二步从包头中解析出对真实数据的描述信息
header_size = struct.unpack('i', obj)[0]
# 第三步接收报头
header_bytes = coont.recv(header_size)
# 第 四步反序列化:
header_json = header_bytes.decode('utf-8')
header_dic = json.loads(header_json)
filename = header_dic['filename']
file_size = header_dic['file_size']
# 第五步 接收真实数据
recv_size = 0
with open('%s/%s' % (dir_share, filename), 'wb') as f:
while recv_size < file_size:
data = coont.recv(1024)
f.write(data)
recv_size += len(data)
print('下载进度', '%.2f%%' % (recv_size / file_size * 100))
def run():
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1) # 端口复用
phone.bind(('127.0.0.1', 8090))
phone.listen(10)
while True: # 连接循环
connt , client = phone.accept()
print('starting ....')
print(client)
while True: # 通信循环
try:
# 1.收到命令:
res = connt.recv(1024)
if not res :continue # 适用于Linux 操作系统
# 2.解析命令:
cmds = res.decode('utf-8').split() # ['get', '1.pm4']
if cmds[0].lower()=='get'and len(cmds[1]) >4:
get(connt,cmds)
elif cmds[0].lower()=='put'and len(cmds[1]) >4:
put(connt)
else:
print('命令错误')
except ConnectionResetError: # 适用于windows操作系统
break
connt.close()
phone.close()
if __name__ == '__main__':
run()
客户端:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import struct
import json
import os
down_file = r'D:\project\menu\Socket1111'
def get(phone1):
# 2.下载文件
# 第一步先收报头长度
obj = phone1.recv(4)
# 第二步从包头中解析出对真实数据的描述信息
header_size = struct.unpack('i', obj)[0]
# 第三步接收报头
header_bytes = phone1.recv(header_size)
# 第 四步反序列化:
header_json = header_bytes.decode('utf-8')
header_dic = json.loads(header_json)
filename = header_dic['filename']
file_size = header_dic['file_size']
# 第五步 接收真实数据
recv_size = 0
with open('%s/%s' % (down_file, filename), 'wb') as f:
while recv_size < file_size:
data = phone1.recv(1024)
f.write(data)
recv_size += len(data)
print('下载进度', '%.2f%%' % (recv_size / file_size * 100))
def put(connt,cmds):
filename = cmds[1]
# 3.返回命令结果给客户端:
# 第一步先制作报头
header_dic = {
'filename': filename,
'md5': 'adv88889992',
'file_size': os.path.getsize('%s\%s' % (down_file, cmds[1]))
}
# 第二步 序列号header_dic
header_json = json.dumps(header_dic)
header_bytes = header_json.encode('utf-8')
# 第三步 先发送包头的长度
res = len(header_bytes)
connt.send(struct.pack('i', res))
# 第四步 再发送报头
connt.send(header_bytes)
# 第五步再发送真实数据
with open('%s\%s' % (down_file, filename), 'rb') as f:
for line in f:
connt.send(line)
def run():
phone1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
phone1.connect(('127.0.0.1',8090))
while True:
# 1.发命令
int_cmd = input('>>>:').strip()
cmds = int_cmd.split()
if cmds[0].lower()=='get' and len(cmds[1])>4:
phone1.send(int_cmd.encode('utf-8'))
get(phone1)
elif cmds[0].lower()=='put' and len(cmds[1])>4:
phone1.send(int_cmd.encode('utf-8'))
put(phone1,cmds)
else:
print('命令错误')
phone1.close()
if __name__ == '__main__':
run()