cgi 文件 .py 不执行;改为显示文件
cgi file .py not exexuting; file gets displayed instead
我在 python 中创建了一个网络服务器。
server.py
import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
if sys.argv[1:]:
port = int(sys.argv[1])
else:
port = 8000
server_address = ('127.0.0.1', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
和一个文件 form.html
<html>
<title>FORM</title>
<body>
<form id="form1" method="GET" action="/var/www/cgi-bin/formaction.py">
Name : <input type="text" name="username">
Password : <input type="password" name="password">
<input type="submit">
</form>
</body>
</html>
和formaction.py
import cgi, cgitb
cgitb.enable()
form = cgi.FieldStorage()
username = form.getvalue('username');
password = form.getvalue('password');
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (username, password)
print "</body>"
print "</html>"
当我提交表单时,cgi 文件按原样显示而不是执行。如何让 python 脚本执行。
目录结构如下:
要 运行 python 中的 CGI 脚本,您需要 运行 CGI 服务器而不是 HTTP 服务器。
尝试在命令 promt 中键入以下内容:
python -m CGIHTTPServer
或尝试以下脚本:
import BaseHTTPServer
import CGIHTTPServer
import cgitb;
cgitb.enable() ## This line enables CGI error reporting
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
PORT = 8000
server_address = ("", PORT)
handler.cgi_directories = ["/cgi-bin"]
httpd = server(server_address, handler)
print 'Server running on 127.0.0.1:8000...'
httpd.serve_forever()
我在 python 中创建了一个网络服务器。
server.py
import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
if sys.argv[1:]:
port = int(sys.argv[1])
else:
port = 8000
server_address = ('127.0.0.1', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
和一个文件 form.html
<html>
<title>FORM</title>
<body>
<form id="form1" method="GET" action="/var/www/cgi-bin/formaction.py">
Name : <input type="text" name="username">
Password : <input type="password" name="password">
<input type="submit">
</form>
</body>
</html>
和formaction.py
import cgi, cgitb
cgitb.enable()
form = cgi.FieldStorage()
username = form.getvalue('username');
password = form.getvalue('password');
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (username, password)
print "</body>"
print "</html>"
当我提交表单时,cgi 文件按原样显示而不是执行。如何让 python 脚本执行。
目录结构如下:
要 运行 python 中的 CGI 脚本,您需要 运行 CGI 服务器而不是 HTTP 服务器。 尝试在命令 promt 中键入以下内容:
python -m CGIHTTPServer
或尝试以下脚本:
import BaseHTTPServer
import CGIHTTPServer
import cgitb;
cgitb.enable() ## This line enables CGI error reporting
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
PORT = 8000
server_address = ("", PORT)
handler.cgi_directories = ["/cgi-bin"]
httpd = server(server_address, handler)
print 'Server running on 127.0.0.1:8000...'
httpd.serve_forever()