Python UDP Socket

Source: Internet
Author: User

Socket (socket), the endpoint of the Transport Layer communication, consisting of an IP and a port number (Ip,port), which can be used to accurately locate and communicate with the process on the server through the socket

python2.6 implementation, based on af_inet (network sockets)
Type Socket_stream (TCP socket), Socket_dgram (UDP socket)

UDP socket implementation is more simple than TCP, there is no process of establishing a connection, the server infinite loop to receive data, processing data return, the client does not need to establish a communication connection,
Send data directly to receive data

UDP Socket Communication Logic
UDP Service side: Create socket>>> bind the ground server >>> infinite loop >>> receive information return information
UDP client: Create socket>>> send information to receive information

Python UDP Socket Service side

1     #!/usr/bin/python2     #-*-coding utf-8-*-3 4     #filename:Udpsocket.py5     #Author:6     #Create date:2015-03-257     #Modify Date:8     #DESCRIPTION:UDP Socket9 Ten     #Import Stander Lib One      fromSocketImportsocket, af_inet, Sock_dgram A     ImportSYS -Sys.path.append ('..' ) -  the  -     classudpsocket (): -         " " - Udpsocket class, set up UDP socket server + Usage: - p = udpsocket.udpsocket () + P.listen () A Infinite loop Listening port, process rewrite data processing method at         " " -          -         def __init__(self): -             " " - Initialize, establish SOCKET,IP port bindings -             " " inSelf.udpserversocket =socket (af_inet, SOCK_DGRAM) -Self.udpServerSocket.bind (('127.0.0.1', 20015) ) toSelf.buffer_size = 1024#buffer is 1k +              -              the         defreceive (self): *             " " $ collect data from the portPanax Notoginseng             " " -Raw_data, addr =self.udpServerSocket.recvfrom (int (self.buffer_size)) the             Print 'Conn from:', addr +             Print 'receive from UDP client:', Raw_data A              the             return(raw_data, addr) +              -  $         defprocess (self, data): $             " " - socket transfer data processing, inheritance override this function -             " " theresult =Data -             returnresultWuyi  the          -         defSend (self, addr, result): Wu             " " - return information About             " " $ self.udpServerSocket.sendto (result, addr) -              -          -         defListen (self): A             " " + listening port, infinite loop the             " " -              whileTrue: $                 Print 'Wait for Connect' theSocket_data = Self.receive ()#Receive Data theresult = Self.process (socket_data[0])#processing the received information theSelf.send (socket_data[1], str (result))#Return Data the self.close () -          in          the         defClose (self): the             " " About Close Connection the             " " the self.udpServerSocket.close () the  +              -         def __del__(self): the self.close ()Bayi              the  the     if __name__=='__main__': -p =Udpsocket () -P.listen ()

Python UDP Socket Client

1     #!/usr/bin/env python2     #-*-coding:utf-8-*-3 4      fromSocketImport*5 6HOST ='127.0.0.1'       #Host7PORT = 20015#Port8Buffsize = 1024#Buffer size9ADDR = (HOST, PORT)#AddressTen  One     #set up socket monitoring AUdpclientsocket =socket (af_inet, SOCK_DGRAM) -  -     #Transmit received data theUdpclientsocket.sendto ('Hello', ADDR) -data, addr =Udpclientsocket.recvfrom (buffsize) -     Print 'Receive data:', the data -     #data, addr = Udpclientsocket.recvfrom (buffsize) +Udpclientsocket.close ()

Python UDP Socket

Related Article

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.