Bulkan at gmail.com
Tue Jun 27 07:09:57 CEST 2006
Simon Forman wrote:
> Awesome! Glad to hear it.
> > Thanks for the help. I got it to work now.
> You're welcome. I'm glad I could help you. :-D
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 within this folder. But when trying to
open files (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!
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>" )
list = os.listdir(path)
self.send_error(404, "No permission to list directory")
list.sort(key=lambda a: a.lower())
displaypath = cgi.escape(urllib.unquote(self.path))
f.write("<title>Directory listing for %s</title>\n" %
f.write("<h2>Directory listing for %s</h2>\n" %
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
# Append / for directories or @ for symbolic links
displayname = name + "/"
linkname = name + "/"
displayname = name + "@"
# Note: a link to a directory displays with @ and
links with /
length = f.tell()
More information about the Python-list