Results of executing hyperlink in script

M

Muddy Coder

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
 
M

MRAB

Muddy said:
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.
 
C

Cameron Laird

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.
 
G

Gabriel Genellina

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.
 
T

Tim Chase

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
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top