![](/img/trans.png)
[英]running a django app on apache with SSL: pexpect spawn/expect doesn't work
[英]Running pexpect from apache httpd
我正在從Python生成HTML頁面。 還有一種邏輯,可以使用pexpect生成SSH會話並在同一Python代碼中獲取命令輸出。 但是,當我從Apache httpd服務器運行Python時,它給了我500 internal server error
。 但是單獨執行Python代碼效果很好。
不確定是Python還是Apache?
代碼在下面,我為調試目的添加了異常。 異常顯示
Exception seen in Web page :
Error! pty.fork() failed: out of pty devices name
'child' is not defined name
'child' is not defined name
'child' is not defined name
'child' is not defined name
'child' is not defined name
'child' is not defined name
'child' is not defined name
Code is below #############################################################
import pexpect
import sys
import time
import cgi, cgitb
import getpass
print "Content-Type: text/html\n\n"
try:
child = pexpect.spawn('ssh -t admin@192.***.***.*** login root')
except Exception, e:
print e
try:
child.expect('(?i)password')
except Exception, e:
print e
try:
child.sendline('password')
except Exception, e:
print e
try:
child.expect('(?i)Password:')
except Exception, e:
print e
try:
child.sendline('password')
except Exception, e:
print e
try:
child.expect('-bash# ')
except Exception, e:
print e
try:
child.sendline('ls -al')
except Exception, e:
print e
try:
child.expect('-bash# ')
except Exception, e:
print e
output = child.before
print "Content-Type: text/html\n\n"
print "<html>"
print "<head>"
print "<title>Hello </title>"
print "</head>"
print "<body>"
print "<h1>",output,"</h1>"
print "</body>"
print "</html>"
子變量在第一個try塊的范圍內定義。 當它超出第一個try塊的范圍時,解釋器將不知道它。 您可以通過將所有try塊合並為一個來解決此問題。 夠了。
嘗試使用以下代碼段:
#!/usr/bin/env python
import pexpect
import sys
import time
import cgi, cgitb
import getpass
output = ""
try:
child = pexpect.spawn('ssh -t admin@192.***.***.*** login root')
child.expect('(?i)password')
child.sendline('password')
child.expect('(?i)Password:')
child.sendline('password')
child.expect('-bash# ')
child.sendline('ls -al')
child.expect('-bash# ')
output = child.before
except Exception, e:
print e
print "Content-Type: text/html\n\n"
print "<html>"
print "<head>"
print "<title>Hello </title>"
print "</head>"
print "<body>"
print "<h1>",output,"</h1>"
print "</body>"
print "</html>"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.