gpt4 book ai didi

python使用tcp传输图片数据

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章python使用tcp传输图片数据由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例为大家分享了python使用tcp传输图片数据的具体代码,供大家参考,具体内容如下 。

数据包格式如下 。

python使用tcp传输图片数据

客户端:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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()

服务端:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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传输图片数据的文章就讲到这里了,如果你想了解更多关于python使用tcp传输图片数据的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com