Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Py3: Terminal or browser output?

Reply
Thread Tools

Py3: Terminal or browser output?

 
 
Gnarlodious
Guest
Posts: n/a
 
      02-13-2010
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
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      02-13-2010
On Sat, Feb 13, 2010 at 11:46 AM, Gnarlodious <(E-Mail Removed)> 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.rebertia.com
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-13-2010
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
 
Reply With Quote
 
Gnarlodious
Guest
Posts: n/a
 
      02-13-2010
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
 
Reply With Quote
 
Roger Binns
Guest
Posts: n/a
 
      02-13-2010
-----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.mozdev.org

iEYEARECAAYFAkt3JWUACgkQmOOfHg372QRifACfYKS7+bmt6F 3WPXYatM7yKVcs
knMAoLx5sJ3lFmofrrgHaS3aYOBAun0d
=Nxd4
-----END PGP SIGNATURE-----

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      02-13-2010
Gnarlodious <(E-Mail Removed)> 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://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
 
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
v = vte.Terminal() AttributeError: 'module' object has no attribute 'Terminal' Steve Python 2 12-07-2010 05:48 PM
open a new terminal window from another terminal window in linux/unixsystem gaurav kashyap Python 3 10-31-2008 12:10 PM
ASP.Net/browser vs Terminal Services RAIL/Window's-based development Ryan ASP .Net 4 01-16-2007 02:20 PM



Advertisments