a little more help with python server-side scripting

J

John Salerno

I contacted my domain host about how Python is implemented on their
server, and got this response:

-------------------
Hello John,

Please be informed that the implementation of python in our server is
through mod_python integration with the apache.

These are the steps needed for you to be able to run .py script directly
from browser for your webpage:

1. Please use the below mentioned path for python:
#!/usr/bin/env python

Furthermore, update us with the script path, so that we can set the
appropriate ownership and permissions of the script on the server.

If you require any further assistance, feel free to contact us.
 
B

Ben Cartwright

John said:
I contacted my domain host about how Python is implemented on their
server, and got this response:

-------------------
Hello John,

Please be informed that the implementation of python in our server is
through mod_python integration with the apache.

These are the steps needed for you to be able to run .py script directly
from browser for your webpage:

1. Please use the below mentioned path for python:
#!/usr/bin/env python

Furthermore, update us with the script path, so that we can set the
appropriate ownership and permissions of the script on the server.

If you require any further assistance, feel free to contact us.
-----------------------

Unfortunately, I don't completely understand what it is I need to do
now. Where do I put the path they mentioned? And what do they mean by my
script path?


The Python tutorial should fill in the blanks
(http://www.python.org/doc/tut/node4.html):
2.2.2 Executable Python Scripts

On BSD'ish Unix systems, Python scripts can be made directly executable,
like shell scripts, by putting the line

#! /usr/bin/env python

(assuming that the interpreter is on the user's PATH) at the beginning
of the script and giving the file an executable mode. The "#!" must be
the first two characters of the file. On some platforms, this first line
must end with a Unix-style line ending ("\n"), not a Mac OS ("\r") or
Windows ("\r\n") line ending. Note that the hash, or pound, character,
"#", is used to start a comment in Python.

This answers your first question. Put the #! bit at the top of your
..py script. This way the web server will know how to run the script.
The script can be given a executable mode, or permission, using the
chmod command:

$ chmod +x myscript.py

And this answers your second. Your host needs to know the path to your
script so they can use chmod to make it executable.

--Ben
 
S

Steve Holden

John said:
I contacted my domain host about how Python is implemented on their
server, and got this response:

-------------------
Hello John,

Please be informed that the implementation of python in our server is
through mod_python integration with the apache.

These are the steps needed for you to be able to run .py script directly
from browser for your webpage:

1. Please use the below mentioned path for python:
#!/usr/bin/env python

Furthermore, update us with the script path, so that we can set the
appropriate ownership and permissions of the script on the server.

If you require any further assistance, feel free to contact us.
-----------------------

Unfortunately, I don't completely understand what it is I need to do
now. Where do I put the path they mentioned? And what do they mean by my
script path?

Don't worry, it looks as though they don't completely understand either :)

Fortunately they've given you the information you need to run CGI
scripts. Try installing this script in your cgi-bin directory as test.py
(you may have to set it executable):

#!/usr/bin/env python
#
import os
print "Content-Type: text/plain"
print
for t in os.environ.items():
print "%s=%s" % t

If it runs when you access http://yourdomain/cgi-bin/test.py it looks
like you're good to go!

regards
Steve
 
J

John Salerno

Ben said:
And this answers your second. Your host needs to know the path to your
script so they can use chmod to make it executable.

Where does this line go? Just at the top as well?
 
J

John Salerno

Steve said:
Fortunately they've given you the information you need to run CGI
scripts. Try installing this script in your cgi-bin directory as test.py
(you may have to set it executable):

Thank you! I desperately needed to test it, and that seemed to work. I
didn't have to make it executable though. I wonder why?
 
S

Steve Holden

John said:
Ben Cartwright wrote:




Where does this line go? Just at the top as well?

Nope. I presume you can log in to your web server using ssh or telnet or
similar. In which case you do so. Then use the commands

cd {wherever}/cgi-bin
chmod +x test.py

to make the script executable, and therefore recognised as a proper
script by Apache.

regards
Steve
 
S

Steve Holden

John said:
Steve Holden wrote:




Thank you! I desperately needed to test it, and that seemed to work. I
didn't have to make it executable though. I wonder why?

Probably because the cgi-bin directory is specially marked to contain
executables. If you drop the script in another directory you will almost
certainly just see the source of the script. Making it executable
*might* cause it to run, but that would depend on exactly how your
server is configured.

Certainly my sites all have a line like

ScriptAlias /cgi-bin/ "c:/apache/cgi-bin/"

in the httpd.conf file. ScriptAlias tells Apache that the files in the
directory are scripts. The first argument is the address in web-space,
the second the address on disk.

You'll find you can affect *some* configuration items by creating files
called .htaccess in your web content directories, but that's a ways down
the road yet.

If the script ran, you will now know waht version of Apaceh you're
running with!

regards
Steve
 
J

John Salerno

Steve said:
Nope. I presume you can log in to your web server using ssh or telnet or
similar. In which case you do so. Then use the commands

cd {wherever}/cgi-bin
chmod +x test.py

to make the script executable, and therefore recognised as a proper
script by Apache.

I'm afraid I still don't understand. I guess this step isn't necessary,
since the script seemed to run anyway, but I'd like to know this anyway.
The way I log into my server space is by an FTP program, so I don't see
an occasion to actually *type* in any kind of commands.
 
J

John Salerno

Steve said:
If the script ran, you will now know waht version of Apaceh you're
running with!

Well, the script did seem to run, but I'm still not sure if this is what
I'm ultimately after. This allows me to run Python script files, which
is good, but what I really want to do is write bits of Python code in my
HTML files (as a means to include headers and footers, for example). So
these bits of Python code will basically be like a PHP include()
function that calls another HTML file which contains the header/footer
HTML to insert into the calling file. This type of situation doesn't
involve calling external Python scripts.

Maybe I can just try this also and see if it works, but I don't know the
code to use to write such an include statement. What would the
equivalent of this be in Python:

<?php include('file.html'); ?>

Thanks.
 
S

Steve Holden

John said:
Steve Holden wrote:




Well, the script did seem to run, but I'm still not sure if this is what
I'm ultimately after. This allows me to run Python script files, which
is good, but what I really want to do is write bits of Python code in my
HTML files (as a means to include headers and footers, for example). So
these bits of Python code will basically be like a PHP include()
function that calls another HTML file which contains the header/footer
HTML to insert into the calling file. This type of situation doesn't
involve calling external Python scripts.

Maybe I can just try this also and see if it works, but I don't know the
code to use to write such an include statement. What would the
equivalent of this be in Python:

<?php include('file.html'); ?>

Thanks.

There are various ways you can do this, each of which depends on having
a particular framework in place. Since your web service provider tells
you that mod_python is available it's likely that you'll be able to use
PSP (one of several beasts known as "Python Server Pages").

This is briefly described (for a flavor of the technology) in

http://www.python.org/pycon/dc2004/papers/14/

by the author of mod_python. If you like what you see then take a look
at the full mod_python documentation, at

http://www.modpython.org/live/current/doc-html/

Note that purists might suggest this isn't the best way to use Python on
the web. If it gets you where you want to be, feel free to ignore them :)

