首页 > 代码库 > python使用xinetd处理多客户端

python使用xinetd处理多客户端

    xinted(eXtended InterNET daemon)即网络守护进程。xinetd能够同时监听多个指定的端口,在接受用户请求时,它能够根据用户请求的端口的不同,启动不同的网络服务进程来处理这些用户请求。

详细了解xinted,可以参考http://www.cnblogs.com/itech/archive/2010/12/27/1914846.html 


例子1,使用xinetd调用httpserver.py:

httpserver.py

#!/usr/bin/python
import sys

request = ‘‘
while True:
    data=http://www.mamicode.com/ sys.stdin.readline().strip()>


1.chmod u+x httpserver.py


2.在/etc/xinetd.d下创建httpserver(任意名字),进行设置:

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.重启xinet.d是配置生效

/etc/init.d/xinetd restart


4.这时查看9991端口(即上面设置的port = 9991)是否打开

[root@cai 11.27]# netstat -atnl|grep 9991
tcp        0      0 0.0.0.0:9991                0.0.0.0:*


5.打开http://192.168.220.2:9991/,能看到数据

wKiom1R1-Hzw6u8OAAGAzUF89Z4864.jpg


例子2,使用xinetd调用errorserver.py:

errorserver.py(注意:由于xinet.d设置了侦听51423端口,因此errorserver.py的s.bind((host,port))要注释掉,否则会报"address already in use"的错误)

#!/usr/bin/python

import socket,traceback,time,struct,sys

host = ‘192.168.220.2‘
port = 51423
s=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.在/etc/xinetd.d下创建pythontestserver(任意名字),进行设置:

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.重启xinet.d是配置生效

/etc/init.d/xinetd restart


4.这时查看51423端口(即上面设置的port = 51423)是否打开

[root@cai 11.25]# netstat -atnl|grep 51423
tcp        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             ESTABLISHED


5.xinet.d配置好了,使用telnet连接51423端口。下面是运行结果

[root@cai ~]# telnet localhost 51423
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is ‘^]‘.


python使用xinetd处理多客户端