How to configure CGI server in Python?

I am running Python 3.2 on Windows. I want to run a simple CGI server on my machine for testing. Here is what I have 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. Then I ran the program and went to http: // localhost: 8000 / in my web browser, and the page was successfully displayed. Then 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 will display the full code above, and not just "Hello World!". How to get python to execute CGI code before serving it?

+3
2
+3

, cgi ; - (- !)

, , wsgi , HTTP. , wsgiref.

def application(environ, start_response):
    start_response([('content-type', 'text/html;charset=utf-8')])
    return ['<html><body><p>Hello World!</p></body></html>'.encode('utf-8')]

(, ):

import wsgiref.simple_server
server = wsgiref.simple_server.make_server('', 8000, application)
server.serve_forever()
+2

All Articles