Python socket連接中的粘包、精確傳輸問題實例分析
本文實例講述了Python socket連接中的粘包、精確傳輸問題。分享給大家供大家參考,具體如下:
粘包: 發(fā)生原因:當(dāng)調(diào)用send的時候,數(shù)據(jù)并不是即時發(fā)給客戶端的。而是放到了系統(tǒng)的socket發(fā)送緩沖區(qū)里,等緩沖區(qū)滿了、或者數(shù)據(jù)等待超時了,數(shù)據(jù)才會發(fā)送,所以有時候發(fā)送太快的話,前一份數(shù)據(jù)還沒有傳給客戶端,那么這份數(shù)據(jù)和上一份數(shù)據(jù)一起發(fā)給客戶端的時候就會造成“粘包” 。
解決方案:解決根源的思想是避免不同段的數(shù)據(jù)一起發(fā)送。
方案1:前一段數(shù)據(jù)send完后,等待一段時間再send第二段數(shù)據(jù)。缺點:時間效率低,而且也無法完全避免問題【因為不清楚該設(shè)置多少時間才能保證前一份數(shù)據(jù)已經(jīng)發(fā)送】 方案2:握手機制:前一段數(shù)據(jù)send完后,嘗試recv,等待客戶端回應(yīng),確認第一段數(shù)據(jù)發(fā)送完后,再send第二段數(shù)據(jù)。完美方案? 方案二的演示:服務(wù)端【發(fā)送方】代碼:
import socketserver=socket.socket()server.bind(('localhost',1234))server.listen()while True: print('正在等待。。。') conn,addr=server.accept() while True: try: conn.send(b'first info') ack=conn.recv(1024) #接收客戶端確認 print(ack) conn.send(b'second info') except ConnectionResetError as e: print(e) breakserver.close()
客戶端【接收方】代碼:
import socketclient=socket.socket()client.connect(('localhost',1234))data=client.recv(1024)print(data.decode())client.send(b'ack')#發(fā)送確認data=client.recv(1024)print(data.decode())client.close()不精確傳輸問題:發(fā)生原因:
由于數(shù)據(jù)太大,發(fā)送方一次send不完,而接收方只recv一次,使得影響了后面數(shù)據(jù)的傳輸
解決方案:解決根源的思想是改變recv的次數(shù)。
方案:將數(shù)據(jù)的大小發(fā)給接收方,讓接收方來決定recv的次數(shù)方案實現(xiàn)代碼【以解決長數(shù)據(jù)shell命令傳輸為例】:服務(wù)端【發(fā)送方】:
import socket,osserver=socket.socket()server.bind(('localhost',1234))server.listen()while True: print('正在等待...') conn,addr=server.accept() print('連接成功!') while True: try: cmd=conn.recv(1024) data=os.popen(cmd.decode()).read() # print(data) cmd_len=len(data.encode()) print(cmd_len) #發(fā)現(xiàn)這里如果cmd_len為0會導(dǎo)致異常,有些是沒有返回值的command if cmd_len==0:data='command has nothing return'cmd_len=len(data.encode()) ##因為這里前面沒有發(fā)送操作,所以不用擔(dān)心粘包,如果有則要考慮處理 conn.send(str(cmd_len).encode())#因為len結(jié)果是int,所以還要轉(zhuǎn)換 #這里要處理粘包 ack=conn.recv(1024) conn.send(data.encode()) except ConnectionResetError as e: print(e) breakserver.close()
客戶端【接收方】:
import socketclient=socket.socket()client.connect(('localhost',1234))while True: cmd = input('>>:') client.send(cmd.encode()) data_len=client.recv(1024) data_len=int(data_len.decode()) print(data_len) recv_len=0 client.send(b’ack’) total_data=b’’ while recv_len<data_len: data=client.recv(1024) recv_len+=len(data) total_data+=data print(total_data.decode())client.close() 利用這個原理可以實現(xiàn)文件傳輸,只要能確定接受次數(shù),就能保證文件傳輸?shù)拇笮≌_。
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章:
