Python写的CGI脚本返回到Web页面的数据为None

孤家寡人丶丶 2019-09-02 11:06:18
apache服务下采用CGI做一个前后端交互的web操作界面

这是python脚本写的CGI程序 目的是登入telnet 然后输入命令 得到结果后把结果返回给web页面 脚本在终端上执行能得到想要的结果,但是现在页面返回的是None有没有懂的大佬帮忙看下原因出在哪里呢

#!/usr/bin/env python
#coding=utf-8
import os
import os.path
import re
import time
import sys
import stat
import commands
import telnetlib
import cgi
import cgitb

Host = '127.0.0.1'

def do_telnet_cmd(host, cmd):
try:
tn = telnetlib.Telnet(host, port=2019, timeout=10)
tn.set_debuglevel(0)
except:
return None

tn.write('config dde' + '\n')
tn.write('stat' + '\n')
tn.write('show' + ' ' + cmd + '\n')
time.sleep(1)

string = tn.read_very_eager()
result = string.split('\r\n')
for i in range(7):
result.pop(0)
result.pop()
print result
tn.close()

return result

def do_print_web(ret):
print "Content-type:text/html"
print
print "<html>"
print "<head>"
print "<meta charset=\"utf-8\">"
print "<title>dde</title>"
print "</head>"
print "<body>"
print "<pre>%s</pre>" % ret
print "</body>"
print "</html>"

if __name__=='__main__':
form = cgi.FieldStorage()
if form.getvalue('select'):
cmd = form.getvalue('select')
else:
cmd = sys.argv[1]
ret = do_telnet_cmd(Host, cmd)
do_print_web(ret)
...全文
280 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
孤家寡人丶丶 2019-09-02
  • 打赏
  • 举报
回复
自己顶一下 来个大佬帮忙看看原因

2,203

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 CGI
社区管理员
  • CGI社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