2015-05-11 27 views
3

我有提供简单的静态页面非常简单的金字塔应用。比方说,它的名字是mypyramid和使用端口9999为什么我的简单应用金字塔无法与Pexpect的工作吗?


如果我在另一个Linux控制台手动启动我的金字塔,然后我可以使用下面的代码打印出来的HTML字符串。

if __name__ == "__main__": 
    import urllib2 
    print 'trying to download url' 
    response = urllib2.urlopen('http://localhost:9999/index.html') 
    html = response.read() 
    print html 

但我想在一个应用程序自动启动我的金字塔

所以在我的其他应用程序,我用pexpect推出我的金字塔,然后尝试从http://localhost:9999/index.html获取HTML字符串。

def _start_mypyramid(): 
    p = pexpect.spawn(command='./mypyramid') 
    return p 

if __name__ == "__main__": 
    p = _start_mypyramid() 
    print p 
    print 'mypyramid started' 
    import urllib2 
    print 'trying to download url' 
    response = urllib2.urlopen('http://localhost:9999/index.html') 
    html = response.read() 
    print html 

看来我的金字塔已使用pexpect成功发射,我可以看到进程的打印和mypyramid started已经达到。

但是,应用程序只是trying to download url后,我不能得到任何东西。


什么是解决方案?我的意思是我认为pexpect会创建另一个过程。如果这是真的,那为什么它会停止检索html?

回答

0

我的猜测是,通过pexpect.spawn返回的孩子需要沟通。 它试图写,但没有人读的,所以应用程序停止。 (我只是猜测)。

如果你没有任何理由使用Pexpect的(你可能没有,如果你不与孩子沟通的过程),你为什么不只是去一个标准模块子?