python使用tcp傳輸圖片數(shù)據(jù)
本文實(shí)例為大家分享了python使用tcp傳輸圖片數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
數(shù)據(jù)包格式如下
客戶(hù)端:
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)) #包頭標(biāo)志 arrBuf = bytearray(b'\xff\xaa\xff\xaa') #以二進(jìn)制方式讀取圖片 picData = open('1.jpg', 'rb') picBytes = picData.read() #圖片大小 picSize = len(picBytes) #數(shù)據(jù)體長(zhǎng)度 = guid大小(固定) + 圖片大小 datalen = 64 + picSize #組合數(shù)據(jù)包 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()
服務(wù)端:
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)) #接收到的數(shù)據(jù),前64字節(jié)是guid,后面的是圖片數(shù)據(jù) 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) #將圖片數(shù)據(jù)保存到本地文件 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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python使用socket高效傳輸視頻數(shù)據(jù)幀(連續(xù)發(fā)送圖片)
- python 中Arduino串口傳輸數(shù)據(jù)到電腦并保存至excel表格
- 對(duì)python中基于tcp協(xié)議的通信(數(shù)據(jù)傳輸)實(shí)例講解
- 在python環(huán)境下運(yùn)用kafka對(duì)數(shù)據(jù)進(jìn)行實(shí)時(shí)傳輸?shù)姆椒?/a>
- 使用python實(shí)現(xiàn)http及ftp服務(wù)進(jìn)行數(shù)據(jù)傳輸?shù)姆椒?/a>
- Python爬蟲(chóng)抓取手機(jī)APP的傳輸數(shù)據(jù)
- python網(wǎng)絡(luò)編程之?dāng)?shù)據(jù)傳輸U(kuò)DP實(shí)例分析
- python實(shí)現(xiàn)udp數(shù)據(jù)報(bào)傳輸?shù)姆椒?/a>
- Python數(shù)據(jù)傳輸黏包問(wèn)題
相關(guān)文章
使用Streamlit和Pandas實(shí)現(xiàn)帶有可點(diǎn)擊鏈接的數(shù)據(jù)表格
這篇文章主要為大家詳細(xì)介紹了如何利用?Streamlit?和?Pandas?在?Python?中創(chuàng)建一個(gè)帶有可點(diǎn)擊鏈接的數(shù)據(jù)表格,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11使用python Telnet遠(yuǎn)程登錄執(zhí)行程序的方法
今天小編就為大家分享一篇使用python Telnet遠(yuǎn)程登錄執(zhí)行程序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Django項(xiàng)目如何正確配置日志(logging)
本文將教你如何在Django項(xiàng)目中正確配置日志(logging),讓Django生成log日志文件,并在程序運(yùn)行發(fā)生error級(jí)別故障時(shí)通知管理員。2021-04-04解決pymysql cursor.fetchall() 獲取不到數(shù)據(jù)的問(wèn)題
這篇文章主要介紹了解決pymysql cursor.fetchall() 獲取不到數(shù)據(jù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05python使用TensorFlow進(jìn)行圖像處理的方法
本篇文章主要介紹了使用TensorFlow進(jìn)行圖像處理的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02python中將函數(shù)賦值給變量時(shí)需要注意的一些問(wèn)題
變量賦值是我們?cè)谌粘i_(kāi)發(fā)中經(jīng)常會(huì)遇到的一個(gè)問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于python中將函數(shù)賦值給變量時(shí)需要注意的一些問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-08-08在pytorch中如何查看模型model參數(shù)parameters
這篇文章主要介紹了在pytorch中如何查看模型model參數(shù)parameters,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11windows10 pycharm下安裝pyltp庫(kù)和加載模型實(shí)現(xiàn)語(yǔ)義角色標(biāo)注的示例代碼
這篇文章主要介紹了windows10 pycharm下安裝pyltp庫(kù)和加載模型實(shí)現(xiàn)語(yǔ)義角色標(biāo)注,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05