一、UPD實現(xiàn)單用戶通信
服務(wù)端:
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
|
''' from socket import * server=socket(AF_INET,SOCK_DGRAM) - 這里代指的是一種數(shù)據(jù)報協(xié)議,數(shù)據(jù)報協(xié)議指的就是udp協(xié)議(補(bǔ)充: 數(shù)據(jù)報就是自己utp協(xié)議中有自己的頭,有自己的數(shù)據(jù)部分) server.bind('IP', PORT) bytes類型的數(shù)據(jù), client_addr = server.recvfrom(1024) - client_addr是一個2元組的形式: 第一個參數(shù)是客戶端的IP地址, 第二個參數(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() |
客戶端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
''' 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 socket client = 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是無鏈接的,先啟動哪一端都不會報錯
- UDP協(xié)議是數(shù)據(jù)報協(xié)議,發(fā)空的時候也會自帶報頭,因此客戶端輸入空,服務(wù)端也能收到
二、UDP普遍無粘包問題
服務(wù)端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import socket server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 數(shù)據(jù)報協(xié)議-》udp server.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() |
客戶端
1
2
3
4
5
6
7
8
9
|
import socket client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 數(shù)據(jù)報協(xié)議-》udp client.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,因為UDP協(xié)議有一個缺陷:如果發(fā)送數(shù)據(jù)的途中發(fā)生數(shù)據(jù)丟失,則數(shù)據(jù)就真的丟失了,而TCP協(xié)議就不會有這種缺陷,因此一般UDP用于一些無關(guān)緊要的數(shù)據(jù)發(fā)送,例如QQ、微信聊天等…
三、總結(jié):UDP與TCP的區(qū)別
區(qū)別一:UDP協(xié)議不會因為客戶端發(fā)送的數(shù)據(jù)為空,從而導(dǎo)致客戶端和服務(wù)端發(fā)生異常。
區(qū)別二:UDP協(xié)議服務(wù)端不會因為客戶端強(qiáng)制斷開連接,從而導(dǎo)致服務(wù)端發(fā)生異常
- UDP協(xié)議叫數(shù)據(jù)報協(xié)議,什么叫數(shù)據(jù)報?報就分成頭和數(shù)據(jù)兩部分, 它是一個完整的整體. 它不是單純的數(shù)據(jù)
- 舉個例子: 基于UDP協(xié)議發(fā)送的數(shù)據(jù), 每次的發(fā)都是一個集裝箱過去,并不是空的,所以,你的數(shù)據(jù)看起來是空,但是我會在數(shù)據(jù)報的基礎(chǔ)上,對你的數(shù)據(jù)進(jìn)行一個處理,所以說服務(wù)端收到的并不是空.
- 數(shù)據(jù)報的概念: 當(dāng)客戶端發(fā)送的數(shù)據(jù)雖然是空,但是數(shù)據(jù)報會以一個集裝箱的樣子給你發(fā)送到服務(wù)端過去,因此服務(wù)端收到的,其實并不是空的數(shù)據(jù), 服務(wù)端收到的還有客戶端的Ip和端口
四、案例
1、基于UDP協(xié)議實現(xiàn)時間格式化服務(wù)器
服務(wù)端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from socket import * from time import strftime server = 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) |
客戶端
1
2
3
4
5
6
7
8
9
|
from socket import * client = socket(AF_INET,SOCK_DGRAM) print ( "輸入時間格式,返回格式化后的時間" ) ip_port = ( "127.0.0.1" , 8908 ) while True : inp = input ( "請輸入時間格式(例:%Y-%m-%d)>>>:" ).strip() client.sendto(inp.encode( "utf-8" ),ip_port) date = client.recv( 1024 ) print (date.decode( "utf-8" )) |
2、基于udp協(xié)議是實現(xiàn)米聊功能
注意:聊天是客戶端與客戶端進(jìn)行的聊天,客戶端把數(shù)據(jù)發(fā)送到了服務(wù)端,再有服務(wù)端轉(zhuǎn)發(fā)到客戶端,這樣就是實現(xiàn)了客戶端與客戶端之間的的聊天。
需求:基于UDP協(xié)議是實現(xiàn)一個多用戶通信,可回多個客戶端的信息, 回完一個緊接著可回下一個, 不需要連接
服務(wù)端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#_*_coding:utf-8_*_ __author__ = "淘小欣" import socket ip_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%s\033[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) |
客戶端一:
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
|
#_*_coding:utf-8_*_ __author__ = "淘小欣" import socket BUFSIZE = 1024 UDP_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%s\033[0m' % (addr[ 0 ], addr[ 1 ], back_msg.decode( 'utf-8' ))) UDP_client_socket.close() |
客戶端二:
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
|
#_*_coding:utf-8_*_ __author__ = "淘小欣" import socket BUFSIZE = 1024 UDP_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%s\033[0m' % (addr[ 0 ], addr[ 1 ], back_msg.decode( 'utf-8' ))) UDP_client_socket.close() |
以上就是python 基于UDP協(xié)議套接字通信的實現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于python 套接字通信的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://blog.csdn.net/weixin_44621343/article/details/112910996