Python handles multiple clients using XINETD

Source: Internet
Author: User

xinted (eXtended Internet daemon) is the network daemon. XINETD can listen to multiple specified ports at the same time, when accepting user requests, it can initiate different network service processes to handle these user requests depending on the port the user requests.

For more information about xinted, you can refer to http://www.cnblogs.com/itech/archive/2010/12/27/1914846.html


Example 1, call httpserver.py using xinetd:

httpserver.py

#!/usr/bin/pythonimport sysrequest = "while True:data= sys.stdin.readline (). Strip () Request = Request + data + ' &L T;br> ' if data = = ': print ' http/1.0 OK ' print ' print ' 


1.chmod u+x httpserver.py


2. Create a httpserver (any name) under/ETC/XINETD.D and set it up:

Vim/etc/xinetd.d/httpserver

service pythontestserver{        disable = no         flags            = REUSE        type             = UNLISTED        port             = 9991         protocol        = tcp         socket_type     = stream         wait            = no         user             = root        server          =  /soft/11.27/httpserver.py        server_args      = /soft/11.27/httpserver.py        log_on_failure   += userid


3. Restart XINET.D is configured to take effect

/etc/init.d/xinetd restart


4. Look at Port 9991 (that is, port = 9991 set above) to open

[[email protected] 11.27]# netstat-atnl|grep 9991tcp 0 0 0.0.0.0:9991 0.0.0.0:*


5. Open http://192.168.220.2:9991/to see the data

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/09/wKiom1R1-Hzw6u8OAAGAzUF89Z4864.jpg "title=" A.png " alt= "Wkiom1r1-hzw6u8oaagazuf89z4864.jpg"/>


Example 2, call errorserver.py using xinetd:

errorserver.py ( note : Xinet.d errorserver.py because S.bind is set to listen on port 51423 ( Host,port)) to comment out, otherwise the "address already in use" error is reported

#!/usr/bin/pythonimport socket,traceback,time,struct,syshost =  ' 192.168.220.2 ' port =  51423s=socket.socket (Socket.af_inet,socket. SOCK_STREAM) s.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1) #s. Bind ((Host,port)) S.listen (1) while true:    try:         message,address = s.accept ()     except  ( Keyboardinterrupt, systemexit):        raise     except:        traceback.print_exc ()          continue    try:        # HOST = SOCKET.GETHOSTBYADDR (message)         print  ' Got  name %s from %s '  %  (Host,message.getpeername ())           #raise  keyboardinterrupt         #print   ' Got connection from ',  Message.getpeername ()     except:        print   ' cannot get name! '     try:        message.close ()      except KeyboardInterrupt:        print  ' ctrl +  c,exit! '         sys.exit (1)     except:         traceback.print_exc ()


    1. chmod u+x errorserver.py


2. Create a pythontestserver (any name) under/ETC/XINETD.D and set it up:

Vim/etc/xinetd.d/pythontestserver

service pythontestserver{        disable = no         flags            = REUSE        type             = UNLISTED        port             = 51423         protocol        = tcp         socket_type     = stream         wait            = no         user             = root        server          =  /soft/11.25/errorserver.py        server_args      = /soft/11.25/errorserver.py        log_on_failure   += userid


3. Restart XINET.D is configured to take effect

/etc/init.d/xinetd restart


4. Look at Port 51423 (that is, port = 51423 set above) to open

[[email protected] 11.25]# netstat -atnl|grep 51423tcp         0      0 0.0.0.0:51423                0.0.0.0:*                    LISTEN       tcp        9      0  127.0.0.1:51423             127.0.0.1:53440              CLOSE_WAIT  tcp         0      0 127.0.0.1:51423              127.0.0.1:53442          &Nbsp;   established 


5.XINET.D configured, use Telnet to connect to port 51423. Here is the result of the run

[[Email protected] ~]# telnet localhost 51423Trying 127.0.0.1...Connected to Localhost.localdomain (127.0.0.1). Escape character is ' ^] '.


Python handles multiple clients using XINETD

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.