当前位置: 首页 > 图灵资讯 > 行业资讯> python怎么传送文件

python怎么传送文件

来源:图灵python
时间: 2025-01-22 15:40:43

在python中传输文件的方法:1、将报头转换成字符串【json.dumps】, 然后包装字符串的长度;2、发送报头长度和报头内容,然后放置真实内容;3、减压报头长度,得到头部信息的大小,然后接收头部信息,反序列化

思路:

# 首先,将报头转换成字符串(json.dumps), 然后包装字符串的长度 # 发送报头长度,发送报头内容,最后放置真实内容 # 报头内容包括文件名、文件信息、报头 # 接收时:首先接收4个字节的报头长度, # 在接收头部信息时,减压报头长度,得到头部信息的大小, 反序列化(json.loads) # 最后,接收真实文件

服务端

#!/usr/bin/envpython
#-*-coding:utf-8-*-
#@File:文件传输-服务端.py
#@Software:PyCharmfromsocketimport*importstructimportjsonimportostcp_server=socket(AF_INET,SOCK_STREAM)ip_port=(('127.0.0.1',8080))buffsize=1024
#重复使用端口
tcp_server.setsockopt(SOL_SOCKET,SO_REUSEPORT,1)tcp_server.bind(ip_port)tcp_server.listen(5)print('还没有人链接')
whileTrue:
'''链接循环'''
conn,addr=tcp_server.accept()
print('链接人的信息:',addr)
whileTrue:
ifnotconn:
print('客户端链接中断')
break
'''通信循环'''
filemesg=input('请输入要传输的文件名和后缀>>>').strip()
filesize_bytes=os.path.getsize(filemesg)#获取文件的大小,字节
filename='new'+filemesg
dirc={
'filename':filename,
'filesize_bytes':filesize_bytes,
}
head_info=json.dumps(dirc)#将字典转换为字符串
head_info_len=struct.pack('i',len(head_info))
#包装字符串的长度
#首先,将报头转换成字符串(json.dumps),然后包装字符串的长度
#发送报头长度,发送报头内容,最后发送真实内容
#报头内容包括文件名、文件信息、报头
#接收时:先接收四个字节的报头长度,
#减压报头长度,获取头部信息的大小,接收头部信息,反序列化(json.loads)
#最后,接收真实文件
conn.send(head_info_len)#发送head_info的长度
conn.send(head_info.encode('utf-8'))
#发送真正的信息
withopen(filemesg,'rb')asf:
data=f.read()
conn.sendall(data)
print('成功发送')

客户端

#!/usr/bin/envpython
#-*-coding:utf-8-*-
#@File:文件传输_客户端.py
#@Software:PyCharmfromsocketimport*
importstruct
importjson
importos
importsys
importtime
from进度条importproces_bar

tcp_client=socket(AF_INET,SOCK_STREAM)
ip_port=(('127.0.0.1',8080))
buffsize=1024
tcp_client.connect_ex(ip_port)
print('等待链接服务端')
whileTrue:

head_struct=tcp_client.recv(4)#接收报头的长度,
ifhead_struct:
print('连接到服务端,等待接收数据')

head_len=struct.unpack('i',head_struct)[0]#分析报头字符串的大小
data=tcp_client.recv(head_len)#接收长度为head_len报头内容的信息(包括文件大小,文件名的内容)
head_dir=json.loads(data.decode('utf-8'))

filesize_b=head_dir['filesize_bytes']

filename=head_dir['filename']
#接受真实的文件内容

recv_len=0

recv_mesg=b''

old=time.time()

f=open(filename,'wb')
whilerecv_len<filesize_b:

percent=recv_len/filesize_b


process_bar(percent)
iffilesize_b-recv_len>buffsize:


recv_mesg=tcp_client.recv(buffsize)

f.write(recv_mesg)

recv_len+=len(recv_mesg)
else:

recv_mesg=tcp_client.recv(filesize_b-recv_len)

recv_len+=len(recv_mesg)

f.write(recv_mesg)
print(recv_len,filesize_b)

now=time.time()

stamp=int(now-old)
print('总共用时%ds'%stamp)

f.close()

进度条

#!/usr/bin/envpython
#-*-coding:utf-8-*-
#@File:进度条.py
#@Software:PyCharm
importsys
importtime
defprocess_bar(precent,width=50):use_num=int(precent*width)space_num=int(width-use_num)precent=precent*100
#第一个梯形显示与最后一个梯形显示相同,中间两个正确,但是在python2中报错了#print('[%s%s]%d%%'%(use_num*'#',space_num*'',precent))#print('[%s%s]%d%%'%(use_num*'#',space_num*'',precent),end='\r')print('[%s%s]%d%%'%(use_num*'#',space_num*'',precent),file=sys.stdout,flush=True,end='\r')
#print('[%s%s]%d%%'%(use_num*'#',space_num*'',precent),file=sys.stdout,flush=True)
#foriinrange(21):
#precent=i/20
#process_bar(precent)
#time.sleep(0.2)

1.jpg

推荐课程:Python高级视频教程

上一篇:

python如何打印变量

下一篇:

返回列表