#!/usr/bin/python """ A fastcgi server for manually redirecting content from another website. This is useful to work round cross site XMLHttpReqest restrictions. If run in anger this might need to do a lot of caching. One option for this is to have a local squid server running as an accelerator for all sites that are used. """ from urllib import urlopen from fastcgi.Server import Handler, ThreadedServer class URLUndefinedError(Exception): pass class GetPageHandler(Handler): def handle(self, req): if not 'url' in self._args: raise URLUndefinedError() url = self._args['url'] try: source = urlopen(url) except IOError, e: req.stdout.write("Status: 404 Not Found\r\n") req.stdout.write("Content-type: text/plain\r\n\r\n") req.stdout.write("This URL could not be found. Check the url option.") return req.stdout.write("Content-type: %s\r\n\r\n" % source.headers['Content-type']) for line in source: req.stdout.write(line) req.finish() server = ThreadedServer(GetPageHandler) server.serve_forever();