[Tutor] python scripts in HTML documents
Erik Price
erikprice@mac.com
Sun, 19 May 2002 22:09:23 -0400
On Sunday, May 19, 2002, at 10:34 AM, john public wrote:
> Can I insert Python code into an HTML document like I can insert a
> piece of JavaScript code into an HTML document? JavaScript is a
> scripting language, and Python is a scripting language and also more.
> Yes?
Scripting languages are also called interpreted languages. This is
because you don't compile the code as a separate step, you just submit
the code to an interpreter and the interpreter executes the appropriate
actions based on the code. Python works this way, as does JavaScript.
The difference between Python and JavaScript is that there is a
JavaScript interpreter in most web browsers, but no Python interpreter
that I have ever heard of. JavaScript has somehow become something of a
standard client-side scripting language for web pages, but Python (and
PHP and Perl) is for the most part limited to performing server-side
scripting. For instance, you could create a Python script that
generates a web page when the following HTTP request is made:
http://domain.com/page.py?article=11232
What this request would do is send a variable "article" whose value is
"11232" to a script called "page.py" at URL "domain.com". What
"page.py" does with this variable could be anything, but possibly it
could search a database for an article (using 11232 as the key of the
article in the database), read the article, insert the contents of the
article into a dynamically-generated string of HTML, and then send that
HTML code (including the article) to your browser so that it looks like
you have received a web page -- even though there may be no
"traditional" web page whatsoever on that server (only scripts like this
that imitate web pages by dynamically generating HTML from database
content).
To get back to your question: if someone ever created a browser with a
Python interpreter built in, then yes you could do the kinds of things
that you do with JavaScript but in Python instead. But I doubt that
this will happen -- one of the nice things about JavaScript, from a
security point of view, is that it is very limited in what it can and
cannot do on the user's computer. For instance, the JavaScript
implementation in most modern browsers cannot read or write any files on
a user's hard disk without some kind of permission on the part of the
user. This makes JavaScript a very safe scripting language from the
perspective that it would be hard for a hacker or other malificant to do
damage to someone using it. And of course, you can always turn your
browser's JavaScript interpreter off, which means that none of the
JavaScript code will work.
Erik