xinetd服务调用python脚本(无法正确执行) [英] xinetd service calls python script (doesn't execute properly)

查看:148
本文介绍了xinetd服务调用python脚本(无法正确执行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了这篇文章:


您可以使用xinetd添加服务来启动python脚本。标准输入和输出将通过网络在所需的端口上传输,因此您无需修改​​脚本(input / raw_input和print方法可以正常工作)。

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).

$ b结果,当建立与192.168.240.37:65123的TCP连接时,我正在使用自定义的xinet服务来启动script.py。该行为不是期望的/预期的。

As a result, I'm using a custom xinet service to launch script.py when a TCP connection is established to 192.168.240.37:65123. The behavior is not as 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

远程登录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]# 

当我说很难说出这里到底发生了什么时,我的意思是很难说在上面的输出中是否显示 John Smith(在输入您的名字:之后出现)提示是打印的结果语句,很难判断外部主机关闭的连接是否是quit()语句的结果。

When I say 'hard to tell exactly what happened here', I mean it's hard to tell if in the above output the 'John Smith' which shows up after the 'Enter your name: ' prompt is a result of the print statement and hard to tell if the 'Connection closed by foreign host' is a result of the quit() statement.

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的行为非常相似。

As you can see I get very similar behavior with netcat.

推荐答案

对我来说,真正的问题显然是我缺乏套接字编程知识。我在网上搜索 xinetd python no data( https://mail.python.org/pipermail/python-list/2007-July/423659.html ),这至少帮助我使我的工作正常了,并通过示例说明了套接字编程的实际方式作品。解决方案如下,我从原始脚本中删除了raw_input并从帖子中替换了概念。

The real problem for me is apparently my lack of socket programming knowledge. I found this post online searching for "xinetd python no data" (https://mail.python.org/pipermail/python-list/2007-July/423659.html) which helped me at least get my stuff working and sort of illustrated via example a bit about how socket programming actually works. The solution is below, I removed the raw_input from the original script and replaced it with concepts from the 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.

这篇关于xinetd服务调用python脚本(无法正确执行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