Hands on Python - Problem with Local Cgi Server

Gary Wood pythonsky at sky.com
Tue Mar 31 14:56:48 CEST 2009

I have the DOS box with the message 
Localhost CGI server started 

But when i try this 
Back in the www directory, 

  1.. Open the web link http://localhost:8080/adder.html (preferably in a new window, separate from this this tutorial). 
  2.. You should see an adder form in your browser again. Note that the web address no longer includes 'cs.luc.edu'. Instead it starts with 'localhost:8080', to reference the local Python server you started. Fill out the form and test it as before. 
  3.. Look at the console window. You should see a log of the activity with the server. Close the server window. 
  4.. Reload the web link http://localhost:8080/adder.html. You should get an error, since you refer to localhost, but you just stopped the local server.

I get the Windows Error 

Failed to Connect

  The connection was refused when attempting to contact localhost:8080.

  Though the site seems valid, the browser was unable to establish a connection.

    *  Could the site be temporarily unavailable? Try again later.

    *  Are you unable to browse other sites?  Check the computer's network connection.

    *  Is your computer or network protected by a firewall or proxy? Incorrect settings can interfere with Web browsing.

The py file brings up the DOS box as if its running ok 

'''Run a local cgi server from the current directory that treats *.cgi files
as executable python cgi scripts.'''

import http.server, sys, os

class CGIExtHTTPRequestHandler(http.server.CGIHTTPRequestHandler):
    '''This request handler mimics the Loyola server, which looks for CGI files
    to end in '.cgi' and be in any directory as opposed to the CGIHTTPServer
    expectation that the cgi script are of the form /cgi-bin/*.py.'''
    def is_python(self, path):
        """Test whether argument path is a Python script: allow .cgi"""
        return path.lower().endswith('.cgi')

    def is_cgi(self):
        '''As on xenon, go by extension only.'''
        base = self.path
        query = ''
        i = base.find('?')
        if i != -1:
            query = base[i:]
            base = base[:i]
        if not base.lower().endswith('.cgi'):
            return False
        [parentDirs, script] = base.rsplit('/', 1)
        self.cgi_info = (parentDirs, script+query)
        return True

def run_server():
    dirName = os.getcwd()
    blanks = dirName.count(' ')
    if 0 < blanks:  # server cannot handle blanks in path names
        print("""The path to this directory contains {blanks} space(s):
Either rename directories to remove the blanks or
move this directory to a place with no blanks in the path.
Aborting the local server run!""".format(**locals()))
        input("Press return after reading this message.")
    server_addr = ('localhost', 8080)
    cgiServer = http.server.HTTPServer(server_addr, CGIExtHTTPRequestHandler)
    sys.stderr.write('Localhost CGI server started\n.')



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090331/bd1a9604/attachment.html>

More information about the Python-list mailing list