Page 271 -
P. 271
web development
CGI lets your web server run programs
I’m all fired up and
The Common Gateway Interface (CGI) is an Internet standard that allows for ready to go! I live to
a web server to run a server-side program, known as a CGI script. serve-up HTML and
Typically, CGI scripts are placed inside a special folder called cgi-bin, so run CGIs...
that the web server knows where to find them. On some operating systems
(most notably UNIX-styled systems), CGI scripts must be set to executable
before the web server can execute them when responding to a web request.
CGI
Web
More on this in So...to run my webapp, Server
a little bit. I need a web server with
CGI enabled.
All webapps need to run on web servers.
Practically every web server on the planet supports CGI. Whether
your running Apache, IIS, nginx, Lighttpd, or any of the others, they
all support running CGI scripts written in Python.
But using one of these tools here is overkill. There’s no way the
coach is going to agree to download, unpack, install, configure,
and manage one of these industry heavyweights.
As luck would have it, Python comes with its very own web server,
included in the http.server library module. Check the
contents of the webapp.zip download: it comes with a CGI-
enabled web server called simplehttpd.py.
Here are the five lines of code needed to
Import the HTTP build a web server in Python.
server and CGI
modules.
from http.server import HTTPServer, CGIHTTPRequestHandler
Specify a port.
Create a port = 8080
HTTP server. httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
Display a friendly print("Starting simple_httpd on port: " + str(httpd.server_port))
message and start httpd.serve_forever()
your server.
you are here 4 235