python使用tcp传输图片数据
作者:zebra_zzh 时间:2023-08-09 06:33:48
本文实例为大家分享了python使用tcp传输图片数据的具体代码,供大家参考,具体内容如下
数据包格式如下
客户端:
import socket
import sys
HOST,PORT = "172.18.0.3",19984
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
#包头标志
arrBuf = bytearray(b'\xff\xaa\xff\xaa')
#以二进制方式读取图片
picData = open('1.jpg', 'rb')
picBytes = picData.read()
#图片大小
picSize = len(picBytes)
#数据体长度 = guid大小(固定) + 图片大小
datalen = 64 + picSize
#组合数据包
arrBuf += bytearray(datalen.to_bytes(4, byteorder='little'))
guid = 23458283482894382928948
arrBuf += bytearray(guid.to_bytes(64, byteorder='little'))
arrBuf += picBytes
sock.sendall(arrBuf)
sock.close()
if __name__ == '__main__':
main()
服务端:
import socketserver
import os
import sys
import time
import threading
ip_port=("172.18.0.3",19984)
class MyServer(socketserver.BaseRequestHandler):
def handle(self):
print("conn is :",self.request) # conn
print("addr is :",self.client_address) # addr
while True:
try:
self.str = self.request.recv(8)
data = bytearray(self.str)
headIndex = data.find(b'\xff\xaa\xff\xaa')
print(headIndex)
if headIndex == 0:
allLen = int.from_bytes(data[headIndex+4:headIndex+8], byteorder='little')
print("len is ", allLen)
curSize = 0
allData = b''
while curSize < allLen:
data = self.request.recv(1024)
allData += data
curSize += len(data)
print("recv data len is ", len(allData))
#接收到的数据,前64字节是guid,后面的是图片数据
arrGuid = allData[0:64]
#去除guid末尾的0
tail = arrGuid.find(b'\x00')
arrGuid = arrGuid[0:tail]
strGuid = str(int.from_bytes(arrGuid, byteorder = 'little')) #for test
print("-------------request guid is ", strGuid)
imgData = allData[64:]
strImgFile = "2.jpg"
print("img file name is ", strImgFile)
#将图片数据保存到本地文件
with open(strImgFile, 'wb') as f:
f.write(imgData)
f.close()
break
except Exception as e:
print(e)
break
if __name__ == "__main__":
s = socketserver.ThreadingTCPServer(ip_port, MyServer)
print("start listen")
s.serve_forever()
来源:https://blog.csdn.net/qq_24282081/article/details/106498871
标签:python,tcp,传输数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python使用QRCode模块生成二维码实例详解
2022-07-04 16:50:44
Python中创建二维数组
2023-11-24 15:14:54
![](https://img.aspxhome.com/file/2023/8/131508_0s.png)
Docker安装MySQL8.0的实现方法
2024-01-23 06:52:06
![](https://img.aspxhome.com/file/2023/1/128001_0s.jpg)
perl产生随机数实现代码
2023-04-14 05:30:10
pytorch SENet实现案例
2021-03-27 05:14:23
![](https://img.aspxhome.com/file/2023/6/134886_0s.jpg)
Python中如何使用Matplotlib库绘制图形
2021-09-27 11:59:04
![](https://img.aspxhome.com/file/2023/9/97699_0s.png)
sql cast,convert,QUOTENAME,exec 函数学习记录
2024-01-23 13:53:35
vue使用file-saver本地文件导出功能
2023-07-02 16:59:26
如何编写一个高效的国税系统通讯录数据库?
2009-11-07 18:53:00
Python根据输入参数计算结果的实例方法
2021-12-15 08:30:45
Python函数生成器原理及使用详解
2023-12-02 17:57:31
![](https://img.aspxhome.com/file/2023/1/130831_0s.png)
Python安装spark的详细过程
2021-05-17 09:59:05
![](https://img.aspxhome.com/file/2023/1/109671_0s.png)
使用FriendFeed来提升MySQL性能的方法
2024-01-28 13:14:44
![](https://img.aspxhome.com/file/2023/1/118211_0s.png)
使用Python自动化Microsoft Excel和Word的操作方法
2023-04-11 16:15:21
![](https://img.aspxhome.com/file/2023/3/75953_0s.png)
Python线程同步的实现代码
2022-10-01 00:20:48
Python实现识别手写数字 Python图片读入与处理
2022-07-14 17:48:10
![](https://img.aspxhome.com/file/2023/7/127797_0s.jpg)
mybatis+mysql 使用存储过程生成流水号的实现代码
2024-01-18 11:09:56
![](https://img.aspxhome.com/file/2023/8/69838_0s.png)
Python实现PS滤镜碎片特效功能示例
2021-04-25 01:35:31
![](https://img.aspxhome.com/file/2023/6/134426_0s.jpg)
SQL学习笔记三 select语句的各种形式小结
2011-09-30 11:09:31
python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法
2023-10-21 08:54:20
![](https://img.aspxhome.com/file/2023/3/80443_0s.png)