regards
Steve
 
D

Dennis Lee Bieber

1. Please use the below mentioned path for python:
#!/usr/bin/env python
Typical UNIX/LINUX "shebang" line. As mentioned, just stuff that as
the first line of your Python file; it tells the system how to access
the program the interprets the file. Instead of:

python program.py

on a command line, one would use

program.py

and the command shell would read the first line to figure out what
interpreter is needed to run it.
Furthermore, update us with the script path, so that we can set the
appropriate ownership and permissions of the script on the server.
The permissions likely have to be set up so that Apache server
(running as a different, special, user) can execute files in "your" web
directory.

This also may imply that you will want to fully test your program on
a local machine (you need a simple web server that can launch Python
programs under CGI conditions) -- because each time you upload a changed
file, the permissions likely will be reset to world-read-only, and you
need to ask the admins to reset the file for execute by Apache. They
aren't going to be happy if you send them an email every 10 minutes
explaining you fixed a bug in the script and uploaded a new version.
--
 
J

John Salerno

Steve said:
Note that purists might suggest this isn't the best way to use Python on
the web. If it gets you where you want to be, feel free to ignore them :)

Thanks for the info. Basically I don't plan to do big stuff with Python
on the internet (at least not right now while I'm still learning the
language). I already know how to use a PHP include, so I could just keep
doing that, but I figure since I'm learning Python, I might as well
start using it wherever I can, assuming that it is okay to use it this way.
 
J

John Salerno

John said:
I contacted my domain host about how Python is implemented on their
server, and got this response:

Uh, okay, I asked a related question to them and now I got this:

------------
Hello John,

There are some corrections based on last reply.

