Executing a python script from an HTML link?

A

Andrew Chalk

Is this possible? In my CGI app. I display a web page with a link (anchor).
When the link is clicked I want to exectute a python script rather than go
to an HTML page.

Many thanks.
 
R

Rene Pijlman

Andrew Chalk:
Is this possible? In my CGI app. I display a web page with a link (anchor).
When the link is clicked I want to exectute a python script rather than go
to an HTML page.

IIUYC: make the link tot a Python CGI script instead of to an HTML page.
 
J

John Roth

Andrew Chalk said:
Is this possible? In my CGI app. I display a web page with a link (anchor).
When the link is clicked I want to exectute a python script rather than go
to an HTML page.

Do you want to run the script on the server or the client? If you
want to run it on the client, you can use Python instead of Javascript
(or in addition to Javascript) in the web page. Look at the <script>
tag for the language (I think that's the one - I haven't looked it up
recently) attribute. Otherwise, just invoke Python the same way you would
invoke any other executable, and use the script name as the
first parameter.

John Roth
 
A

Andrew Chalk

Thanks. I want to run the script on the server. The problem is that I don't
have an html page as an HREF. Viz:

<tr><A HREF="Fred.htm">Fred</A>
What replaces Fred.htm to invoke a server-side script?
Regards.
 
A

Alan Kennedy

[Andrew Chalk]
I want to run the script on the server.

Then you will need to figure out how to run the script on the server
;-)

If you're not aware of how to do this, then it's probably best for you
to read up on CGI, which is way of getting web servers to execute
scripts (in any language) when the scripts URL is requested.
The problem is that I don't
have an html page as an HREF. Viz:

<tr><A HREF="Fred.htm">Fred</A>
What replaces Fred.htm to invoke a server-side script?

Whatever is the URL of your script on your server. Examples include

http://www.mydomain.com/cgi-bin/myscript.py
http://www.mydomain.com/cgi-bin/myscript.cgi
http://www.mydomain.com/cgi-bin/myscript.py?paramA=v1&paramB=v2

Your python script should then generate (i.e. print out) an HTML page
when it is run (actually it can generate anything, images included,
but HTML is a good place to start). The server takes the output of
your script and sends it to the requesting browser. The browser has no
way of knowing that the HTML did not come from a static file.

You can read about Python and CGI here:-

http://starship.python.net/crew/davem/cgifaq/faqw.cgi?req=index

There are other "linkage mechanisms" by which you can cause the
execution of scripts on a server, e.g. mod_python, but none is
conceptually as simple as CGI. Also, CGI tends to be more portable
between different server software, and is almost always supported by
hosting providers.

HTH,
 
J

John Roth

Andrew Chalk said:
Thanks. I want to run the script on the server. The problem is that I don't
have an html page as an HREF. Viz:

<tr><A HREF="Fred.htm">Fred</A>
What replaces Fred.htm to invoke a server-side script?
Regards.

That's not a Python issue. It's a web server configuration
issue. You should be able to configure your web server
to recognize href="fred.py" as a request to run the fred.py
script using the Python interpreter, and pass back whatever
web page, graphic or other file it outputs. For that matter,
you might even be able to configure it to recognize
"Fred.htm" as a python script, and rewrite the name
to "fred.py."

John Roth
 
A

Andrew Chalk

Thanks!
Alan Kennedy said:
[Andrew Chalk]
I want to run the script on the server.

Then you will need to figure out how to run the script on the server
;-)

If you're not aware of how to do this, then it's probably best for you
to read up on CGI, which is way of getting web servers to execute
scripts (in any language) when the scripts URL is requested.
The problem is that I don't
have an html page as an HREF. Viz:

<tr><A HREF="Fred.htm">Fred</A>
What replaces Fred.htm to invoke a server-side script?

Whatever is the URL of your script on your server. Examples include

http://www.mydomain.com/cgi-bin/myscript.py
http://www.mydomain.com/cgi-bin/myscript.cgi
http://www.mydomain.com/cgi-bin/myscript.py?paramA=v1&paramB=v2

Your python script should then generate (i.e. print out) an HTML page
when it is run (actually it can generate anything, images included,
but HTML is a good place to start). The server takes the output of
your script and sends it to the requesting browser. The browser has no
way of knowing that the HTML did not come from a static file.

You can read about Python and CGI here:-

http://starship.python.net/crew/davem/cgifaq/faqw.cgi?req=index

There are other "linkage mechanisms" by which you can cause the
execution of scripts on a server, e.g. mod_python, but none is
conceptually as simple as CGI. Also, CGI tends to be more portable
between different server software, and is almost always supported by
hosting providers.

HTH,
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top