700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python如何运行一个python程序_在python中 如何运行一个命令行程序 它在发送Ctrl+D

python如何运行一个python程序_在python中 如何运行一个命令行程序 它在发送Ctrl+D

时间:2019-04-24 02:02:41

相关推荐

python如何运行一个python程序_在python中 如何运行一个命令行程序 它在发送Ctrl+D

由于没有人提供任何代码来帮助解决这个问题,我将做如下的事情。结果发现pexpect非常强大,而且您不需要signal模块。在import os

import sys

import pexpect

def run_server():

server_dir = '/path/to/server/root'

current_dir = os.path.abspath(os.curdir)

os.chdir(server_dir)

server_call = pexpect.spawn('atlas-run')

server_response = server_call.expect(['Server Error!', 'Sever is running!'])

os.chdir(current_dir)

if server_response:

return server_call #return server spawn object so we can shutdown later

else:

print 'Error starting the server: %s'%server_response.after

sys.exit(1)

def run_unittests():

# several ways to do this. either make a unittest.TestSuite or run command line

# here is the second option

unittest_dir = '/path/to/tests'

pexpect.spawn('python -m unittest discover -s %s -p "*test.py"'%unittest_dir)

test_response = pexpect.expect('Ran [0-9]+ tests in [0-9\.]+s') #catch end

print test_response.before #print output of unittests before ending.

return

def main():

server = run_sever()

run_unittests()

server.sendcontrol('d') #shutdown server

if __name__ == "__main__":

main()

python如何运行一个python程序_在python中 如何运行一个命令行程序 它在发送Ctrl+D之前不会返回...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。