Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > HTTP server

Reply
Thread Tools

HTTP server

 
 
placid
Guest
Posts: n/a
 
      06-28-2006
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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTTP SOAP/HTTP GET/HTTP POST milan_9211 Software 0 01-10-2011 02:10 PM
The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/822319ev1'. 'HTTP/1.1 500 Internal Server Error'. chanmm ASP .Net 2 09-07-2010 07:37 AM
multitask http server (single-process multi-connection HTTP server) lkcl Python 6 07-15-2010 10:39 PM
Can I change the response URL to http://server/page.XLS instead of http://server/page.ASPX ? guillermojco@gmail.com ASP .Net 3 04-26-2007 04:49 AM
URL: 'http://localhost/WebProject1', 'HTTP/1.1 500 Server Error' larkpics ASP .Net 9 10-22-2005 11:00 AM



Advertisments