Display python script output to HTML page

谁都会走 提交于 2020-01-23 03:52:04

问题


I have this hello_world.py script which takes username and password and prints user information. Separately the python executes without any error. But, when I submit from HTML the html page gives internal server 500 error. I have added executable permission to script in file system, in httpd.conf. But, I am not sure why this is not executing when I provide submit.

Thanks for suggestions and help.

<form action="/cgi-bin/hello_world.py" method="post">
   IP Address: <input type="text" name="user_name"><br />
   Password: <input type="text" name="pass_word" />
   <input type="submit" value="Submit" />
</form>

My python script is

#!/usr/bin/python

# Import modules for CGI handling 
import cgi, cgitb
import getpass
import sys
import telnetlib


# Create instance of FieldStorage 
form = cgi.FieldStorage()

# Get data from fields
user_name = form.getvalue('user_name')
//username will get IP of machine
pass_word  = form.getvalue('pass_word')

loginid = 'admin'

tc = telnetlib.Telnet(user_name)
tc.read_until("login: ")
tc.write(loginid + "\n")
tc.read_until("Password: ")
tc.write(pass_word + "\r\n")

tc.write("ls -l\n")
tc.write("exit\n")


print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello </title>"
print "</head>"
print "<body>"
print "<h2> %s is IP  and  %s is passowrd</h2>" % (user_name, pass_word)
print tc.read_all()
print "</body>"
print "</html>"

来源:https://stackoverflow.com/questions/22641530/display-python-script-output-to-html-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!