Py3: Terminal or browser output?

Discussion in 'Python' started by Gnarlodious, Feb 13, 2010.

  1. Gnarlodious

    Gnarlodious Guest

    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
     
    Gnarlodious, Feb 13, 2010
    #1
    1. Advertising

  2. Gnarlodious

    Chris Rebert Guest

    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
     
    Chris Rebert, Feb 13, 2010
    #2
    1. Advertising

  3. 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
     
    Diez B. Roggisch, Feb 13, 2010
    #3
  4. Gnarlodious

    Gnarlodious Guest

    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
     
    Gnarlodious, Feb 13, 2010
    #4
  5. Gnarlodious

    Roger Binns Guest

    -----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-----
     
    Roger Binns, Feb 13, 2010
    #5
  6. Gnarlodious

    John Bokma Guest

    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
     
    John Bokma, Feb 13, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Collin Winter

    Single type for __builtins__ in Py3.0

    Collin Winter, Sep 23, 2005, in forum: Python
    Replies:
    4
    Views:
    364
    Collin Winter
    Sep 23, 2005
  2. Collin Winter

    Re: Single type for __builtins__ in Py3.0

    Collin Winter, Sep 23, 2005, in forum: Python
    Replies:
    4
    Views:
    336
    Collin Winter
    Sep 24, 2005
  3. gaurav kashyap
    Replies:
    3
    Views:
    6,665
    Paul Boddie
    Oct 31, 2008
  4. Gnarlodious

    Py3: decode subprocess output

    Gnarlodious, Nov 3, 2010, in forum: Python
    Replies:
    3
    Views:
    618
    Gnarlodious
    Nov 3, 2010
  5. Steve
    Replies:
    2
    Views:
    942
    edicionsdigitals.com edicions digitals xarxa socia
    Dec 7, 2010
Loading...

Share This Page