1

I am a noob; trying to create and use a simple webserver in Python that executes CGI scripts written in Python. I am using Windows XP and Python v3.3.0. I have a "myserver" directory which contains "myserver.py","sample.html" and the directory "cgi-bin" which in turn contains "cgi_demo.py"

myserver.py

from http.server import HTTPServer
from http.server import CGIHTTPRequestHandler

port = 8080
host = '127.0.0.1'
server_address = (host,port) 
httpd = HTTPServer(server_address,CGIHTTPRequestHandler)
print("Starting my web server on port "+str(port))
httpd.serve_forever()

cgi_demo.py

import cgi
import cgitb; cgitb.enable()

print("Content-type: text/html")
print
print("<html><body>")
for i in range(0,100):
  print(i,"<br>")
print("</body></html>")

Now the directory listing works fine for "myserver" but not for "cgi-bin"; maybe that is how it is coded - I don't have a problem here. "sample.html" is retrieved fine too. However, the execution of "cgi_demo.py" is not proper. I get a blank page in the browser; and the console window (which is blank too) appears and disappears. Moreover, on the server's console I get the message

127.0.0.1 - - [29/Nov/2012 12:00:31] "GET /cgi-bin/cgi_demo.py HTTP/1.1" 200 -
127.0.0.1 - - [29/Nov/2012 12:00:31] command: C:\Python33\python.exe -u "D:\python apps\my web server\cgi-bin\cgi_demo.py" ""
127.0.0.1 - - [29/Nov/2012 12:00:32] CGI script exited OK

Please tell me what is wrong! I get the feeling that the output stream of my script is not connected to the server. What am I doing wrong? Don't say that I have to extend CGIHTTPRequestHandler!!

Jazz
  • 639
  • 1
  • 11
  • 28

1 Answers1

2

SORRY for the trouble!

Well, it is my fault. 2 things to note:

[1]The console window that appeared and disappeared; it only happens when I use IDLE to execute the server. If the script is already running in a normal windows console then this does not happen. My Feeling was WRONG.

[2]There is an bug/error in my cgi script. After printing the HTTP header; the print statement that I wrote was just "print" instead of actually being "print()".This is so embarrassing! But, even then why didn't the interpreter catch this error?

Jazz
  • 639
  • 1
  • 11
  • 28
  • 1
    On [2]: I think that is one of them ost annoying python errors ever. Happened to me as well but with f.close. The reason that the interpreter does not flag this, is probably because a reference to an object of type function is a valid statement although it accomplishes nothing. – er4z0r Aug 04 '13 at 20:57