首先先加強對Python socket的了解
Reference : https://ithelp.ithome.com.tw/articles/10205819
socket.accept //server等待client端的連線 accept代表socket 採用阻塞式訪問
| 位類別 | 名稱宣告 | 解釋內容 |
|---|---|---|
| family | socket.AF_UNIX | 於本機端進行串接 |
| socket.AF_INET | 於伺服器與伺服器之間進行串接 | |
| socket.AF_INET6 | 使用IPv6於伺服器與伺服器之間進行串接 | |
| type | socket.SOCK_STREAM | 使用TCP(資料流)的方式提供可靠、雙向、串流的通信頻道 |
| socket.SOCK_DGRAM | 使用UDP()的方式通用的免連線訊息交換通道 |
Reference : https://www.itread01.com/content/1541825839.html
Reference : https://www.itread01.com/content/1549100881.html
def convertRaw_and_SplitToSave(split_size):
print("[Func]splitDataAndSave")
with open(r'c:\test.jpg', 'rb') as f:
res = base64.b64encode(f.read())
print("len = " + str(len(str(res))) + " Context : " + str(res))
globals.tmp_array = re.findall('.{' + str(split_size) + '}', str(res))
globals.tmp_array.append(str(res)[(len(globals.tmp_array) * split_size):])
globals.tmp_array[0] = str(globals.tmp_array[0]).strip('b\'')
#找尋最後一筆 把'過濾掉回存 先取得長度 將陣列長度減1 strip(''') 然後回存
print("end print " + str(globals.tmp_array[0]))
另外,在轉塗作字串切割時 會有b'的特殊符號出現,必須去除
在此,參考這篇【python】去除字符串头尾的多余符号 https://blog.csdn.net/u014636245/article/details/103120201使用.strip('b\'')去除掉 加鞋線是因為跳脫字元'此外針對影像串流部分:Reference : https://www.jianshu.com/p/931cd24450ec上傳串流部分result, imgencode = cv2.imencode('.jpg', frame, encode_param)
data = numpy.array(imgencode)
stringData = data.tobytes()
stringDataLen = str(len(stringData)).ljust(16)
print("stringDataLength = " + stringDataLen)
client.send(stringDataLen.encode())
print("stringDataLen = " + stringDataLen + " Context : " + str(stringData))
print(stringData)
data_perform = [b'']
data_perform = re.findall(b"[\s\S]{500}", stringData) # 就是这里需要做小小的改造,看仔细哦
data_perform.append(stringData[(len(data_perform) * 500):])
#globals.tmp_array = re.findall('.{' + str(split_size) + '}', str(stringData))
#globals.tmp_array.append(str(stringData)[(len(globals.tmp_array) * split_size):])
print(data_perform)
print(len(data_perform))
for i in range(len(data_perform)):
print(" data_perform array len = " + str(len(data_perform)) + " ,prepare to send [" + str(i) + "]:" + str(data_perform[i]))
client.send(data_perform[i]);
ret, frame = capture.read()# 以幀為單位獲取影象
decimg = cv2.imdecode(data, 1)
cv2.imshow('CLIENT', decimg)
key = cv2.waitKey(20)
if key == 113: # 按q拍照, 比照ASCII碼
cv2.imwrite("fangjian2.jpeg", frame) # 拍照存檔檔名
continue
if key == 27: # 按esc停止, 比照ASCII碼
break
client.close()
cv2.destroyAllWindows()接收串流部分def recvall(sock, count):
buf = b''
while count:
newbuf = sock.recv(count)
if not newbuf: return None
buf += newbuf
count -= len(newbuf)
return buflength = recvall(conn, 16)
print(length)
stringData = recvall(conn, int(length))
data = numpy.fromstring(stringData, dtype='uint8')
decimg = cv2.imdecode(data, 1)
cv2.imshow('SERVER', decimg)
cv2.waitKey(30)
time.sleep(1)







