From d9f9c73903907c2f2f79bbcf573142c46689e3c9 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Tue, 16 Nov 2010 21:16:36 -0500 Subject: [PATCH] Added python support to web remote Now I can write web pages in python! Whoo!! --- plugins/web_remote.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/plugins/web_remote.py b/plugins/web_remote.py index d7650c25..cfb17805 100644 --- a/plugins/web_remote.py +++ b/plugins/web_remote.py @@ -1,18 +1,21 @@ # Bryan Schumaker (11/13/2010) import ocarina +import imp from libsaria import threads -path = ocarina.libsaria.path -exists = path.exists -join = path.join +path = ocarina.libsaria.path +exists = path.exists +join = path.join +basename = path.basename from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer html = join(path.cwd(), "html") server = None - -types = {".html":"html", ".js":"javascript"} +types = {".html":"text/html", ".js":"text/javascript", + ".ico":"image/vnd.microsoft.icon", + ".py":"text/html"} class HTTPRequest(BaseHTTPRequestHandler): @@ -27,11 +30,16 @@ class HTTPRequest(BaseHTTPRequestHandler): raise IOError base, ext = path.splitext(fpath) self.send_response(200) - self.send_header('Content-type', 'text/%s' % types[ext]) + self.send_header('Content-type', '%s' % types[ext]) self.end_headers() - f = open(fpath) - self.wfile.write(f.read()) - f.close() + if ext == ".py": + file = basename(base) + mod = imp.load_source(file, fpath) + mod.to_html(self.wfile) + else: + f = open(fpath) + self.wfile.write(f.read()) + f.close() except IOError: self.send_error(404, 'File Not Found: %s' % self.path)