diff options
author | willmcgugan@gmail.com <willmcgugan@gmail.com@67cdc799-7952-0410-af00-57a81ceafa0f> | 2013-08-10 12:25:53 +0000 |
---|---|---|
committer | willmcgugan@gmail.com <willmcgugan@gmail.com@67cdc799-7952-0410-af00-57a81ceafa0f> | 2013-08-10 12:25:53 +0000 |
commit | e414b0b3ea3f4323beb4620916b32aa1718456f6 (patch) | |
tree | f8430f30f81f6a7e1c79c57f64d155ee6efb0815 | |
parent | 352be7b9fd264afd7faafffe5bdb27f44460f158 (diff) | |
download | pyfilesystem-e414b0b3ea3f4323beb4620916b32aa1718456f6.tar.gz |
Fixes an issue serving binary files
git-svn-id: http://pyfilesystem.googlecode.com/svn/trunk@862 67cdc799-7952-0410-af00-57a81ceafa0f
-rw-r--r-- | fs/expose/http.py | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/fs/expose/http.py b/fs/expose/http.py index fca3348..fb00749 100644 --- a/fs/expose/http.py +++ b/fs/expose/http.py @@ -17,15 +17,15 @@ def _datetime_to_epoch(d): return mktime(d.timetuple()) class FSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): - + """A hacked together version of SimpleHTTPRequestHandler""" - + def __init__(self, fs, request, client_address, server): self._fs = fs SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, request, client_address, server) - + def do_GET(self): - """Serve a GET request.""" + """Serve a GET request.""" f = None try: f = self.send_head() @@ -33,10 +33,10 @@ class FSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): try: self.copyfile(f, self.wfile) except socket.error: - pass + pass finally: if f is not None: - f.close() + f.close() def send_head(self): """Common code for GET and HEAD commands. @@ -65,15 +65,15 @@ class FSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): break else: return self.list_directory(path) - ctype = self.guess_type(path) + ctype = self.guess_type(path) try: info = self._fs.getinfo(path) - f = self._fs.open(path, 'r') + f = self._fs.open(path, 'rb') except FSError, e: self.send_error(404, str(e)) return None - self.send_response(200) - self.send_header("Content-type", ctype) + self.send_response(200) + self.send_header("Content-type", ctype) self.send_header("Content-Length", str(info['size'])) if 'modified_time' in info: self.send_header("Last-Modified", self.date_time_string(_datetime_to_epoch(info['modified_time']))) @@ -103,11 +103,11 @@ class FSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath) f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath) f.write("<hr>\n<ul>\n") - + parent = dirname(path) if path != parent: f.write('<li><a href="%s">../</a></li>' % urllib.quote(parent.rstrip('/') + '/')) - + for path in paths: f.write('<li><a href="%s">%s</a>\n' % (urllib.quote(path), cgi.escape(path))) @@ -119,45 +119,45 @@ class FSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): self.send_header("Content-Length", str(length)) self.end_headers() return f - + def translate_path(self, path): # abandon query parameters path = path.split('?',1)[0] path = path.split('#',1)[0] path = posixpath.normpath(urllib.unquote(path)) return path - + def serve_fs(fs, address='', port=8000): - + """Serve an FS instance over http - + :param fs: an FS object :param address: IP address to serve on :param port: port number - + """ - + def Handler(request, client_address, server): return FSHTTPRequestHandler(fs, request, client_address, server) - + #class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): - # pass - httpd = SocketServer.TCPServer((address, port), Handler, bind_and_activate=False) + # pass + httpd = SocketServer.TCPServer((address, port), Handler, bind_and_activate=False) #httpd = ThreadedTCPServer((address, port), Handler, bind_and_activate=False) httpd.allow_reuse_address = True httpd.server_bind() httpd.server_activate() - - server_thread = threading.Thread(target=httpd.serve_forever) - server_thread.start() + + server_thread = threading.Thread(target=httpd.serve_forever) + server_thread.start() try: while True: time.sleep(0.1) except (KeyboardInterrupt, SystemExit): httpd.shutdown() - + if __name__ == "__main__": - + from fs.osfs import OSFS serve_fs(OSFS('~/'))
\ No newline at end of file |