python UDP of udp Protocol Send and Receive Examples

  • 2021-07-24 11:26:03
  • OfStack

Two files need to be created, one as the client and one as the server

File 1 is the client client and File 2 is the server server

The characteristic of udp is that there is no need to establish a connection

File 1 client


# There is no need to establish a connection 
import socket
# Create socket Object 
#SOCK_DGRAM  udp Mode 
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
# Send data   Byte 
s.sendto(" How do you do ".encode(),("169.254.184.146",8000))

File 2 server


import socket
# Create socket Object 
#SOCK_DGRAM  udp Mode 
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind(("169.254.184.146",8000)) # Object that binds to the server ip And ports 
data=s.recv(1024) #1 Secondary reception 1024 Byte 
print(data.decode())# decode() Decoding received bytes 

Note: Run File 2 first before running File 1


Related articles: