a little more help with python server-side scripting

M

Magnus Lycka

John said:
The python installation on our windows hosting is not configured as
mod_python.

Perhaps you can switch to linux hosting instead?
If they have mod_python in that environment, you
can use Python Server Pages, and whatever tools
you use, there is much better support for Apache
than for IIS in Python (and many other) web
development tools. After all, Apache has almost 70%
of the web server share, and IIS has about 20%...
http://news.netcraft.com/archives/web_server_survey.html

For mod_python's PSP support, see
http://www.modpython.org/live/current/doc-html/pyapi-psp.html
 
J

John Salerno

John said:
I found a PSP website that said this:

---------
In general, PSP pages are like normal HTML files with two exceptions:

1. They always have a .PSP file extension
2. They can have JPython code embedded in them

After a little more investigation, this seems to be just one
implementation of PSP. I need to just figure out how to get 'regular'
PSP working for me, or if it's even possible on a Windows 2003 server.
 
S

Sybren Stuvel

John Salerno enlightened us with:
That sounds like just what I want, except do I have to write my code
in Jython? Can't I just use regular Python?

I wouldn't know, never used Python ;-)

Sybren
 
M

Magnus Lycka

John said:
After a little more investigation, this seems to be just one
implementation of PSP. I need to just figure out how to get 'regular'
PSP working for me, or if it's even possible on a Windows 2003 server.

I don't think there is such a thing as "regular PSP".
If they've enabled Python as a windows scripting language,
you can use Python instead of VBScript or JScript in ASP.

Another option might be http://spyce.sourceforge.net/index.html
 
J

John Salerno

John said:
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?

Ok, seems like the verdict is that the server doesn't have mod_python
nor does it detect the .psp extension. It can, however, detect the .py
extension. But does this mean I can simply include Python code in my
HTML files, and then give my HTML files a .py extension? I don't know
how to test this because I don't know how to write inline code yet. Is
it something like:

<%@ include file=something %>

? They say:

"Please note that you may map .py to .html extension. However, if you
set this mapping, .html will not work with the normal html tags anymore."

But I don't know what this means about 'mapping' the extension and what
it means that html tags won't work anymore.
 
P

Paul Boddie

John said:
Ok, seems like the verdict is that the server doesn't have mod_python
nor does it detect the .psp extension. It can, however, detect the .py
extension.
From what I understand of the situation, noting that I haven't used IIS
with Python at all as far as I remember (and it'd be about six or seven
years ago if I did), you're using Windows hosting where .py files get
special treatment - ie. they don't get served up as text files but are
executed somehow - and that this is probably configured by your
provider as described in this document (found via Google using the
search text "IIS Python"):

http://support.microsoft.com/default.aspx?scid=kb;en-us;276494
But does this mean I can simply include Python code in my
HTML files, and then give my HTML files a .py extension?

No, it means that you either write Python programs which produce pages
as output, according to the CGI specification, or that you embed Python
code inside ASP files, as described in the above document.
I don't know how to test this because I don't know how to write inline code yet. Is
it something like:

<%@ include file=something %>

I haven't used ASP, and I've been working hard to remove from my memory
any trace of similar systems (eg. JSP), but I imagine that if you want
to include normal Python programs inside ASP files, there may be a way
of using some kind of "include" directive. Take a look at the above
document for some ideas.
? They say:

"Please note that you may map .py to .html extension. However, if you
set this mapping, .html will not work with the normal html tags anymore."

But I don't know what this means about 'mapping' the extension and what
it means that html tags won't work anymore.

I think they're just trying to say that if you decide that your Python
programs should have a .html extension and still be executed as Python
programs, don't expect the server to serve up HTML pages any more.

Paul
 
J

John Salerno

Paul said:
No, it means that you either write Python programs which produce pages
as output, according to the CGI specification, or that you embed Python
code inside ASP files, as described in the above document.
I haven't used ASP, and I've been working hard to remove from my memory
any trace of similar systems (eg. JSP), but I imagine that if you want
to include normal Python programs inside ASP files, there may be a way
of using some kind of "include" directive. Take a look at the above
document for some ideas.

Why do you mention ASP though? Can't this be done without it?
 
G

Gerard Flanagan

John said:
Ok, seems like the verdict is that the server doesn't have mod_python
nor does it detect the .psp extension. It can, however, detect the .py
extension. But does this mean I can simply include Python code in my
HTML files, and then give my HTML files a .py extension?
No.

[...]


? They say:

"Please note that you may map .py to .html extension. However, if you
set this mapping, .html will not work with the normal html tags anymore."

But I don't know what this means about 'mapping' the extension and what
it means that html tags won't work anymore.

