Py3: Terminal or browser output?

G

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)?

-- Gnarlie
 
C

Chris Rebert

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
 
D

Diez B. Roggisch

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
 
G

Gnarlodious

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
 
R

Roger Binns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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.mozdev.org

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

John Bokma

Gnarlodious said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top