Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—一种在控制台中进行交互式脚本执行的方法_Python_Django_Stdout_Stdin - Fatal编程技术网

Python—一种在控制台中进行交互式脚本执行的方法

Python—一种在控制台中进行交互式脚本执行的方法,python,django,stdout,stdin,Python,Django,Stdout,Stdin,我正在为Django migrate/makemigrations命令开发一个web界面。我一直在检查代码,他们使用Python输入来获得问题的答案 目前,我已经尝试使用Python子流程库,以便通过外部脚本回答运行Python脚本的Python脚本 我生成了一个简单的脚本,可以用作测试: sum.py 我发现让脚本运行第一个脚本的唯一方法是: from subprocess import Popen, PIPE command = ["python", "sum.py"] p = Popen

我正在为Django migrate/makemigrations命令开发一个web界面。我一直在检查代码,他们使用Python输入来获得问题的答案

目前,我已经尝试使用Python子流程库,以便通过外部脚本回答运行Python脚本的Python脚本

我生成了一个简单的脚本,可以用作测试:

sum.py

我发现让脚本运行第一个脚本的唯一方法是:

from subprocess import Popen, PIPE

command = ["python", "sum.py"]
p = Popen(command, stdin=PIPE, stdout=PIPE)
p.stdin.write("1\n")
p.stdin.write("2\n")
print p.communicate()[0]
几天前,我在互联网上找到了一些代码,可以让平安像实时标准一样接收:

from subprocess import Popen, PIPE

command = ["ping", "192.168.1.137"]
p = Popen(command, stdout=PIPE, stdin=PIPE, )
while p.poll() is None:
    print p.stdout.readline()
我已修改代码并尝试运行脚本:

192.168.1.137中的64字节:icmp_seq=7 ttl=64时间=1.655毫秒 从子流程导入Popen、PIPE

command = ["python", "suma.py"]
p = Popen(command, stdout=PIPE, stdin=PIPE, )
response = "1\n"
while p.poll() is None:
    print p.stdout.readline() #Obtain the message
    p.stdin.write(response) #Answer the question
我已经找到了一些可能的方法来使用WebSocket和node.js,比如or,但这可能很难实现,因为我对编程语言没有太多的了解

我发现的是接收来自stdout的消息并发送到HTML页面,然后从用户那里得到响应

我有点迷路了,任何建议都将不胜感激。
谢谢。

AFAIK这在纯Django中是不可能的:您需要一个事件服务器,如Twisted、Gevent或类似的服务器来实现这一点。像Django这样的传统web框架不能提供实时更新服务。您可以阅读任何聊天教程,这与聊天完全相同,只是您的消息来自何处以及在何处结束。无论使用Django框架,我甚至没有在terminal中实现这一点。:/感谢服务器的建议。
command = ["python", "suma.py"]
p = Popen(command, stdout=PIPE, stdin=PIPE, )
response = "1\n"
while p.poll() is None:
    print p.stdout.readline() #Obtain the message
    p.stdin.write(response) #Answer the question