Thread: Web Server

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    11
    Rep Power
    0

    Web Server


    I'm new to Python, but good at Perl. I've learned that python is capable of creating web servers. I've been looking at this example server as well as the module documentation. I've figured out how to change it to load index.html if you just enter localhost. According to the documentation for SimpleHTTPServer, there is a way to list all the files in a directory using this:

    Code:
    list_directory(self, path)
    I tried adding this to the server code:

    Code:
                if self.path == '/':
                    try:
                        self.send_response(200)
                        self.send_header('Content-type',	'text/html')
                        self.end_headers()
                        f = open(curdir + '/' + 'index.html')
                        self.wfile.write(f.read())
                        f.close()
                        return
                    except IOError:
                        def list_directory(self, path)
                        return
    I was basing a lot of that on the rest of the code in the file (I also tried a few variations). As you can see, the idea is that if they go to 'localhost' it will send the appropriate headers, and then either load the index.html file if it is there, or list the files in the directory. I'm not that clear about the try, finaly, and except things, but if someone could clear this up, and help solve this problem, it would help a lot.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by Hello, World.py
    I'm new to Python, but good at Perl. I've learned that python is capable of creating web servers. I've been looking at this example server as well as the module documentation. I've figured out how to change it to load index.html if you just enter localhost. According to the documentation for SimpleHTTPServer, there is a way to list all the files in a directory using this:

    Code:
    list_directory(self, path)
    I tried adding this to the server code:

    Code:
                if self.path == '/':
                    try:
                        self.send_response(200)
                        self.send_header('Content-type',	'text/html')
                        self.end_headers()
                        f = open(curdir + '/' + 'index.html')
                        self.wfile.write(f.read())
                        f.close()
                        return
                    except IOError:
                        def list_directory(self, path)
                        return
    I was basing a lot of that on the rest of the code in the file (I also tried a few variations). As you can see, the idea is that if they go to 'localhost' it will send the appropriate headers, and then either load the index.html file if it is there, or list the files in the directory. I'm not that clear about the try, finaly, and except things, but if someone could clear this up, and help solve this problem, it would help a lot.
    Seems to me you'll get a syntax error from this. I don't claim to have used the SimpleHTTPServer but you could try:

    Code:
      if self.path == '/':
                    try:
                        self.send_response(200)
                        self.send_header('Content-type',	'text/html')
                        self.end_headers()
                        f = open(curdir + '/' + 'index.html')
                        self.wfile.write(f.read())
                        f.close()
                        return
                    except IOError:
                        self.send_response(200)
                        self.send_header('Content-type',	'text/html')
                        self.end_headers()
    
                        for each_path in list_directory(self, path):
                            self.wfile.write(each_path)
                        return
    Hope this helps some,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    11
    Rep Power
    0
    That gave me this error in the console window:

    NameError: global name 'list_directory' is not defined
    And this message in the browser:
    HTTP/1.0 200 OK Server: BaseHTTP/0.3 Python/2.4.2 Date: Wed, 22 Feb 2006 03:05:20 GMT Content-type: text/html
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by Hello, World.py
    That gave me this error in the console window:

    And this message in the browser:
    Not a clue I don't know where you got list_directory() from however there is a function for doing this in the 'os module names listdir() that you may be able to use.

    Code:
    #!/usr/bin/env python
    
    import os
    
    ...
                        ...
                        for each_path in os.listdir(path):
                            self.wfile.write(each_path)
                        return
    That said maybe list_directory is a method in which case you can refer to is though self.list_directory(args).

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    11
    Rep Power
    0
    No luck. I wish there was a feature like in pearl that let you read from a directory and do something with each file...
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by Hello, World.py
    No luck. I wish there was a feature like in pearl that let you read from a directory and do something with each file...
    There is it's called os.listdir(), of course it doesn't really compare to perls on this matter. I don't think any language goes as far as perl for system admin stuff though if you feeling the sting maybe Ruby would suite you better?

    I don't really see why it wouldn't work though, what errors did you get Hello?

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    According to the documentation for SimpleHTTPServer, there is a way to list all the files in a directory using this:
    It says:

    The SimpleHTTPRequestHandler defines the following methods:

    ...

    do_GET( )
    The request is mapped to a local file by interpreting the request as a path relative to the current working directory.

    If the request was mapped to a directory, the directory is checked for a file named index.html or index.htm (in that order). If found, the file's contents are returned; otherwise a directory listing is generated by calling the list_directory() method. This method uses os.listdir() to scan the directory, and returns a 404 error response if the listdir() fails.
    Meaning the SimpleHTTPRequestHandler has a method called list_directory which it uses internally.

    Depending on how you are using the code, you would need to try calling list_directory on an instance of the SimpleHTTPRequestHandler class.

    Except, it already serves directory listings for you, so there doesn't seem much point in using it...

    Example:

    Code:
    # File: simplehttpserver-example-1.py
    
    import SimpleHTTPServer
    import SocketServer
    
    # minimal web server.  serves files relative to the
    # current directory.
    
    PORT = 8000
    
    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    
    print "serving at port", PORT
    httpd.serve_forever()
    From http://effbot.org/librarybook/simplehttpserver.htm

    Code:
    try:
        self.send_response(200)
        self.send_header('Content-type',	'text/html')
        self.end_headers()
        f = open(curdir + '/' + 'index.html')
        self.wfile.write(f.read())
        f.close()
        return
    except IOError:
        def list_directory(self, path)
        return
    o_O

    try:
    ...
    except:
    ...

    is Python's exception handling mechanism. It allows you to try to do some code that might fail, and then catch any exceptions the block of code throws and deal with them - e.g. you might get an IOError if this: f = open(curdir + '/' + 'index.html') points to a non-existant file.

    In the except block is the place to put error handling code - messages to the user, etc.


    def xyz():
    ...

    is a function definition. It has to have a body to go with it.
    Last edited by sfb; February 22nd, 2006 at 08:12 PM.

IMN logo majestic logo threadwatch logo seochat tools logo