[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Py3: Terminal or browser output?

Gnarlodious

2/13/2010 7:46:00 PM

Hello, searched all over but no success. I want to have a script
output HTML if run in a browser and plain text if run in a Terminal.
In Python 2, I just said this:

if len(sys.argv)==True:

and it seemed to work. Py3 must have broken that by sending a list
with the path to the script in BOTH the browser and Terminal. Is there
some newfangled way to determine what is running the script (hopefully
without a try wrapper)?

-- Gnarlie
5 Answers

Chris Rebert

2/13/2010 8:16:00 PM

0

On Sat, Feb 13, 2010 at 11:46 AM, Gnarlodious <gnarlodious@gmail.com> wrote:
> Hello, searched all over but no success. I want to have a script
> output HTML if run in a browser and plain text if run in a Terminal.
> In Python 2, I just said this:
>
> if len(sys.argv)==True:

That line doesn't make sense really as it is practically equivalent to:

if len(sys.argv) == 1:

Which, since argv always contains at least 1 element (the program's
name), is essentially checking whether /no/ arguments were passed on
the command line.
Recall that issubclass(bool, int) is true and that True == 1 in Python
due to details of said subclassing.

Note also that "== True" is almost always unnecessary as it is
essentially implicit in the "if".

I suspect you instead meant to write:

if len(sys.argv) > 0:

which for the record can be written more idiomatically as:

if sys.argv: # bool(some_list) is True if some_list is not empty

Which, however, as I explained, is always true since argv is /never/
completely empty, and thus the test is useless. What you probably
*wanted* is:

if len(sys.argv) > 1:

Which is effectively the opposite of my very first code snippet and
checks whether we /were/ passed some arguments on the command line.

> and it seemed to work.

By happy accident I suspect, due to the aforementioned way == works
between bools and ints and probably a logic error in your code.

> Py3 must have broken that by sending a list
> with the path to the script in BOTH the browser and Terminal. Is there
> some newfangled way to determine what is running the script (hopefully
> without a try wrapper)?

How exactly are you running the script *"in"* the browser? Browsers
can only execute JavaScript, not Python. Do you mean you're running it
via a webserver?
If so, there are /several/ possible ways of doing that, please explain
exactly which you are using.

Cheers,
Chris
--
http://blog.re...

Diez B. Roggisch

2/13/2010 8:17:00 PM

0

Am 13.02.10 20:46, schrieb Gnarlodious:
> Hello, searched all over but no success. I want to have a script
> output HTML if run in a browser and plain text if run in a Terminal.
> In Python 2, I just said this:
>
> if len(sys.argv)==True:
>
> and it seemed to work. Py3 must have broken that by sending a list
> with the path to the script in BOTH the browser and Terminal. Is there
> some newfangled way to determine what is running the script (hopefully
> without a try wrapper)?

I have no idea what you mean by "running python in a browser". I can
only guess you mean as cgi or mod_python-skript?

However, maybe

if os.isatty(sys.stdout.fileno()):


works for you.

Or you could check for certain environment variables that are present
when running as CGI or mod_python skript, but not knowing *what* you do
can only lead to educated guesses at best.

Diez

Gnarlodious

2/13/2010 8:35:00 PM

0

On Feb 13, 1:17 pm, "Diez B. Roggisch" wrote:

> However, maybe
>
> if os.isatty(sys.stdout.fileno()):

OK, this works in Python 2:

#!/usr/bin/python
import sys, os

if __name__=="__main__":
if os.isatty(sys.stdout.fileno()):
print "Terminal"
else:
print "Content-type:text/html\n\nBROWSER"


likewise in Python3:

#!/usr/local/bin/python3.1
import sys, os

if __name__=="__main__":
if os.isatty(sys.stdout.fileno()):
print("Terminal")
else:
print("Content-type:text/html\n\nBROWSER")


Thank you, always impressed with the fast help here.

-- Gnarlie

Roger Binns

2/13/2010 10:19:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Gnarlodious wrote:
> I want to have a script
> output HTML if run in a browser and plain text if run in a Terminal.

You may also want to look into urwid. It provides you with a text console
interface but can also provide HTML. It has widgets like text boxes, lists,
tick boxes etc.

Roger

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkt3JWUACgkQmOOfHg372QRifACfYKS7+bmt6F3WPXYatM7yKVcs
knMAoLx5sJ3lFmofrrgHaS3aYOBAun0d
=Nxd4
-----END PGP SIGNATURE-----

John Bokma

2/13/2010 10:51:00 PM

0

Gnarlodious <gnarlodious@gmail.com> writes:

> Hello, searched all over but no success. I want to have a script
> output HTML if run in a browser and plain text if run in a Terminal.
> In Python 2, I just said this:
>
> if len(sys.argv)==True:
>
> and it seemed to work. Py3 must have broken that by sending a list
> with the path to the script in BOTH the browser and Terminal. Is there
> some newfangled way to determine what is running the script (hopefully
> without a try wrapper)?

Your web server (assuming CGI) sets some environment variables, which
you could test for. This has as advantage (or disadvantage) that you can
set the variable at the CLI and see the output the browser could get.

--
John Bokma j3b

Hacking & Hiking in Mexico - http://john...
http://castle... - Perl & Python Development