Python
Python入门系列
Python字典
Python语法
Python代码规范与命名规则
uWSGI和WSGI之间的关系
Python字符编码
Python正则表达式
Python异常处理
Python循环控制
Python文件读写操作
Python模块和函数
Python类
Python格式处理
Python单元测试
Pythonweb采集
python的jQuery-Ajax使用
python的日志
Python变量-字符-数字
Python数组-元祖-字典-集合
Python输入输出
Python安装部署
centos下使用yum安装pip
Python原理说明
Python服务维护
Python模块
Python常用方法
Python新闻
Python使用案例
socket实现客户端和服务端
python射击游戏
将json转换成execl
Python连接Mysql测试
python发送文件
窗口实例化
Python_小说下载脚本
python代码编写规范
本文档使用 MrDoc 发布
-
+
home page
python发送文件
## 一.简介 有需要发送文件的需求  思路: 1. 先将报头转换成字符串(json.dumps), 再将字符串的长度打包 2. 发送报头长度,发送报头内容,最后放真是内容 3. 报头内容包括文件名,文件信息,报头 4. 接收时:先接收4个字节的报头长度, 5. 将报头长度解压,得到头部信息的大小,在接收头部信息, 反序列化(json.loads) 6. 最后接收真实文件 ## 二.客户端发到服务端 ### master.py ```python #!/usr/bin/env python # -*- coding: utf-8 -*- from socket import * import struct import json import os import sys import time tcp_server = socket(AF_INET, SOCK_STREAM) ip_port = (('172.16.5.4', 7777)) buffsize = 1024 # 端口的重复利用 tcp_server.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) tcp_server.bind(ip_port) tcp_server.listen(200) while True: '''链接循环''' print("等待客户端连接...") conn, addr = tcp_server.accept() print('链接人的信息:', addr) head_struct = conn.recv(4) # 接收报头的长度, if head_struct: print('已连客户端,等待接收数据') head_len = struct.unpack('i', head_struct)[0] # 解析出报头的字符串大小 data = conn.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') while recv_len < filesize_b: percent = recv_len / filesize_b if filesize_b - recv_len > buffsize: recv_mesg = conn.recv(buffsize) f.write(recv_mesg) recv_len += len(recv_mesg) else: recv_mesg = conn.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() ``` ### slave.py ```python #!/usr/bin/env python # -*- coding: utf-8 -*- from socket import * import struct import json import os tcp_client = socket(AF_INET, SOCK_STREAM) ip_port = (('172.16.5.4', 7777)) buffsize = 1024 tcp_client.connect_ex(ip_port) filename = 'gitlab-Statistics.py' filesize_bytes = os.path.getsize(filename) # 得到文件的大小,字节 dirc = { 'filename': filename, 'filesize_bytes': filesize_bytes, } head_info = json.dumps(dirc) # 将字典转换成字符串 head_info_len = struct.pack('i', len(head_info)) # 将字符串的长度打包 tcp_client.send(head_info_len) # 发送head_info的长度 tcp_client.send(head_info.encode('utf-8')) # 发送真是信息 with open(filename, 'rb') as f: data = f.read() tcp_client.sendall(data) print('发送成功') ``` ## 三.服务端发到客户端 ## master.py ```python #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 18-5-21 下午1:59 # @Author : LK # @File : 文件传输-服务端.py # @Software: PyCharm from socket import * import struct import json import os tcp_server = socket(AF_INET, SOCK_STREAM) ip_port = (('172.16.1.46', 7777)) buffsize = 1024 # 端口的重复利用 tcp_server.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1) tcp_server.bind(ip_port) tcp_server.listen(5) print('还没有人链接') while True: '''链接循环''' conn, addr = tcp_server.accept() print('链接人的信息:', addr) while True: if not conn: print('客户端链接中断') break '''通信循环''' #filemesg = input('请输入要传送的文件名加后缀>>>').strip() filemesg = 'gitlab-Statistics.py' 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), 再将字符串的长度打包 # 发送报头长度,发送报头内容,最后放真是内容 # 报头内容包括文件名,文件信息,报头 # 接收时:先接收4个字节的报头长度, # 将报头长度解压,得到头部信息的大小,在接收头部信息, 反序列化(json.loads) # 最后接收真实文件 conn.send(head_info_len) # 发送head_info的长度 conn.send(head_info.encode('utf-8')) # 发送真是信息 with open(filemesg, 'rb') as f: data = f.read() conn.sendall(data) print('发送成功') break ``` ## slave.py ```python #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 18-5-21 下午1:59 # @Author : LK # @File : 文件传输_客户端.py # @Software: PyCharm from socket import * import struct import json import os import sys import time #from 进度条 import process_bar tcp_client = socket(AF_INET, SOCK_STREAM) ip_port = (('172.16.1.46', 7777)) buffsize = 1024 tcp_client.connect_ex(ip_port) print('等待链接服务端') while True: head_struct = tcp_client.recv(4) # 接收报头的长度, if head_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') while recv_len < filesize_b: percent = recv_len / filesize_b # process_bar(percent) if filesize_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() ```
日行一善
Dec. 15, 2020, 7:10 p.m.
Share documents
Collection documents
Last
Next
Scan wechat
Copy link
Scan your mobile phone to share
Copy link
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
QQ粉丝交流群:882382311
Markdown文件
share
link
type
password
Update password