Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Best way to capture output from an exec'ed (or such) script?

Reply
Thread Tools

Best way to capture output from an exec'ed (or such) script?

 
 
exscape@gmail.com
Guest
Posts: n/a
 
      08-02-2007
Hey everyone,
I'm writing a tiny web server, mostly to learn - security and such is
a non-issue, but learning how to make things secure is of course nice
too. To be clear - I will most likely not even use it myself.

In any case. I've added some minor scripting support, so that you can
write dynamic pages in Python. To do this, I use execfile(), and pass
the script a dictionary with some basic variables. The script then
sets a "ret" variable that's sent back to the browser. That's some
major ugliness right there! If I do a "print" inside the script, then
it'll end up on the server console. I want it to end up in the web
browser.

How would I accomplish this, i.e. redirect the standard output to
somewhere else, such as a variable in the calling script? If it's not
easy (as in, quite trivial) just say so. I've been thinking about
popen(2,3,4) but that's almost as ugly, if not even worse, than the
current "solution".

Thanks in advance.

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-02-2007
En Thu, 02 Aug 2007 16:48:06 -0300, <(E-Mail Removed)> escribió:

> In any case. I've added some minor scripting support, so that you can
> write dynamic pages in Python. To do this, I use execfile(), and pass
> the script a dictionary with some basic variables. The script then
> sets a "ret" variable that's sent back to the browser. That's some
> major ugliness right there! If I do a "print" inside the script, then
> it'll end up on the server console. I want it to end up in the web
> browser.


If `print` were a function, this would be easy: just provide a replacement
into the dictionary you pass to the script. But print is a statement, and
this becomes a bit harder.

If your web server only processes a single request at a time, you can
replace sys.stdout (and perhaps sys.stderr) with a suitable object having
a write() function: a true open file, or a StringIO instance, or even a
custom object that collects "printed" lines into a list.

If your web server is multithreaded (or you use some other way to process
many simultaneous requests) you have to be more careful - remember that
sys.stdout is global, you must find a way to distinguish between output
from different processes all going into the same collector.

--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
Cameron Laird
Guest
Posts: n/a
 
      08-03-2007
In article <(E-Mail Removed)>,
Gabriel Genellina <(E-Mail Removed)> wrote:
>En Thu, 02 Aug 2007 16:48:06 -0300, <(E-Mail Removed)> escribió:
>
>> In any case. I've added some minor scripting support, so that you can
>> write dynamic pages in Python. To do this, I use execfile(), and pass
>> the script a dictionary with some basic variables. The script then
>> sets a "ret" variable that's sent back to the browser. That's some
>> major ugliness right there! If I do a "print" inside the script, then
>> it'll end up on the server console. I want it to end up in the web
>> browser.

>
>If `print` were a function, this would be easy: just provide a replacement
>into the dictionary you pass to the script. But print is a statement, and
>this becomes a bit harder.
>
>If your web server only processes a single request at a time, you can
>replace sys.stdout (and perhaps sys.stderr) with a suitable object having
>a write() function: a true open file, or a StringIO instance, or even a
>custom object that collects "printed" lines into a list.
>
>If your web server is multithreaded (or you use some other way to process
>many simultaneous requests) you have to be more careful - remember that
>sys.stdout is global, you must find a way to distinguish between output
> from different processes all going into the same collector.

.
.
.
While we're on the subject of Web servers so small
as to be educational, I'll recommend <URL:
http://www.ibm.com/developerworks/we.../wa-ltwebserv/ >.
 
Reply With Quote
 
gregpinero@gmail.com
Guest
Posts: n/a
 
      08-03-2007
On Aug 2, 7:32 pm, "Gabriel Genellina" <(E-Mail Removed)> wrote:
> If your web server is multithreaded (or you use some other way to process
> many simultaneous requests) you have to be more careful - remember that
> sys.stdout is global, you must find a way to distinguish between output
> from different processes all going into the same collector.
>

Any ideas on how to do this? Is it even possible?

