Python Udp Socket and pythonudpsocket

Source: Internet
Author: User

Python Udp Socket and pythonudpsocket

Socket (socket), the transport layer communication endpoint, composed of IP and Port number (IP, Port), you can use socket to precisely find and communicate with processes on the server

Python2.6 implementation, based on AF_INET (network socket)
SOCKET_STREAM (TCP socket), SOCKET_DGRAM (UDP socket)

UDP socket implementation is simpler than TCP, and there is no connection process. The server receives data in an infinite loop and processes the data returned. The client does not need to establish a communication connection either,
Send data directly to receive data.

UDP socket communication logic
UDP server: Create socket >>> bind local server >>> infinite loop >>> receive information return information
UDP client: Create socket> send message receiving information

 

Python Udp Socket server

1 #! /Usr/bin/python 2 #-*-coding UTF-8-*-3 4 # filename: Udpsocket. py 5 # author: 6 # create date: 2015-03-25 7 # modify date: 8 # description: udp socket 9 10 # import stander lib11 from socket import socket, AF_INET, SOCK_DGRAM12 import sys13 sys. path. append ('.. ') 14 15 16 class Udpsocket (): 17''' 18 Udpsocket class, build udp socket server 19 usage: 20 p = Udpsocket. udpsocket () 21 p. listen () 22 infinite loop listening port, process rewrite Data Processing Method 23 '''24 25 def _ init _ (self): 26''' 27 initialization, establish socket, bind the ip port to 28''' 29 self. udpServerSocket = socket (AF_INET, SOCK_DGRAM) 30 self. udpServerSocket. bind ('2017. 0.0.1 ', 20015) 31 self. buffer_size = 1024 # the buffer is 1k32 33 34 def receive (self): 35''' 36. The collected data from the port is 37''' 38 raw_data, addr = self. udpServerSocket. recvfrom (int (self. buffer_size) 39 print 'conn from: ', addr40 print 'receive from udp client:', raw_data41 42 return (raw_data, addr) 43 44 45 def process (self, data ): 46 ''' 47 Data Processing for socket transmission. inherit to rewrite this function by 48 ''' 49 result = data50 return result51 52 53 def send (self, addr, result ): 54 ''' 55 returned information ''' 57 self. udpServerSocket. sendto (result, addr) 58 59 60 def listen (self): 61 '''62 listener port, infinite loop 63''' 64 while True: 65 print 'Wait for connect '66 socket_data = self. receive () # receive data 67 result = self. process (socket_data [0]) # process received information 68 self. send (socket_data [1], str (result) # return data 69 self. close () 70 71 72 def close (self): 73 ''' 74 close connection 75''' 76 self. udpServerSocket. close () 77 78 79 def _ del _ (self): 80 self. close () 81 82 83 if _ name _ = '_ main _': 84 p = Udpsocket () 85 p. listen ()

Python Udp Socket Client

1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 4 from socket import * 5 6 HOST = '2017. 0.0.1 '# HOST 7 PORT = 20015 # PORT 8 BUFFSIZE = 1024 # buffer size 9 ADDR = (HOST, PORT) # address 10 11 # establish socket listening 12 udpClientSocket = socket (AF_INET, SOCK_DGRAM) 13 14 # transfer received data 15 udpClientSocket. sendto ('hello', ADDR) 16 data, addr = udpClientSocket. recvfrom (BUFFSIZE) 17 print 'receive data: ', data18 # data, addr = udpClientSocket. recvfrom (BUFFSIZE) 19 udpClientSocket. close ()

 

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.