Simple CGIHTTPServer and Client in Python 3k

Python 3k - CGIHTTPRequestHandler example.


import http.server
import http.server

class Handler(http.server.CGIHTTPRequestHandler):
cgi_directories = ["/cgi"]

server = http.server.HTTPServer(("",8000),Handler)
server.serve_forever()


Run this server.

Create a Directory 'cgi' in the directory you have the above server script and place the following python cgi code.

filename: test.py

#!/usr/local/bin/python3.1

import cgitb;cgitb.enable()

print("Content-type: text/html")
print()
print("<title>CGI 101</title>")
print("<h1>First CGI Example</h1>")
print("<p>Hello, CGI World!</p>")



Open the Browser and point to http://localhost:8000/cgi/test.py

1 comment:

SIDH said...

Create a bash script named pws-cgi:

#!/usr/bin/env bash
nohup python -m CGIHTTPServer 80 > /dev/null &

Make it executable:

chmod 700 pws-cgi

Run the script:
./pws-cgi

create cgi-bin on the current dir:
mkdir cgi-bin

Place any cgi script inside cgi-bin and access from browser. It also support any static files (you have to place those on the current dorectory only). That's it