Python's method of implementing UDP datagram transmission

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.