2

I'm wanting to run a very simply python based server in a windows environment, to test CGI scripts. I want to do this in windows, but run ultimately in a unix environment. So far, I have a simple server, and a simple program. When I go to the site, I am seeing a blank page, with nothing in the source, and I can't figure out what's going on.

Server

from http.server import HTTPServer, CGIHTTPRequestHandler

class Handler(CGIHTTPRequestHandler):
    cgi_directories = ["/cgi-bin"]

PORT = 8080

httpd = HTTPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()
httpd.serve_forever()

App:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import cgi, cgitb
form = cgi.FieldStorage() 

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print ("Content-type:text/html\r\n\r\n")
print ("<html>")
print ("<head>")
print ("<title>Hello - Second CGI Program</title>")
print ("</head>")
print ("<body>")
print ("<h2>Hello %s %s</h2>" % (first_name, last_name))
print ("</body>")
print ("</html>")

I go to the program in my web browser, and I'm getting a downloadable output, with it being the name of the file. It appears the file has executed, but I'm not getting a valid web site. What am I doing wrong?

3
  • 1
    Any reason not to use something simple like bottle? Commented Jan 4, 2013 at 16:18
  • @fp: Shear laziness. I just don't want to install something else for the moment... Commented Oct 8, 2013 at 17:59
  • Hi! I am attempting to do something like you are describing: > to run a very simply python based server in a windows environment, to test CGI scripts. I want to do this in windows But I am new to CGI and webserver programming. I copied your code, in a folder I have to script, a server.py (server, and a test_CGHI.py (App). To test it I go to localhost:8080, I can see the file in the folder and when I click on the app (test_CGI.py), I can see it is content but it is not executing, what am I doing wrong? Commented Mar 23, 2016 at 14:22

1 Answer 1

4

Of course I figured out the answer just as I posted it... I should just use a \n\n, not \r\n\r\n.

print ("Content-Type: text/html\n\n")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.