In this paper, we describe the method of Python transmitting UDP datagram, which is of practical value. Share to everyone for your reference. The specific methods are analyzed as follows:
Service-Side code:
Import Socket port = 8081 s = Socket.socket (socket.af_inet,socket. SOCK_DGRAM) #从给定的端口, from any sender, receives the UDP datagram S.bind (("", port)) print ' Waiting on port: ', port while True: data,addr = S.recvfrom (1024x768) #接收一个数据报 (maximum to 1024 bytes)
Client code:
Result: Run the server first, then run the client,
The service side prints out:
Waiting on Port:8081reciveed:hello World from (' 127.0.0.1 ', 62644)
Add:
Socket.sendto (string[, flags], address)
Official documents are as follows:
Send data to the socket. The socket should not being connected to a remote socket, since the destination socket was specified by address. The optional Flags argument have the same meaning as for recv () above. Return the number of bytes sent. (The format of the address depends on the address family-see above.) The address parameter is in the protocol type socket. SOCK_DGRAM, the address structure is a tuple, (host,port) format
Hopefully this article will help you with Python programming.