xinetd 服务调用 python 脚本(未正确执行)
xinetd service calls python script (doesn't execute properly)
我读了这个:
You can use xinetd to add a service starting your python script. The standard input and output will be transmitted over the network on desired port, so you do not need to modify your scripts (input/raw_input and print methods will work fine).
因此,当与 192.168.240.37:65123 建立 TCP 连接时,我使用自定义 xinet 服务启动 script.py。行为不像 desired/expected.
/root/script.py
#! /usr/bin/python
my_name = raw_input("Enter your name: ")
print my_name
quit()
/etc/xinetd.d/netunique-server
service netunique
{
disable = no
id = netunique-server
type = unlisted
wait = no
socket_type = stream
protocol = tcp
user = root
server = /usr/bin/python
server_args = /root/script.py
port = 65123
flags = IPv4 REUSE
bind = 192.168.240.37
}
systemctl status xinetd
Nov 11 21:24:00 netunique.ourhome.com xinetd[2161]: xinetd Version 2.3.15 started with libwrap loadavg labeled-ne... in.
Nov 11 21:24:00 netunique.ourhome.com xinetd[2161]: Started working: 1 available service
telnet 192.168.240.37 65123(预期行为)
[root@netunique xinetd.d]# telnet 192.168.240.37 65123
Trying 192.168.240.37...
Connected to 192.168.240.37.
Escape character is '^]'.
Enter your name: John Smith <-- I type name after prompt here
John Smith <-- Script prints entry back to screen
Connection closed by foreign host. <-- Script does its quit()
telnet 192.168.240.37 65123(实际行为)
[root@netunique xinetd.d]# telnet 192.168.240.37 65123
Trying 192.168.240.37...
Connected to 192.168.240.37.
Escape character is '^]'.
<blank> <-- no prompt as expected
John Smith <-- I enter some data and hit Enter
Enter your name: John Smith <-- Hard to tell exactly what happened here
Connection closed by foreign host. <-- Hard to tell exactly what happened here
[root@netunique xinetd.d]#
当我说 'hard to tell exactly what happened here' 时,我的意思是很难判断在上面的输出中 'Enter your name: ' 提示后出现的 'John Smith' 是否是 print 语句的结果,并且很难判断 'Connection closed by foreign host' 是否是 quit() 语句的结果。
nc 192.168.240.37 65123(实际行为)
[root@netunique xinetd.d]# nc 192.168.240.37 65123
<blank> <-- no prompt as expected
John Smith <-- I enter some data and hit Enter
Enter your name: John Smith <-- Hard to tell exactly what happened here
<-- Nothing happened here, I hit Enter
<-- I hit Enter again
Ncat: Broken pipe. <-- This is the end result
如您所见,我的行为与 netcat 非常相似。
对我来说真正的问题显然是我缺乏套接字编程知识。我发现这个 post 在线搜索 "xinetd python no data" (https://mail.python.org/pipermail/python-list/2007-July/423659.html) 这至少帮助我让我的东西工作并且通过示例说明了一些关于套接字编程的实际工作方式。解决方案如下,我从原始脚本中删除了 raw_input 并将其替换为 post.
中的概念
/root/script.py(原创)
#! /usr/bin/python
my_name = raw_input("Enter your name: ")
print my_name
quit()
/root/script.py(修改和工作)
#! /usr/bin/python
import sys
print "Enter your name:"
sys.stdout.flush()
my_name = sys.stdin.readline().strip()
print "Your name is %s" % my_name
sys.stdout.flush()
quit()
telnet 192.168.240.37 65123(实际行为 - 工作)
[root@netunique ~]# telnet 192.168.240.37 65123
Trying 192.168.240.37...
Connected to 192.168.240.37.
Escape character is '^]'.
Enter your name:
Bob Smith
Your name is Bob Smith
Connection closed by foreign host.
我读了这个:
You can use xinetd to add a service starting your python script. The standard input and output will be transmitted over the network on desired port, so you do not need to modify your scripts (input/raw_input and print methods will work fine).
因此,当与 192.168.240.37:65123 建立 TCP 连接时,我使用自定义 xinet 服务启动 script.py。行为不像 desired/expected.
/root/script.py
#! /usr/bin/python
my_name = raw_input("Enter your name: ")
print my_name
quit()
/etc/xinetd.d/netunique-server
service netunique
{
disable = no
id = netunique-server
type = unlisted
wait = no
socket_type = stream
protocol = tcp
user = root
server = /usr/bin/python
server_args = /root/script.py
port = 65123
flags = IPv4 REUSE
bind = 192.168.240.37
}
systemctl status xinetd
Nov 11 21:24:00 netunique.ourhome.com xinetd[2161]: xinetd Version 2.3.15 started with libwrap loadavg labeled-ne... in.
Nov 11 21:24:00 netunique.ourhome.com xinetd[2161]: Started working: 1 available service
telnet 192.168.240.37 65123(预期行为)
[root@netunique xinetd.d]# telnet 192.168.240.37 65123
Trying 192.168.240.37...
Connected to 192.168.240.37.
Escape character is '^]'.
Enter your name: John Smith <-- I type name after prompt here
John Smith <-- Script prints entry back to screen
Connection closed by foreign host. <-- Script does its quit()
telnet 192.168.240.37 65123(实际行为)
[root@netunique xinetd.d]# telnet 192.168.240.37 65123
Trying 192.168.240.37...
Connected to 192.168.240.37.
Escape character is '^]'.
<blank> <-- no prompt as expected
John Smith <-- I enter some data and hit Enter
Enter your name: John Smith <-- Hard to tell exactly what happened here
Connection closed by foreign host. <-- Hard to tell exactly what happened here
[root@netunique xinetd.d]#
当我说 'hard to tell exactly what happened here' 时,我的意思是很难判断在上面的输出中 'Enter your name: ' 提示后出现的 'John Smith' 是否是 print 语句的结果,并且很难判断 'Connection closed by foreign host' 是否是 quit() 语句的结果。
nc 192.168.240.37 65123(实际行为)
[root@netunique xinetd.d]# nc 192.168.240.37 65123
<blank> <-- no prompt as expected
John Smith <-- I enter some data and hit Enter
Enter your name: John Smith <-- Hard to tell exactly what happened here
<-- Nothing happened here, I hit Enter
<-- I hit Enter again
Ncat: Broken pipe. <-- This is the end result
如您所见,我的行为与 netcat 非常相似。
对我来说真正的问题显然是我缺乏套接字编程知识。我发现这个 post 在线搜索 "xinetd python no data" (https://mail.python.org/pipermail/python-list/2007-July/423659.html) 这至少帮助我让我的东西工作并且通过示例说明了一些关于套接字编程的实际工作方式。解决方案如下,我从原始脚本中删除了 raw_input 并将其替换为 post.
中的概念/root/script.py(原创)
#! /usr/bin/python
my_name = raw_input("Enter your name: ")
print my_name
quit()
/root/script.py(修改和工作)
#! /usr/bin/python
import sys
print "Enter your name:"
sys.stdout.flush()
my_name = sys.stdin.readline().strip()
print "Your name is %s" % my_name
sys.stdout.flush()
quit()
telnet 192.168.240.37 65123(实际行为 - 工作)
[root@netunique ~]# telnet 192.168.240.37 65123
Trying 192.168.240.37...
Connected to 192.168.240.37.
Escape character is '^]'.
Enter your name:
Bob Smith
Your name is Bob Smith
Connection closed by foreign host.