redirecting python console output to web browser page

5,332 views
Skip to first unread message

blackshirt

unread,
Dec 29, 2011, 10:21:27 PM12/29/11
to web2py-users
i have a little programming with web2py, with import some library from
python-apt...
i want to show output from some command/function from that library to
the web page..
Output from that script showing on console, not showing on web page,
but only result on return value (True / false) send to web page..


Here some code from controller page

def update():

apt_pkg.init()
cache = apt.Cache()
cache.update(apt.progress.TextFetchProgress())
cache.open(None)

this successfully run, if we call this controller, but output show on
console not send to page..
How we solved this, i'm stuck here ?

Thanks for clue, advice, or reply's

Alan Etkin

unread,
Dec 30, 2011, 6:29:39 AM12/30/11
to web2py-users
I am not sure if web2py has a feature for that. You can still redirect
output to a filelike object during controller processing and on exit
return the control to the original handler. That way you could extract
the output from the file object to append it to the web2py response

Pseudo-code:

on controller start

# create obj
# send all stdout to obj
# return dict with obj content as message or another name to be
processed by a web2py view

Massimo Di Pierro

unread,
Dec 30, 2011, 11:29:23 AM12/30/11
to web2py-users
There are two ways around it:
- hopefully the apt module can be configured to send output to a file
- if not, use the python subprocess module to run the code is a
separate process. The popen function has the ability to capture the
output.

blackshirt

unread,
Jan 1, 2012, 9:12:52 AM1/1/12
to web2py-users
Thank's Alan
that was very inspiring for noob like me..
if we look at http://groups.google.com/group/web2py/browse_thread/thread/bea1afc64dcc39a6/280bbdcf5acd0bbd#280bbdcf5acd0bbd
,can we use similar methods to do real time streaming output ??

blackshirt

unread,
Jan 1, 2012, 9:25:50 AM1/1/12
to web2py-users
Thank's massimo..
look's very complex to do..
Massimo, is there web2py feature's for doing this ?
On Dec 30 2011, 11:29 pm, Massimo Di Pierro

Ross Peoples

unread,
Jan 1, 2012, 12:20:27 PM1/1/12
to web...@googlegroups.com
I use subprocess.Popen a lot to run shell commands from Python. There is no need for a specific web2py feature.

This is the method I use whenever I need to call something:

    import subprocess
    def run_command(self, *args):
        """
        Returns the output of a command as a tuple (output, error).
        """
        p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        return p.communicate()

It's very simple to use:

output, error = run_command('python', 'my_script.py', 'arg1', 'arg2', ...)

This runs the command, waits for it to exit, and then you have a string "output" for the output from the command, and a string "error" for any error output from the command.

blackshirt

unread,
Jan 1, 2012, 9:45:22 PM1/1/12
to web2py-users
Okey Ross, if we using popen, can we doing it for real time (i mean,
streaming output) ?

Ross Peoples

unread,
Jan 2, 2012, 11:42:28 AM1/2/12
to web...@googlegroups.com
Yes, you can, though it's much more difficult to stream it live to a web page. You would need to use JavaScript (comet or polling) to get the output as it is generated and display it in the browser. There are many ways to do this, but it's not the simplest thing in the world.

Off the top of my head, I can think of a few different ways to implement this (though each has pros and cons):

- Simplest: load a page as usual with an <iframe> tag that runs the command and streams the output as it happens
- Better: Start the process and just AJAX calls to grab latest output and display
- Best/Hardest: Use comet (AJAX push) to push output to the web page

Using <iframe> is sometimes frowned upon as it's seen as the "easy way out" of doing complicated tasks. Using JavaScript offers some extra flexibility on how the output is displayed in the browser (and could even allow the user to interact).

Paolo Caruccio

unread,
Jan 2, 2012, 3:17:42 PM1/2/12
to web...@googlegroups.com
There is another HTML5 technology to push stream from server to client : EventSource aka Sent-Server Events (http://dev.w3.org/html5/eventsource/). Its behaviour is like ajax long polling.
I'm - just now - experimenting a simple mechanism to send to clients a message when the database changes.
Pros : The implementation is enough simple.
Cons : IE9 doesn't support EventSource.



blackshirt

unread,
Jan 9, 2012, 10:20:25 PM1/9/12
to web2py-users
like hard to implement.. i want simple approach for them

blackshirt

unread,
Jan 9, 2012, 10:21:13 PM1/9/12
to web2py-users
i'm still pretty newbie for them.. but i'll try to search.

blackshirt

unread,
Jan 9, 2012, 10:22:05 PM1/9/12
to web2py-users
Ross, i think i don't want to use external script, like
my_script.py ...

On Jan 2, 12:20 am, Ross Peoples <ross.peop...@gmail.com> wrote:

Ross Peoples

unread,
Jan 10, 2012, 7:54:19 AM1/10/12
to web...@googlegroups.com
Two things:

First, the "my_script.py" is the program you want to run to get output from. I just used a Python script as an example. This line could just as easily be:

output, error = run_command('ls', '-l')

to list all files in a directory (though you would typically use Python's os module for this). Again, the command can be anything that you would type yourself, with the exception that arguments need to be in a list format (in other words, you can't use 'ls -l' as the command, you have to split it up into 'ls', '-l').

Secondly, this may not be the best thing for you to try as a beginner. Doing things like this requires managing multiple processes, which makes things much more complicated. If you must do this right now, then use the first example I gave (that blocks until the command is complete). That is the easiest way.

Trust me, if you are uncomfortable with this, then having to sync up multiple processes to do live streaming of output will be a nightmare for you, no matter what route you take.
Reply all
Reply to author
Forward
0 new messages