python 基于UDP協(xié)議套接字通信的實(shí)現(xiàn)
服務(wù)端:
’’’from socket import *server=socket(AF_INET,SOCK_DGRAM)- 這里代指的是一種數(shù)據(jù)報(bào)協(xié)議,數(shù)據(jù)報(bào)協(xié)議指的就是udp協(xié)議(補(bǔ)充: 數(shù)據(jù)報(bào)就是自己utp協(xié)議中有自己的頭,有自己的數(shù)據(jù)部分)server.bind(’IP’, PORT)bytes類型的數(shù)據(jù), client_addr = server.recvfrom(1024) - client_addr是一個(gè)2元組的形式: 第一個(gè)參數(shù)是客戶端的IP地址, 第二個(gè)參數(shù)是客戶端發(fā)送數(shù)據(jù)進(jìn)程軟件的端口號.server.sendto(bytes類型處理過后的數(shù)據(jù), client_addr)server.close()’’’from socket import *server=socket(AF_INET,SOCK_DGRAM)IP_PORT=(’127.0.0.1’,8123)server.bind(IP_PORT)while True: print('server wait...') data_bytes,client_addr=server.recvfrom(1024) server.sendto(data_bytes.upper(),client_addr) print(’data_bytes:’, data_bytes) print(’client_addr:’, client_addr)server.close()
客戶端:
’’’from socket import *client=socket(AF_INET,SOCK_DGRAM)client.sendto(bytes類型的數(shù)據(jù), (’服務(wù)端IP’, 服務(wù)端端口))data_bytes, client_addr = client.recvfrom(1024)client.close()’’’import socketclient = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)IP_PORT=(’127.0.0.1’,8123)while True: msg=input('請輸入要發(fā)送的消息》》》').strip() client.sendto(msg.encode('utf-8'),IP_PORT) data_bytes,server_addr=client.recvfrom(1024) print('data_bytes:',data_bytes) print('server_addr:',server_addr)client.close() UDP是無鏈接的,先啟動(dòng)哪一端都不會(huì)報(bào)錯(cuò) UDP協(xié)議是數(shù)據(jù)報(bào)協(xié)議,發(fā)空的時(shí)候也會(huì)自帶報(bào)頭,因此客戶端輸入空,服務(wù)端也能收到 二、UDP普遍無粘包問題
服務(wù)端:
import socketserver = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 數(shù)據(jù)報(bào)協(xié)議-》udpserver.bind((’127.0.0.1’, 8084))data, client_addr = server.recvfrom(1024) # b’hello’==>b’h’print(’第一次:’, client_addr, data)data, client_addr = server.recvfrom(1024) # b’world’ =>b’world’print(’第二次:’, client_addr, data)#data,client_addr=server.recvfrom(1024)print(’第三次:’,client_addr,data)server.close()
客戶端
import socketclient = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 數(shù)據(jù)報(bào)協(xié)議-》udpclient.sendto(’hello’.encode(’utf-8’), (’127.0.0.1’, 8084))client.sendto(’world’.encode(’utf-8’), (’127.0.0.1’, 8084))client.sendto(’’.encode(’utf-8’),(’127.0.0.1’,8084))client.close() UDP協(xié)議一般不用于傳輸大數(shù)據(jù) UDP普遍雖然沒有粘包問題,但是并不能替代TCP,因?yàn)閁DP協(xié)議有一個(gè)缺陷:如果發(fā)送數(shù)據(jù)的途中發(fā)生數(shù)據(jù)丟失,則數(shù)據(jù)就真的丟失了,而TCP協(xié)議就不會(huì)有這種缺陷,因此一般UDP用于一些無關(guān)緊要的數(shù)據(jù)發(fā)送,例如QQ、微信聊天等…三、總結(jié):UDP與TCP的區(qū)別
區(qū)別一:UDP協(xié)議不會(huì)因?yàn)榭蛻舳税l(fā)送的數(shù)據(jù)為空,從而導(dǎo)致客戶端和服務(wù)端發(fā)生異常。區(qū)別二:UDP協(xié)議服務(wù)端不會(huì)因?yàn)榭蛻舳藦?qiáng)制斷開連接,從而導(dǎo)致服務(wù)端發(fā)生異常
UDP協(xié)議叫數(shù)據(jù)報(bào)協(xié)議,什么叫數(shù)據(jù)報(bào)?報(bào)就分成頭和數(shù)據(jù)兩部分, 它是一個(gè)完整的整體. 它不是單純的數(shù)據(jù) 舉個(gè)例子: 基于UDP協(xié)議發(fā)送的數(shù)據(jù), 每次的發(fā)都是一個(gè)集裝箱過去,并不是空的,所以,你的數(shù)據(jù)看起來是空,但是我會(huì)在數(shù)據(jù)報(bào)的基礎(chǔ)上,對你的數(shù)據(jù)進(jìn)行一個(gè)處理,所以說服務(wù)端收到的并不是空. 數(shù)據(jù)報(bào)的概念: 當(dāng)客戶端發(fā)送的數(shù)據(jù)雖然是空,但是數(shù)據(jù)報(bào)會(huì)以一個(gè)集裝箱的樣子給你發(fā)送到服務(wù)端過去,因此服務(wù)端收到的,其實(shí)并不是空的數(shù)據(jù), 服務(wù)端收到的還有客戶端的Ip和端口 四、案例1、基于UDP協(xié)議實(shí)現(xiàn)時(shí)間格式化服務(wù)器服務(wù)端
from socket import *from time import strftimeserver=socket(AF_INET,SOCK_DGRAM)server.bind(('127.0.0.1',8908))print('server run...')while True: msg,addr=server.recvfrom(1024) print(f'[{addr[0]}]鏈接成功') if not msg: fmt = '%Y-%m-%d %X' else: fmt=msg.decode('utf-8') time_fmt=strftime(fmt) server.sendto(time_fmt.encode('utf-8'),addr)
客戶端
from socket import *client=socket(AF_INET,SOCK_DGRAM)print('輸入時(shí)間格式,返回格式化后的時(shí)間')ip_port=('127.0.0.1',8908)while True: inp=input('請輸入時(shí)間格式(例:%Y-%m-%d)>>>:').strip() client.sendto(inp.encode('utf-8'),ip_port) date=client.recv(1024) print(date.decode('utf-8'))2、基于udp協(xié)議是實(shí)現(xiàn)米聊功能
注意:聊天是客戶端與客戶端進(jìn)行的聊天,客戶端把數(shù)據(jù)發(fā)送到了服務(wù)端,再有服務(wù)端轉(zhuǎn)發(fā)到客戶端,這樣就是實(shí)現(xiàn)了客戶端與客戶端之間的的聊天。
需求:基于UDP協(xié)議是實(shí)現(xiàn)一個(gè)多用戶通信,可回多個(gè)客戶端的信息, 回完一個(gè)緊接著可回下一個(gè), 不需要連接
服務(wù)端:
#_*_coding:utf-8_*___author__='淘小欣'import socketip_port = (’127.0.0.1’, 8081)UDP_server_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #買手機(jī)UDP_server_sock.bind(ip_port)print('Server...')while True: ml_msg, addr = UDP_server_sock.recvfrom(1024) print(’來自[%s:%s]的一條消息:033[1;44m%s033[0m’ % (addr[0], addr[1], ml_msg.decode(’utf-8’))) back_msg = input(’回復(fù)消息: ’).strip() UDP_server_sock.sendto(back_msg.encode(’utf-8’), addr)
客戶端一:
#_*_coding:utf-8_*___author__='淘小欣'import socketBUFSIZE=1024UDP_client_socket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)MiLiao_name_dic={ '淘小欣':('127.0.0.1',8881), 'shawn':('127.0.0.1',8881), '派大星':('127.0.0.1',8881), '派大星的真心話':('127.0.0.1',8881)}while True: ml_name = input(’請選擇聊天對象: ’).strip() while True: msg = input(’請輸入消息,回車發(fā)送: ’).strip() if msg == ’quit’: break if not msg or not ml_name or ml_name not in MiLiao_name_dic: continue UDP_client_socket.sendto(msg.encode(’utf-8’), MiLiao_name_dic[ml_name]) back_msg, addr = UDP_client_socket.recvfrom(BUFSIZE) print(’來自[%s:%s]的一條消息:033[1;44m%s033[0m’ % (addr[0], addr[1], back_msg.decode(’utf-8’)))UDP_client_socket.close()
客戶端二:
#_*_coding:utf-8_*___author__='淘小欣'import socketBUFSIZE=1024UDP_client_socket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)MiLiao_name_dic={ '淘小欣':('127.0.0.1',8881), 'shawn':('127.0.0.1',8881), '派大星':('127.0.0.1',8881), '派大星的真心話':('127.0.0.1',8881)}while True: ml_name = input(’請選擇聊天對象: ’).strip() while True: msg = input(’請輸入消息,回車發(fā)送: ’).strip() if msg == ’quit’: break if not msg or not ml_name or ml_name not in MiLiao_name_dic: continue UDP_client_socket.sendto(msg.encode(’utf-8’), MiLiao_name_dic[ml_name]) back_msg, addr = UDP_client_socket.recvfrom(BUFSIZE) print(’來自[%s:%s]的一條消息:033[1;44m%s033[0m’ % (addr[0], addr[1], back_msg.decode(’utf-8’)))UDP_client_socket.close()
以上就是python 基于UDP協(xié)議套接字通信的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于python 套接字通信的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. bootstrap select2 動(dòng)態(tài)從后臺Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼2. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼3. ASP常用日期格式化函數(shù) FormatDate()4. 網(wǎng)頁中img圖片使用css實(shí)現(xiàn)等比例自動(dòng)縮放不變形(代碼已測試)5. html中的form不提交(排除)某些input 原創(chuàng)6. CSS3中Transition屬性詳解以及示例分享7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. python 如何在 Matplotlib 中繪制垂直線9. jsp文件下載功能實(shí)現(xiàn)代碼10. 開發(fā)效率翻倍的Web API使用技巧