-Greg

 
Reply With Quote
 
gregpinero@gmail.com
Guest
Posts: n/a
 
      08-04-2007
On Aug 3, 11:14 am, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> On Aug 2, 7:32 pm, "Gabriel Genellina" <(E-Mail Removed)> wrote:> If your web server is multithreaded (or you use some other way to process
> > many simultaneous requests) you have to be more careful - remember that
> > sys.stdout is global, you must find a way to distinguish between output
> > from different processes all going into the same collector.

>
> Any ideas on how to do this? Is it even possible?
>
> -Greg


I'm actually worried about this now. Does anyone know of any
potential solutions? Anything to at least get me started?

-Greg

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-05-2007
En Sat, 04 Aug 2007 18:52:16 -0300, http://www.velocityreviews.com/forums/(E-Mail Removed)
<(E-Mail Removed)> escribió:

>> On Aug 2, 7:32 pm, "Gabriel Genellina" <(E-Mail Removed)> wrote:>
>> If your web server is multithreaded (or you use some other way to
>> process
>> > many simultaneous requests) you have to be more careful - remember

>> that
>> > sys.stdout is global, you must find a way to distinguish between

>> output
>> > from different processes all going into the same collector.

>
> I'm actually worried about this now. Does anyone know of any
> potential solutions? Anything to at least get me started?


You can use threading.currentThread() to distinguish between threads, and
a Lock (or RLock) to ensure the output doesn't get mixed:

def write(msg):
t = time.strftime("%x %X", time.localtime())
who = threading.currentThread().getName()
line = "%s (%-15.15s) %s\n" % (t, who, msg)
OutputDebugString("%s (%-15.15s) %s\n" % (t, who, msg))
loglock.acquire()
try:
with logfile() as f:
f.write(line)
finally:
loglock.release()

--
Gabriel Genellina

 
Reply With Quote
 
gregpinero@gmail.com
Guest
Posts: n/a
 
      08-05-2007
On Aug 5, 5:50 am, "Gabriel Genellina" <(E-Mail Removed)> wrote:
> En Sat, 04 Aug 2007 18:52:16 -0300, (E-Mail Removed)
> <(E-Mail Removed)> escribió:
>
> >> On Aug 2, 7:32 pm, "Gabriel Genellina" <(E-Mail Removed)> wrote:>
> >> If your web server is multithreaded (or you use some other way to
> >> process
> >> > many simultaneous requests) you have to be more careful - remember
> >> that
> >> > sys.stdout is global, you must find a way to distinguish between
> >> output
> >> > from different processes all going into the same collector.

>
> > I'm actually worried about this now. Does anyone know of any
> > potential solutions? Anything to at least get me started?

>
> You can use threading.currentThread() to distinguish between threads, and
> a Lock (or RLock) to ensure the output doesn't get mixed:
>
> def write(msg):
> t = time.strftime("%x %X", time.localtime())
> who = threading.currentThread().getName()
> line = "%s (%-15.15s) %s\n" % (t, who, msg)
> OutputDebugString("%s (%-15.15s) %s\n" % (t, who, msg))
> loglock.acquire()
> try:
> with logfile() as f:
> f.write(line)
> finally:
> loglock.release()
>
> --
> Gabriel Genellina



Thanks, Gabriel. I'm not actually making the threads myself though.
Rather it's a simple single threaded script running under FastCGI.
Would your method still work? I don't know too much about how FastCGI
works. Perhaps that's where I should start.

-Greg

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Best way to capture QuickFlix ? Me NZ Computing 13 04-09-2012 07:34 AM
Best way to capture input. Aaron Fude Java 6 11-29-2008 12:54 AM
Any way to capture stdcout output to memory? Jim Langston C Programming 11 05-02-2008 09:25 PM
Best way to capture output of another command-line program in Win32? Peter A. Schott Python 4 02-07-2006 12:30 AM
best way to capture chill@will.com DVD Video 2 01-13-2004 07:10 PM



Advertisments