xinetd服务调用python脚本(无法正确执行)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了xinetd服务调用python脚本(无法正确执行),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3475字,纯文字阅读大概需要5分钟。
内容图文
![xinetd服务调用python脚本(无法正确执行)](/upload/InfoBanner/zyjiaocheng/692/338e1ae9b3f14d52b2ebeb306c035b31.jpg)
我读到这个:
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.该行为不是期望/预期的.
/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状态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]#
当我说“很难说出这里到底发生了什么”时,我的意思是很难说在上面的输出中是否显示“ John Smith”(出现在“输入您的名字:”之后)提示是打印语句的结果,并且很难判断“外部主机关闭的连接”是否是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的行为非常相似.
解决方法:
对我来说,真正的问题显然是我缺乏套接字编程知识.我发现此帖子在线搜索“ xinetd python no data”(https://mail.python.org/pipermail/python-list/2007-July/423659.html),这至少帮助我起了作用,并通过示例举例说明了套接字编程的实际工作方式.解决方案如下,我从原始脚本中删除了raw_input并用文章中的概念替换了它.
/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脚本(无法正确执行)全部内容,希望文章能够帮你解决xinetd服务调用python脚本(无法正确执行)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。