John

There is a configuration section in the IIS management tool
(inetmgr.exe) called 'Application Mappings' which maps file extensions
to applications. For example, on my (WinXP) machine I have:

.aspx -->
C:\WINDOWS\Microsoft.NET\Framework\v1.14322\aspnet_isapi.dll
.asp --> C:\WINDOWS\system32\inetsrv\asp.dll
.php --> C:\Program Files\PHP\Php51\php-cgi.exe

among others. So if IIS receives a request for a url with one of these
extensions it passes that request on to the relevant program. You
could map '.py' to 'python.exe %s %s' but any '.py' files would have to
be pure Python.

IIS doesn't need any help with standard, static .html requests, it can
serve those itself. It may be possible to map '.html' to any of the
three applications above and still have static HTML pages function
identically (even if this would create a pointless overhead ), however
you couldn't map '.html' to 'python.exe' and expect anything meaningful
- there is nothing like 'python-cgi.exe' that understands HTML with or
without inlined python code.

Gerard
 
P

Paul Boddie

John said:
Why do you mention ASP though? Can't this be done without it?

You mean, can't you just write normal HTML files, add some kind of
"include" directive, call them something ending in .html, and then have
the server produce a combination of normal HTML and the output from
Python code? Well, not really, since that (apart from the .html
extension on the filename) is a description of what ASP is about, more
or less.

But yes, if you want to run Python code without doing strange things
with ASP files, then just write a program (not a Web page, but an
actual Python program) as described in the Microsoft document (or
virtually any introduction to CGI with Python), upload it into the
appropriate folder/directory with a filename ending in .py and test it
out! See this recipe for other examples (found using the search text
"CGI Python" with Google):

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52220

Paul
 
M

Magnus Lycka

John said:
> Ok, seems like the verdict is that the server doesn't have mod_python
nor does it detect the .psp extension. It can, however, detect the .py
extension. But does this mean I can simply include Python code in my
HTML files, and then give my HTML files a .py extension? I don't know
how to test this because I don't know how to write inline code yet. Is
it something like:

No, you can't do that. From your descriptions, it seems that Python
is set up as a CGI language, just as one would use Perl etc. This
means that if you have an URL ending with x.py, the web server will
do these things when you request this URL from the server:

1. Set up a number of environment variables which are useful for the
python script it will call. Very important if the URL was invoked
from an HTML form, which I don't suspect will happen on your site.
2. Invoke Python with your script. Basically it should call
python.exe -u x.py
3. Read the data that x.py sends to stdout, e.g. print statements
etc. Provided that this output looks ok, it will send it to the
browser, otherwise it will typically send ERROR 500 (internal
server error) instead.

You were given a CGI script already that printed out environment
variables. I got the impression that you tried that out. If you
set the environment variables shown by that CGI script using the
SET command at a Windows command prompt, and then execute your
python script at that command prompt, it has to print something like

----
Content-type: text/html

<html>
blah blah whatever
</html>
----

