[Python-Help] How to redirect output to browser - second request for help

Alex Martelli aleax at aleax.it
Mon Jul 29 10:49:45 EDT 2002


On Monday 29 July 2002 02:11 pm, A wrote:
> Hi,
> Is it possible to send output continuously to a web browser?.

Module webbrowser lets you direct the browser to any URL
of your choice.  However, to perform the task you require...:

> For example I have the following code
>
> >>> import httplib
> >>> h = httplib.HTTP('www.cwi.nl')
> >>> h.putrequest('GET', '/index.html')
> >>> h.putheader('Accept', 'text/html')
> >>> h.putheader('Accept', 'text/plain')
> >>> h.endheaders()
>
> I would like to send coming bytes ( here coming bytes of
> /index.html file)  directly to web browser in the same
> way as I use a web browser on my computer to open a
> webpage where I can see a progress how webpage is being
> opened.
> 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.

...that URL must also be supplied by your Python program.

In other words, you must implement a webserver and feed
the data as it comes to the browser.  Python supplies
modules in its standard library that are web browsers of
various capabilities, you can subclass and customize one
of those.  To get simultaneous operation between the data
arriving from the remote server and the data you feed to
the waiting browser, you will need either to write your Python
program as multi-threaded (and I suggest using module
Queue to communicate between the threads, it's simple
and natural), or use asynchronous even-driven programming
(the Python standard libraries come with two modules that
support such programming, asyncore and asynchat, but a
vastly more powerful arrangement for both asynchronous
servers and clients is found in the vast Python package
"Twisted Matrix", www.twistedmatrix.com, specifically in
subpackage twisted.internet).


In other words: the answer to your question is YES, but the
amount of programming (and the necessary skill, in Python
in particular and in programming in general) to achieve
your goal can be surprisingly high.  Were I approached as
a freelance consultant by a client requesting such a program,
I would size it at about 12 hours (and that's because I'm
already somewhat skilled in twisted.matrix, as well as an
expert programmer -- i.e., my consulting hours do _not_ come
cheap:-).  That's for a finished product of course, a rough
and ready feasibiliy-check prototype might take me less!


Alex




More information about the Python-list mailing list