xinted(eXtended InterNET daemon)即網絡守護進程。xinetd能夠同時監聽多個指定的端口,在接受用戶請求時,它能夠根據用戶請求的端口的不同,啟動不同的網絡服務進程來處理這些用戶請求。
例子1,使用xinetd調用httpserver.py:
httpserver.py
#!/usr/bin/python
import 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()
break
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/,能看到數據
例子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向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.htm
Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm
在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm
Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm
Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