There is no magic beyond this. Try renaming an HTML file to .py
and invoke Python with that. It will just say SyntaxError! The
change isn't big though! You just need to make the HTML content
into a big string (surround it with """) and print the content-type
line, an empty line and this big string, and you have a start.

You have already been provided with all the information you need.
I think you need to look closely at the information Paul and several
others have given you instead of just asking more questions, and
really start using it. You won't get anywhere otherwise.

The other option is ASP. You have been given information about
that already. Be aware that ASP does not imply VBScript. You can
use Python in ASP as long as that's enabled. It seems to be exactly
what you are asking for, so I don't understand why you seem to reject
it. Don't you like the file name endings?
 
J

John Salerno

Magnus said:
The other option is ASP. You have been given information about
that already. Be aware that ASP does not imply VBScript. You can
use Python in ASP as long as that's enabled. It seems to be exactly
what you are asking for, so I don't understand why you seem to reject
it. Don't you like the file name endings?

Maybe I'm misunderstanding what is meant when you say to use ASP. I'm
thinking that it involves having to learn another language (such as C#
with ASP.NET), instead of writing my code in Python. Is that not the case?
 
M

Magnus Lycka

John said:
Maybe I'm misunderstanding what is meant when you say to use ASP. I'm
thinking that it involves having to learn another language (such as C#
with ASP.NET), instead of writing my code in Python. Is that not the case?

Nope. Just like CGI, ASP is language agnostic. It's a Microsoft
standard for embedding code in HTML pages. Look at the references
earlier in the thread and/or google for Python ASP.

Of course, the catch here is that your ISP might not have
enabled Python as a scripting language on the servers...

Finally, it's my impression that your ambition is to use some
kind of scripting to get a uniform look and navigation etc in
your web pages. This might not require any kind of dynamic
web site.

In that case I'd suggest that you simply make build some
Python tool on your own computer that builds a static version
of your entire web site in the safe and free environment of
your home. If you want to change something, you simply edit
a file at home, rebuild your site and upload the new files.

I've built a few sites like that, and if you don't really
need to serve anything but static pages, it's probably your
best bet.
- It'll give you a snappy site. It just serves static pages.
- You don't rely on any particular technology from your ISP,
they just need FTP etc so that you can upload your files,
and a plain web server.
- You have the original content at home, so you always have
a backup of your web site.
- You can use whatever tools you like to create your data
without involving your ISP.

Personally, I think HTML is a crappy language to write text
in. I prefer to use something like reST, see
http://docutils.sourceforge.net/rst.html

From reST there are convenient tools to get both HTML and
PDF documents--good for CVs etc. The standard tool doesn't
make room for those custom headers or footers though. There
is some help there though. Look here:
http://docutils.sourceforge.net/docs/user/links.html
 
K

Kent Johnson

John said:
But isn't this code:

Response.Write('Python Test<br>')
Response.write('<h3>Smaller heading</hr>')

written using ASP instead of Python?

No, it's Python code that calls the Write() method on the Response
object that is exposed by ASP.

Kent
 
G

Gerard Flanagan

John said:
But isn't this code:

Response.Write('Python Test<br>')
Response.write('<h3>Smaller heading</hr>')

written using ASP instead of Python?


Here's a demo script called 'tut1.asp' located in
'site-packages\win32comext\axscript\Demos\client\asp':

<HTML>

<SCRIPT Language="Python" RUNAT=Server>

for i in range(3,8):
Response.Write("<FONT SIZE=%d>Hello World!!<BR>" % i)

</SCRIPT>

</HTML>

###################

and here is 'CreateObject.asp' from the same directory:

<HTML>

<SCRIPT Language="Python" RUNAT=Server>

# Just for the sake of the demo, our Python script engine
# will create a Python.Interpreter COM object, and call that.

# This is completely useless, as the Python Script Engine is
# completely normal Python, and ASP does not impose retrictions, so
# there is nothing the COM object can do that we can not do natively.

o = Server.CreateObject("Python.Interpreter")

Response.Write("Python says 1+1=" + str(o.Eval("1+1")))

</SCRIPT>

</HTML>


Gerard
 
M

Magnus Lycka

John said:
But isn't this code:

Response.Write('Python Test<br>')
Response.write('<h3>Smaller heading</hr>')

written using ASP instead of Python?

Listen John. This will be the last time I respond
to this thread, since you just ask more and more
instead of digesting all the information you are
given. Why should we spend time on answers if you
don't read them properly anyway.

I understand that you are confused, but you need to
analyze the material you have been provided. ASP is
not a programming language. The code above shows the
Python ASP API.

Most ASP developers use VBScript, and since ASP is one
of those technologies Microsoft tosses at clueless
people, I'm sure many people who use VBScript in ASP
don't understand the distinction, and those who hire
ASP developers or buy web site development often don't.
These guys will probably also think that Sun's language
Java and Netscape Corp's JavaScript are the same things
too. Clueless.

The example is a bit boring of course, since it doesn't
show anything except a few calls that probably look
almost the same in most languages that can be used in
ASP. If there had just been a loop or something...

Whatever web tool kit you use, there will be an API
that you must learn. An interface for communicating with
these mechanisms that provide the web server interface.

In the web context, there is some sort of request made
from a browser (or spider or whatever) and based on that,
the Python script (CGI, ASP, SkunkWeb PSP or whatever)
must handle the request (if there are parameters to care
about) and produce some kind of response.

In a CGI script, you work on a very low level, and need
to print content-type headers, error codes etc. In ASP,
your API takes care of the details, and you just print
the "meat". This abstraction is managed through the
Response object. I'm sure you can find plenty of help
on the web. Use google.
 
J

John Salerno

Magnus said:
Listen John. This will be the last time I respond
to this thread, since you just ask more and more
instead of digesting all the information you are
given. Why should we spend time on answers if you
don't read them properly anyway.

Well, I appreciate the help. I'm trying to figure it out, but it seems
like with each new post, there is some new technology being mentioned or
new method to do what I'm trying to do. I guess I'm not well-versed
enough in Python to even attempt this kind of thing yet. I didn't
realize it would be this complicated. I guess I hoped it would be as
easy as including some PHP code and letting the server detect the .php
extension and handle it all. I'm going to try to solidify the basics
first, then move on to web programming a little later.
 

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,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top