我正在從Python生成HTML頁面。 也有用於在相同的Python代碼中使用pexpect和fetching命令輸出產生SSH會話的邏輯。但是當我從Apache httpd服務器運行Python時,它給了我500 internal server error
。 但分開執行Python代碼工作正常。從apache httpd運行pexpect
不確定問題出在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 [email protected]***.***.*** 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>"
捕捉並顯示您的Pexpect的代碼的所有異常。這可能是你的ssh會話不能用作apache www-data用戶。例如,它通常沒有〜/ .ssh/*文件。 – tdelaney
我正在通過將它保存在/ var/www/cgi-bin文件夾中來執行此腳本。調用它爲localhost/cgi-bin/test1.py plain cgi 「來自腳本的格式不正確的頭文件錯誤的頭文件= name'child'未定義:test1.py」,在error_log中顯示child是會話產生的處理程序 – NoviceInPython
你能編輯你的文章並粘貼你的腳本代碼? – ylabidi