Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Python使用xinetd处理多客户端

xinted(eXtended InterNET daemon)即网络守护进程。xinetd能够同时监听多个指定的端口,在接受用户请求时,它能够根据用户请求的端口的不同,启动不同的网络服务进程来处理这些用户请求。例子1,使用xinetd调用httpserver.py:httpserver.py#!/usr/bin/pythonimport sys request = ""while True:    data= sys.stdin.readline().strip()    request = request + data + "<br>"    if data == "":        print "HTTP/1.0 200 OK"        print ""        print "<html><body><p>" + request + "</p></body></html>"        sys.stdout.flush()        break1.chmod u+x httpserver.py2.在/etc/xinetd.d下创建httpserver(任意名字),进行设置:vim /etc/xinetd.d/httpserverservice 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  += USERID3.重启xinet.d是配置生效/etc/init.d/xinetd restart4.这时查看9991端口(即上面设置的port = 9991)是否打开[root@cai 11.27]# netstat -atnl|grep 9991tcp        0      0 0.0.0.0:9991                0.0.0.0:*5.打开http://192.168.220.2:9991/,能看到数据
例子2,使用xinetd调用errorserver.py:errorserver.py(注意:由于xinet.d设置了侦听51423端口,因此errorserver.py的s.bind((host,port))要注释掉,否则会报"address already in use"的错误)#!/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.py2.在/etc/xinetd.d下创建pythontestserver(任意名字),进行设置:vim /etc/xinetd.d/pythontestserverservice 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  += USERID3.重启xinet.d是配置生效/etc/init.d/xinetd restart4.这时查看51423端口(即上面设置的port = 51423)是否打开[root@cai 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            ESTABLISHED5.xinet.d配置好了,使用telnet连接51423端口。下面是运行结果[root@cai ~]# telnet localhost 51423Trying 127.0.0.1...Connected to localhost.localdomain (127.0.0.1).Escape character is "^]".Python向PHP发起GET与POST请求 http://www.linuxidc.com/Linux/2014-10/107903.htm《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htmPython脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htmPython 语言的发展简史 http://www.linuxidc.com/Linux/2014-09/107206.htmPython 的详细介绍:请点这里
Python 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-11/109925.htm