我如何设置Python CGI服务器? [英] How do I set up a Python CGI server?
问题描述
我在Windows上运行Python 3.2。我想在我的机器上运行一个简单的CGI服务器用于测试目的。这是我迄今为止所做的:
我用下面的代码创建了一个python程序:
import http.server
import socketserver
PORT = 8000
Handler = http.server.CGIHTTPRequestHandler
httpd = socketserver.TCPServer((, PORT),Handler)
httpd.serve_forever()
在同一个文件夹中,我创建了index.html,一个简单的HTML文件。然后我运行该程序并在我的Web浏览器中访问 http:// localhost:8000 / ,页面成功显示。接下来,我在同一目录下创建了一个名为hello.py的文件,其代码如下:
import cgi
)输入cgitb
cgitb.enable()
print(Content-Type:text / html; charset = utf-8)
print()
print( < html>< body>< p> Hello World!< p>< / body>< / html>)
现在,如果我转到 http:// localhost:8000 / hello .py ,我的网页浏览器显示上面的完整代码,而不仅仅是Hello World!。如何让Python在执行CGI代码之前执行CGI代码?
请查看 docs for CGIHTTPRequestHandler ,它描述了如何确定哪些文件是CGI脚本。 p>
I'm running Python 3.2 on Windows. I want to run a simple CGI server on my machine for testing purposes. Here's what I've done so far:
I created a python program with the following code:
import http.server
import socketserver
PORT = 8000
Handler = http.server.CGIHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()
In the same folder, I created "index.html", a simple HTML file. I then ran the program and went to http://localhost:8000/ in my web browser, and the page displayed successfully. Next I made a file called "hello.py" in the same directory, with the following code:
import cgi
import cgitb
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print()
print("""<html><body><p>Hello World!</p></body></html>""")
Now if I go to http://localhost:8000/hello.py, my web browser displays the full code above instead of just "Hello World!". How do I make python execute the CGI code before serving it?
Take a look at the docs for CGIHTTPRequestHandler, which describe how it works out which files are CGI scripts.
这篇关于我如何设置Python CGI服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!