这篇文章主要介绍了python基于FTP实现文件传输相关功能代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本实例有文件传输相关功能,包括:文件校验、进度条打印、断点续传
客户端示例:
import socket
import json
import os
import hashlib
CODE = {
'1001':'重新上传文件'
}
def file_md5(file_path):
obj = open(file_path,'rb')
m = hashlib.md5()
for line in obj:
m.update(line)
obj.close()
return m.hexdigest()
def jdt(size,total_size):
'''
显示进度条
'''
val = int(size/total_size * 100)
print('\r%s%%|%s' %(val,'#'*val,),end='')
def send_file(exist_size,file_total_size):
'''
发送文件
:param exist_size: 开始读取字节的位置
:param file_total_size: 文件大小
:return:
'''
f = open(file_path, 'rb')
f.seek(exist_size)
send_size = exist_size
while send_size < file_total_size:
data = f.read(1024)
sk.sendall(data)
send_size += len(data)
jdt(send_size,file_total_size)
f.close()
print('上传成功')
def upload(file_path):
'''文件上传(含断点)'''
file_path = '111.jpg'
file_md5_val = file_md5(file_path)
file_name = os.path.basename(file_path)
file_size = os.stat(file_path).st_size
cmd_dict = {'cmd': 'upload', 'file_name': file_name, 'size': file_size, 'md5': file_md5_val}
upload_cmd_bytes = json.dumps(cmd_dict).encode('utf8')
sk.sendall(upload_cmd_bytes)
# 2.等待服务端相应
response = json.loads(sk.recv(8096).decode('utf8'))
if response['code'] == 1001:
send_file(0,file_size)
else: # 断点续传
exist_size = response('size')
send_file(exist_size,file_size)
sk = socket.socket()
sk.connect(('127.0.0.1',8001))
while 1:
#upload|文件路径
user_input = input('命令>>>')
# 1、自定义协议{'cmd':'upload','file_name':'...'}
cmd,file_path = user_input.split('|',maxsplit=1)
if cmd == 'upload':
upload(file_path)
elif cmd == 'download':
pass
服务端示例:
import socketserver
import json
import os
import shutil
CODE = {
'1001':'重新上传文件'
}
def upload(cmd_dict,conn,username):
'''服务端完成上传文件(含断点续传)'''
# 2.获取文件信息
file_md5 = cmd_dict['md5']
file_name = cmd_dict['file_name']
file_md5_path = os.path.join('home', username, file_md5)
file_name_path = os.path.join('home', username, file_name)
upload_file_size = cmd_dict['size']
# 3.判断文件是否存在
exist = os.path.exists(file_md5_path)
if not exist: # 不续传
# 3.1.1通知可以开始上传,已经准备好
response = {'code': 1001}
conn.sendall(json.dumps(response).encode('utf8'))
# 3.1.2接收上传的文件内容
f = open(file_md5_path, 'wb')
recv_size = 0
while recv_size < upload_file_size:
data = conn.recv(1024)
f.write(data) # 写到内存
f.flush() # 刷到硬盘
recv_size += len(data)
f.close()
# 3.1.3改文件名
shutil.move(file_md5_path, file_name_path)
else: # 存在就续传
# 3.2续传 + 大小
exist_size = os.stat(file_md5_path).st_size
response = {'code': 1002, 'size': exist_size}
conn.sendall(json.dumps(response).encode('utf8'))
f = open(file_md5_path, 'ab')
recv_size = exist_size
while recv_size < upload_file_size:
data = conn.recv(1024)
f.write(data) # 写到内存
f.flush() # 刷到硬盘
recv_size += len(data)
f.close()
# 3.1.3改文件名
shutil.move(file_md5_path, file_name_path)
class NBServer(socketserver.BaseRequestHandler):
def handle(self):
'''self.request是请求客户端的socket对象'''
#1.接受命令
upload_cmd_bytes = self.request.recv(8096)
cmd_dict = json.loads(upload_cmd_bytes.decode('utf8'))
if cmd_dict['cmd'] == 'upload':
upload(cmd_dict,self.request,'zhangzijian')
elif cmd_dict['cmd'] == 'download':
pass
if __name__ == '__main__':
server = socketserver.ThreadingTCPServer(('127.0.0.1',8001),NBServer)
server.serve_forever()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。