Re: Py3: Terminal or browser output?
Gnarlodious 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Py3: Terminal or browser output?
-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+bmt6F3WPXYatM7yKVcs knMAoLx5sJ3lFmofrrgHaS3aYOBAun0d =Nxd4 -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: Py3: Terminal or browser output?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Py3: Terminal or browser output?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Py3: Terminal or browser output?
On Sat, Feb 13, 2010 at 11:46 AM, Gnarlodious 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 -- http://mail.python.org/mailman/listinfo/python-list
Py3: Terminal or browser output?
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 -- http://mail.python.org/mailman/listinfo/python-list