The python installation on our windows hosting is not configured as
mod_python. Howevet, it is directly map with a website extensions
mapping in IIS. Save your python script as .py file on the wwwroot and
you may call the scripts using http://www.yourdomain.com/yourfile.py

If you have any enquiries, please do not hesitate to contact.

Best regards,

Mackenzie S.
Support Executive
-----------

Does that really change anything?

What I had asked was if I could just embed Python code within my HTML
files, like you do with PHP, but they didn't address that yet.
 
S

Sybren Stuvel

John Salerno enlightened us with:
What I had asked was if I could just embed Python code within my
HTML files, like you do with PHP, but they didn't address that yet.

Check out PSP.

Sybren
 
D

Dennis Lee Bieber

I'm afraid I still don't understand. I guess this step isn't necessary,
since the script seemed to run anyway, but I'd like to know this anyway.
The way I log into my server space is by an FTP program, so I don't see
an occasion to actually *type* in any kind of commands.

Which may be one reason you are expected to have the admin staff do
that for you, after you've uploaded the file.

Another matter (this is hypothetical, I don't have the experience
for it) may depend on whether your test had some stray authentication
information -- if the server was able to run the CGI "as you" (due to
some login cookie), the privileges may not need to be set; but will fail
for other users.
--
 
D

Dennis Lee Bieber

mapping in IIS. Save your python script as .py file on the wwwroot and

Ugh... M$ server... Suggest google or M$ help pages on how to manage
external script language with IIS for some information.
--
 
S

Steve Holden

John said:
Uh, okay, I asked a related question to them and now I got this:

------------
Hello John,

There are some corrections based on last reply.

The python installation on our windows hosting is not configured as
mod_python. Howevet, it is directly map with a website extensions
mapping in IIS. Save your python script as .py file on the wwwroot and
you may call the scripts using http://www.yourdomain.com/yourfile.py

If you have any enquiries, please do not hesitate to contact.

Best regards,

Mackenzie S.
Support Executive
Well, it means that PSP is out, unfortunately.
What I had asked was if I could just embed Python code within my HTML
files, like you do with PHP, but they didn't address that yet.

OK, what you need to ask them is whether they have installed Python as
an Active Scripting language. If they have then you can use it pretty
much like VBscript.

regards
Steve
 
J

John Salerno

Steve said:
OK, what you need to ask them is whether they have installed Python as
an Active Scripting language. If they have then you can use it pretty
much like VBscript.

Here's there latest:

----------
Please note that it is possible for the server to recognize bits of
Python code that are embedded within an HTML file. However, it will need
a custom mapping on the domain for mapping .html to the Python
executable on the server. But note that after mapping .html will no
longer be call as normal html file to be loaded on the browser as the
IIS server now will parse .html with the Python executable engine. Your
user will need to embed the Python coding into each of the .html file in
order for it to be load properly on the browser.
 
D

Dennis Lee Bieber

Does that answer the question about active scripting language?

Slipping in... It sure sounds to me like they have Python as a pure
CGI language.

That is:
You must reference the script with a URL having .../script.py (and
arguments will possibly be sent appended to this, but should have been
parsed out and become part of the standard CGI environment).

You must generate, as stdout from the script, the headers AND HTML
page that is to be displayed in response to the inbound data. You DO NOT
have "snippets" of Python scattered about within an HTML file.

If you really want to keep HTML files separate from Python scripts,
I'd suggest using a templating system (I've mentioned CherryTemplate
before) since the template file mostly pure HTML with special template
codes for the dynamic data. The Python script, then, in response to the
CGI data, builds up whatever is the proper dynamic data (in a
dictionary, say), invokes the CherryTemplate rendering function, passing
it the template (HTML) file and the dictionary; the output of that
function is what you write out as the web page to be displayed.
--
 
M

Magnus Lycka

Dennis said:
Slipping in... It sure sounds to me like they have Python as a pure
CGI language.

That is:
You must reference the script with a URL having .../script.py (and
arguments will possibly be sent appended to this, but should have been
parsed out and become part of the standard CGI environment).

If you want existing links to continue to work, you can simply
place HTML redirects in the current HTML files, so that they
invoke the proper Python CGI script, i.e. somepage.html should
contain a redirect to /my_cgi.py?page=somepage

I've roughly described in a previous posting how my_cgi.py should
work. Just let it suck in the file with the 'somepage' content,
and add the header and footer stuff.

You'll find Python CGI tutorials on the net, and the library
reference chapters on cgi and cgitb will also help. Start by
enabling cgitb...
 

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