HTTP server

Discussion in 'Python' started by placid, Jun 28, 2006.

  1. placid

    placid Guest

    Hi all,

    Im having trouble with the following code for handling GET requests
    from a client to my HTTP server. What i want to do is restrict access
    only to a folder and contents(files) within this folder. But when
    trying to
    open files (eg text files) i get file not found error from send_head()
    method of SimpleHTTPServer. The reason behind this is when opening the
    file the path to the file is only C:\file.txt when it should be
    C:\folder\file.txt. And when i remove the code that checks if path
    contains "txt" it works (i can access files without errors).

    Any help will be greatly appreciated!

    <code>
    def list_directory(self, path):
    """Helper to produce a directory listing (absent index.html).

    Return value is either a file object, or None (indicating an
    error). In either case, the headers are sent, making the
    interface the same as for send_head().

    """
    f = StringIO()

    p = self.translate_path(self.path)
    if p.find("txt") == -1:
    f.write("<title>httpserver.py: Access Denied</title>" )
    f.write("<h2>httpserver.py: Access Denied</h2>" )
    else:
    try:
    list = os.listdir(path)
    except os.error:
    self.send_error(404, "No permission to list directory")
    return None

    list.sort(key=lambda a: a.lower())

    displaypath = cgi.escape(urllib.unquote(self.path))
    f.write("<title>Directory listing for %s</title>\n"
    %displaypath)
    f.write("<h2>Directory listing for %s</h2>\n" %
    displaypath)
    f.write("<hr>\n<ul>\n")
    for name in list:
    fullname = os.path.join(path, name)
    displayname = linkname = name
    # Append / for directories or @ for symbolic links
    if os.path.isdir(fullname):
    displayname = name + "/"
    linkname = name + "/"
    if os.path.islink(fullname):
    displayname = name + "@"
    # Note: a link to a directory displays with @ and
    links with /
    f.write('<li><a href="%s">%s</a>\n' %
    (urllib.quote(linkname), \

    cgi.escape(displayname)))
    f.write("</ul>\n<hr>\n")

    length = f.tell()
    f.seek(0)
    self.send_response(200)
    self.send_header("Content-type", "text/html")
    self.send_header("Content-Length", str(length))
    self.end_headers()
    return f
    </code>
     
    placid, Jun 28, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. larkpics
    Replies:
    9
    Views:
    9,016
    Patrick.O.Ige
    Oct 22, 2005
  2. mike
    Replies:
    5
    Views:
    973
    Keith M. Corbett
    Sep 21, 2004
  3. -
    Replies:
    2
    Views:
    402
    Alan Krueger
    Jul 29, 2005
  4. Replies:
    3
    Views:
    798
    Guillermo
    Apr 26, 2007
  5. lkcl
    Replies:
    6
    Views:
    1,045
Loading...

Share This Page