Results of executing hyperlink in script

Discussion in 'Python' started by Muddy Coder, Jan 28, 2009.

  1. Muddy Coder

    Muddy Coder Guest

    Hi Folks,

    My previous post got a many helps from the people, and I tested what
    they suggested. Since this topic maybe needed in future, so I drop
    these lines below to help the future programmers. The methods worked
    as below:

    1. This method was suggested by Cameron Laird:

    os.system("start %s" % URL)

    It works. But, if the URL contains character &, it will fail. For
    example, if URL has only one field, such as: http://www.mydomain.com/ascript.cgi?user=muddy
    this method works well. But, if there more than one field need to be
    input, such as http://www.mydomain.com/ascript.cgi/user=muddy&password=foo,
    the field password failed to reach server, and the CGI script
    complained.

    2. The best way is to use urllib2, suggested by Ron Barak, my code is
    below:

    import urllib2
    source = urllib2.urlopen(URL).read()
    print source

    It successfully triggered CGI script, and also got feedback from
    server. It works very well!

    My thanks go to all the helpers!

    Muddy Coder
     
    Muddy Coder, Jan 28, 2009
    #1
    1. Advertising

  2. Muddy Coder

    MRAB Guest

    Muddy Coder wrote:
    > Hi Folks,
    >
    > My previous post got a many helps from the people, and I tested what
    > they suggested. Since this topic maybe needed in future, so I drop
    > these lines below to help the future programmers. The methods worked
    > as below:
    >
    > 1. This method was suggested by Cameron Laird:
    >
    > os.system("start %s" % URL)
    >
    > It works. But, if the URL contains character &, it will fail. For
    > example, if URL has only one field, such as: http://www.mydomain.com/ascript.cgi?user=muddy
    > this method works well. But, if there more than one field need to be
    > input, such as http://www.mydomain.com/ascript.cgi/user=muddy&password=foo,
    > the field password failed to reach server, and the CGI script
    > complained.
    >

    You could put quotes around the URL:

    os.startfile('"%s"' % URL)

    or:

    os.system('start "%s"' % URL)

    if "&" has a special meaning to the command-line.
     
    MRAB, Jan 28, 2009
    #2
    1. Advertising

  3. In article <>,
    MRAB <> wrote:
    >Muddy Coder wrote:

    .
    .
    .
    >You could put quotes around the URL:
    >
    >os.startfile('"%s"' % URL)
    >
    >or:
    >
    >os.system('start "%s"' % URL)
    >
    >if "&" has a special meaning to the command-line.


    In fact, no, happiness does NOT result in these contexts with
    another layer of quoting.
    os.startfile(URL)
    works fine even if URL embeds special characters, and does not
    work at all if URL is itself quoted.

    os.system("start ...")

    just gives a variety of unuseful results if URL embeds special
    characters.
     
    Cameron Laird, Jan 29, 2009
    #3
  4. En Wed, 28 Jan 2009 20:49:14 -0200, Muddy Coder <>
    escribió:

    > My previous post got a many helps from the people, and I tested what
    > they suggested. Since this topic maybe needed in future, so I drop
    > these lines below to help the future programmers. The methods worked
    > as below:
    >
    > 1. This method was suggested by Cameron Laird:
    >
    > os.system("start %s" % URL)
    >
    > It works. But, if the URL contains character &, it will fail. For


    > 2. The best way is to use urllib2, suggested by Ron Barak, my code is
    > below:
    >
    > import urllib2
    > source = urllib2.urlopen(URL).read()
    > print source


    Note that both methods are essencially different. The first one opens a
    browser window, and it's up to the user what to do after the initial
    request is done -- if this is what you want, the webbrowser module is
    better suited for that task.
    The second one is a pure programming interfase - the Python script is in
    control, and the user isn't involved at all.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jan 29, 2009
    #4
  5. Muddy Coder

    Tim Chase Guest

    > 1. This method was suggested by Cameron Laird:
    >
    > os.system("start %s" % URL)
    >
    > It works. But, if the URL contains character &, it will fail. For



    As an aside, the START command is a bit picky regarding quotes.
    You have to use this horrible contortion

    os.system('start "title" "%s"' % URL)

    The "title" is optional content-wise, but required positionally
    if there's a quoted resource, so you can just use

    start "" "%s"

    a pain, but that's CMD.EXE for you. :)

    -tkc
     
    Tim Chase, Jan 29, 2009
    #5
    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. Cédric Rossé
    Replies:
    1
    Views:
    533
    Cédric Rossé
    Nov 9, 2004
  2. sarmin kho
    Replies:
    1
    Views:
    378
    Cameron Laird
    Jun 4, 2004
  3. Replies:
    3
    Views:
    1,061
    James Stroud
    Dec 13, 2005
  4. Replies:
    3
    Views:
    191
    Josef Moellers
    Nov 1, 2007
  5. ashish
    Replies:
    4
    Views:
    298
    ashish
    Sep 20, 2012
Loading...

Share This Page