Я прочитал это:
Вы можете использовать xinetd, чтобы добавить службу, запускающую ваш скрипт Python. Стандартный ввод и вывод будут передаваться по сети на нужный порт, поэтому вам не нужно модифицировать ваши скрипты (методы ввода/raw_input и печати будут работать нормально).
В результате я использую специальную службу xinet для запуска script.py, когда установлено TCP-соединение с адресом 192.168.240.37:65123. Поведение не соответствует желаемому/ожидаемому.
/root/script.py
#! /usr/bin/python
my_name = raw_input("Enter your name: ")
print my_name
quit()
/etc/xinetd.d/netunique-сервер
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 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]#
Когда я говорю «трудно точно сказать, что здесь произошло», я имею в виду, что трудно сказать, является ли в приведенном выше выводе «Джон Смит», который появляется после приглашения «Введите ваше имя:», результатом оператора печати и жесткого чтобы узнать, является ли «Соединение закрыто внешним хостом» результатом оператора 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.