python udp客戶端,服務端

2021-09-24 15:02:25 字數 1311 閱讀 3200

"""

客戶端1、建立套接字

2、收發資料

3、關閉套接字

"""import socket

# 建立套接字

socket = socket.socket(socket.af_inet, socket.sock_dgram)

# 傳送資料(傳送資料的型別必須是位元組型別)

data =

'你猜'

.encode(

'gbk'

)addr =

('192.168.16.48'

,8080

)socket.sendto(data, addr)

# 關閉套接字

socket.close(

)"""

服務端1、建立套接字

2、繫結ip和埠

3、實現接收資料

4、關閉套接字

"""import socket

# 建立套接字

socket = socket.socket(socket.af_inet, socket.sock_dgram)

# 繫結ip和埠

socket.bind(

('192.168.1.129'

,8089))

print

('繫結埠完畢.............'

)while

true

:print

('等待客戶端傳送資料..........'

) res = socket.recvfrom(

1204

)# print('客戶端傳送的內容是:{}'.format(res))

data, addr_recv = res

print

('客戶端說:{}'

.format

(data.decode(

'gbk'))

)if'bye'

in data.decode(

'gbk'):

break

data =

input

('服務端說:'

) addr_send =

('192.168.1.129'

,8081

) socket.sendto(data.encode(

'gbk'

), addr_send)

print

('資料傳送完畢.......'

)print

('聊天結束。。。。。'

)socket.close(

)

Python UDP客戶端 服務端

udpclient.py coding utf 8 from socket import servername 127.0.0.1 伺服器位址,本例中使用一台遠端主機 serverport 12000 伺服器指定的埠 clientsocket socket af inet,sock dgram 建立...

udp服務端 客戶端

個數 2的16次方 埠是資料發出或接收的入口 埠的目的 通過埠號找到對應的程序,完成資料的通訊 著名埠0 1023 這是建立了乙個基於udp協議的服務端 import socket todo 1.0 建立了乙個套接字,用來連線客戶端,傳送與接收資料 udp server socket.socket ...

Windows TCP 客戶端服務端

服務端 socket bind listen accept recv send close 客戶端 socket connect send recv close socket 函式 網路位元組 network byte 轉換函式tcp socket程式設計 服務端 include include i...