How to run CGI "hello world" using python http.server

I am using Windows 7 and Python 3.4.3. I would like to run this simple helloworld.py file in my browser:

print('Content-Type: text/html') print( '<html>') print( '<head></head>') print( '<body>') print( '<h2>Hello World</h2>') print( '</body></html>') 

What am I doing:

1) Go to the command line C:\Python (where python is installed)

2) run: python -m http.server

3) Get Firefox and enter http://localhost:8000/hello.py

However, instead of "Hello World," the browser simply prints the contents of the hello.py file.

How can i fix this?

+8
python cgi
source share
3 answers

From http.server docs :

CGIHTTPRequestHandler can be enabled on the command line by passing the --cgi :

 $ python3 -m http.server --bind localhost --cgi 8000 

Put your script in cgi_directories :

This default value is ['/cgi-bin', '/htbin'] and describes the directories to be processed as containing CGI scripts.

Open in browser:

 $ python -mwebbrowser http://localhost:8000/cgi-bin/hello.py 

where hello.py :

 #!/usr/bin/env python3 print("Content-Type: text/html\n") print("<!doctype html><title>Hello</title><h2>hello world</h2>") 

I had to make it executable on POSIX: chmod +x cgi-bin/hello.py .

+10
source

I did it a while ago for Python2.7

 from BaseHTTPServer import BaseHTTPRequestHandler class GetHandler(BaseHTTPRequestHandler): def do_HEAD(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() def do_GET(self): x = self.wfile.write self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() # <--- HTML starts here ---> x("<html>") # <--- HEAD starts here ---> x("<head>") x("<title>Title goes here!</title>") x("</head>") # <--- HEAD ends here ---> # <--- BODY starts here ---> x("<body>") x("<p>This is a test.</p>") x("</body>") # <--- BODY ends here ---> x("</html>") # <--- HTML ends here ---> if __name__ == '__main__': from BaseHTTPServer import HTTPServer server = HTTPServer(('localhost', 777), GetHandler) print 'Starting server, use <Ctrl + F2> to stop' server.serve_forever() 

So in Python 3 you just need to change the import

 from http.server import BaseHTTPRequestHandler class GetHandler(BaseHTTPRequestHandler): def do_HEAD(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() def do_GET(self): x = self.wfile.write self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() # <--- HTML starts here ---> x("<html>") # <--- HEAD starts here ---> x("<head>") x("<title>Title goes here!</title>") x("</head>") # <--- HEAD ends here ---> # <--- BODY starts here ---> x("<body>") x("<p>This is a test.</p>") x("</body>") # <--- BODY ends here ---> x("</html>") # <--- HTML ends here ---> if __name__ == '__main__': from http.server import HTTPServer server = HTTPServer(('localhost', 777), GetHandler) print('Starting server, use <Ctrl + F2> to stop') server.serve_forever() 

I don't know right now if the print function in python 3

 print("") 

or

 print "" 

but I think it was with ()

Edit: this is print ()

+1
source

I created a complete example for a friend. This is a complete demo that you can run with 8 simple, ready-to-copy lines of code. Enjoy it.

 echo -e "\n\n Usage: after running this script, visit http://localhost:8000/cgi-bin/hello \n\n" mkdir /tmp/cgi-bin/ cat > /tmp/cgi-bin/hello <<EOF #!/bin/bash echo -e "Content-Type: text/plain\n\n"; date; echo; env EOF chmod +x /tmp/cgi-bin/hello (cd /tmp; python3 -m http.server --cgi 8000) 
0
source

All Articles