xinetd service calls python script (doesn't execute properly)

依然范特西╮ 提交于 2019-12-02 08:52:24

问题


I read this:

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

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

telnet 192.168.240.37 65123 (expected behavior)

[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 (actual behavior)

[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]# 

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 (actual behavior)

[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

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


回答1:


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 (original)

#! /usr/bin/python
my_name = raw_input("Enter your name: ")
print my_name
quit()

/root/script.py (modified & working)

#! /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 (actual behavior - working)

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


来源:https://stackoverflow.com/questions/47244913/xinetd-service-calls-python-script-doesnt-execute-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!