How to redirect output to browser - second request for help
Fredrik Lundh
fredrik at pythonware.com
Mon Jul 29 12:03:59 EDT 2002
"A" <printers at sendme.cz> wrote:
> In other words I do NOT want to wait for downloading the
> whole file and only after that open that downloaded file
> in a web browser.
browsers have no problem reading data incrementally from
a web servers. something like this might work:
# based on simplehttpserver-example-2.py from
# O'Reilly's Python Standard Library
import SocketServer
import SimpleHTTPServer
import urllib, string
PORT = 1234
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
path = self.path
... convert the path in some way ...
file = urllib.urlopen(path) # or use httplib
while 1:
data = file.read(1024)
if not data:
break
... process the string in some way ...
... for example data = string.upper(data)
self.wfile.write(data)
self.wfile.flush()
httpd = SocketServer.ForkingTCPServer(('', PORT), Proxy)
print "serving at port", PORT
httpd.serve_forever()
use the webbrowser module to point your browser to
localhost:1234.
</F>
<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list