Explain the example of a select-based socket server in Python

Source: Internet
Author: User
This article mainly introduces the socket server implemented by Python based on select. The example analyzes the skills related to socket communication implemented by Python based on select and socket modules, for more information, see the example in this article. We will share this with you for your reference. The details are as follows:

The usage of select. select in the asyncore module is referenced.

import socketimport tracebackimport selectEOL1 = b'\n\n'EOL2 = b'\n\r\n'socketmap = {}r,w,e = [],[],[]response = b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'response += b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'response += b'Hello, world!'serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)serversocket.bind(('0.0.0.0', 23456))serversocket.listen(1)#serversocket.setblocking(0)listening_fileno = serversocket.fileno()socketmap[listening_fileno] = serversocketprint 'listening_fileno',listening_filenotry:  while True:    r,w,e = [],[],[]    for fd in socketmap:      r.append(fd)      w.append(fd)      e.append(fd)    r,w,e = select.select(r,w,e,1)    for fd in r:      request = b''      isocket = socketmap[fd]      if fd == listening_fileno:        print 'accepting'        clientsock,clientaddr = isocket.accept()        #clientsock.setblocking(0)        cli_fileno = clientsock.fileno()        r.append(cli_fileno)        w.append(cli_fileno)        e.append(cli_fileno)        socketmap[cli_fileno] = clientsock      else:        print 'reading'        while EOL1 not in request and EOL2 not in request:          request += isocket.recv(1024)        print(request.decode())    for fd in w:      print 'writing'      osocket = socketmap[fd]      osocket.send(response)    for fd in e:      esocket = socketmap[fd]      print 'socket close',fd      esocket.close()      del socketmap[fd]    print "no data coming"except Exception,e:  print traceback.print_exc()  serversocket.close()

For more details about the example of a socket server implemented by Python based on select, please follow the PHP Chinese network!

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.