Coming from Perl

A

Amer Neely

I'm a complete newbie with Python, but have several years experience
with Perl in a web environment.

A question I have, if someone here is familiar with Perl, does Python
have something like Perl's 'here document'? I've just searched and read
some postings on generating HTML but they all seem to refer to various
template utilities. Is this the only way, or am I missing something? I'm
used to generating X/HTML by hand, which makes the here document in Perl
ideal for me. Also, many times a client already existing HTML code that
I can use in a script.
 
B

Ben Finney

Amer Neely said:
A question I have, if someone here is familiar with Perl, does Python
have something like Perl's 'here document'?

I'm not sure exactly what behaviour you want, but Python accepts
literal strings to be triple-quoted (i.e. delimited by """ pairs or
''' pairs), which allows you to freely use literal line feed and quote
characters inside that string.

<URL:http://docs.python.org/ref/strings.html>
 
T

TheFlyingDutchman

I'm a complete newbie with Python, but have several years experience
with Perl in a web environment.

A question I have, if someone here is familiar with Perl, does Python
have something like Perl's 'here document'? I've just searched and read
some postings on generating HTML but they all seem to refer to various
template utilities. Is this the only way, or am I missing something? I'm
used to generating X/HTML by hand, which makes the here document in Perl
ideal for me. Also, many times a client already existing HTML code that
I can use in a script.

--
Amer Neely
w:www.webmechanic.softouch.on.ca/
Perl | MySQL programming for all data entry forms.
"Others make web sites. We make web sites work!"

I am not sure if this is what you are looking for, but Python has a
special string with 3 quotes that I believe duplicates part of the
functionality of a here document:

myHmtlHeader = """
<head attribute = "abc">
<title>My Page</title>
</head>
"""

print myHtmlHeader


outputs:


<head attribute="abc">
<title>My Page</title>
</head>
 
A

Amer Neely

Ben said:
I'm not sure exactly what behaviour you want, but Python accepts
literal strings to be triple-quoted (i.e. delimited by """ pairs or
''' pairs), which allows you to freely use literal line feed and quote
characters inside that string.

<URL:http://docs.python.org/ref/strings.html>

I saw that and I guess that is the closest thing. In Perl I can do ..

print <<EndHTML;
<html>
<body>
Hello
</body>
</html>
EndHTML

In this case 'EndHTML' is a label, and I'm telling perl to print
everything as is up to that label.
 
A

Amer Neely

TheFlyingDutchman said:
I am not sure if this is what you are looking for, but Python has a
special string with 3 quotes that I believe duplicates part of the
functionality of a here document:

myHmtlHeader = """
<head attribute = "abc">
<title>My Page</title>
</head>
"""

print myHtmlHeader


outputs:


<head attribute="abc">
<title>My Page</title>
</head>

Yep, I think that is the way I will have to go. Thanks.
 
B

Ben Finney

Amer Neely said:
print <<EndHTML;
<html>
<body>
Hello
</body>
</html>
EndHTML

The equivalent in Python would be::

print """
<html>
<body>
Hello
</body>
</html>
"""

You can even use Python's standard textwrap module to indent your code
properly::

import textwrap

def foo(bar):
if baz:
print textwrap.dedent("""
<html>
<body>
Hello
</body>
</html>
""")

The 'textwrap.dedent' function strips *common* leading whitespace from
all lines in the string, so in the above example the resulting string
will be as though the original string were typed flush to the left
margin.

See 'help(textwrap.dedent)' for what that function does, or read it
online <URL:http://docs.python.org/lib/module-textwrap>.
 
R

Ryan Ginstrom

On Behalf Of Amer Neely
I saw that and I guess that is the closest thing. In Perl I can do ..

print <<EndHTML;
<html>
<body>
Hello
</body>
</html>
EndHTML <head>
<title>%(title)s</title>
</head>
<body>
%(content)s
<html>
<head>
<title>Python Rocks!</title>
</head>
<body>
<p>I heart Python</p>

You might also want to check out string.Template
(http://docs.python.org/lib/node40.html), or even move on to one of the
many, many templating languages (Mako, Cheetah, Kid, etc.)

Regards,
Ryan Ginstrom
 
A

Amer Neely

TheFlyingDutchman said:
I am not sure if this is what you are looking for, but Python has a
special string with 3 quotes that I believe duplicates part of the
functionality of a here document:

myHmtlHeader = """
<head attribute = "abc">
<title>My Page</title>
</head>
"""

print myHtmlHeader


outputs:


<head attribute="abc">
<title>My Page</title>
</head>

Well, I have checked everything I can but I'm getting '500 Internal
Server Error'. The log files aren't helpful:
[Thu Sep 13 03:43:00 2007] [error] [client 24.235.184.39] Premature end
of script headers: /home/softouch/public_html/cgi-bin/scratch/hello.py

I can't even get it to run on my home PC running Apache + Win2K. Same error.

My script:
#!/usr/bin/python
import cgitb; cgitb.enable(display=0, logdir=".")
import sys
sys.stderr = sys.stdout
print "Content-Type: text/html"
print

print """
<html>
<body>
<div align="center"><font style="font-family:verdana; size:18px">Hello
from Python</font></div>
<br>
Goodbye.
</body>
</html>
"""
 
A

Amer Neely

Amer said:
TheFlyingDutchman said:
I am not sure if this is what you are looking for, but Python has a
special string with 3 quotes that I believe duplicates part of the
functionality of a here document:

myHmtlHeader = """
<head attribute = "abc">
<title>My Page</title>
</head>
"""

print myHtmlHeader


outputs:


<head attribute="abc">
<title>My Page</title>
</head>

Well, I have checked everything I can but I'm getting '500 Internal
Server Error'. The log files aren't helpful:
[Thu Sep 13 03:43:00 2007] [error] [client 24.235.184.39] Premature end
of script headers: /home/softouch/public_html/cgi-bin/scratch/hello.py

I can't even get it to run on my home PC running Apache + Win2K. Same
error.

My script:
#!/usr/bin/python
import cgitb; cgitb.enable(display=0, logdir=".")
import sys
sys.stderr = sys.stdout
print "Content-Type: text/html"
print

print """
<html>
<body>
<div align="center"><font style="font-family:verdana; size:18px">Hello
from Python</font></div>
<br>
Goodbye.
</body>
</html>
"""

I should have added that it runs from the command line OK.
 
B

Ben Finney

Amer Neely said:
Well, I have checked everything I can but I'm getting '500 Internal
Server Error'.

This is the HTTP response code saying that the program that should
have served the response document instead exited with an error.

To debug, you should first run the program (or at least the part that
you're trying to implement) *outside* the context of a web server. Do
it at a command line, and any errors will show up as exception
tracebacks.

Only when you have something that performs correctly outside a web
server context should you consider how to wrap it in that new
(harder-to-debug) context.
 
A

Amer Neely

Ben said:
This is the HTTP response code saying that the program that should
have served the response document instead exited with an error.

To debug, you should first run the program (or at least the part that
you're trying to implement) *outside* the context of a web server. Do
it at a command line, and any errors will show up as exception
tracebacks.

Only when you have something that performs correctly outside a web
server context should you consider how to wrap it in that new
(harder-to-debug) context.

That is where I'm stuck. It runs fine on my home computer, but I don't
have shell access to my hosted site.

Can you or anyone see anything in the posted code that might be the
cause? Why is it so much harder to debug? There is no syntax error that
I can tell. I've set the file permissions to rwxr-xr-x. I've printed the
correct content type. It's a simple print statement, as per several
examples I've seen.

Is it an Apache thing? I've not seen any mention in the documentation on
that.
 
R

Richie Hindle

[Amer]
Can you or anyone see anything in the posted code that might be the
cause?
#!/usr/bin/python
import cgitb; cgitb.enable(display=0, logdir=".")
import sys
sys.stderr = sys.stdout
print "Content-Type: text/html"
print

My guess would be that you don't have cgitb in your server environment, or
that you have a bogus one. Rearrange things like this:

#!/usr/bin/python
print "Content-Type: text/html"
print
import sys
sys.stderr = sys.stdout
import cgitb; cgitb.enable(display=0, logdir=".")
 
C

Cameron Laird

Amer said:
TheFlyingDutchman said:
I'm a complete newbie with Python, but have several years experience
with Perl in a web environment.

A question I have, if someone here is familiar with Perl, does Python
have something like Perl's 'here document'? I've just searched and read
some postings on generating HTML but they all seem to refer to various
template utilities. Is this the only way, or am I missing something? I'm
used to generating X/HTML by hand, which makes the here document in Perl
ideal for me. Also, many times a client already existing HTML code that
I can use in a script.

--
Amer Neely
w:www.webmechanic.softouch.on.ca/
Perl | MySQL programming for all data entry forms.
"Others make web sites. We make web sites work!"

I am not sure if this is what you are looking for, but Python has a
special string with 3 quotes that I believe duplicates part of the
functionality of a here document:

myHmtlHeader = """
<head attribute = "abc">
<title>My Page</title>
</head>
"""

print myHtmlHeader


outputs:


<head attribute="abc">
<title>My Page</title>
</head>

Well, I have checked everything I can but I'm getting '500 Internal
Server Error'. The log files aren't helpful:
[Thu Sep 13 03:43:00 2007] [error] [client 24.235.184.39] Premature end
of script headers: /home/softouch/public_html/cgi-bin/scratch/hello.py

I can't even get it to run on my home PC running Apache + Win2K. Same
error.

My script:
#!/usr/bin/python
import cgitb; cgitb.enable(display=0, logdir=".")
import sys
sys.stderr = sys.stdout
print "Content-Type: text/html"
print

print """
<html>
<body>
<div align="center"><font style="font-family:verdana; size:18px">Hello
from Python</font></div>
<br>
Goodbye.
</body>
</html>
"""

I should have added that it runs from the command line OK.
.
.
.
Yes, it should work fine. Do the things you'd do if it were Perl source:
when you say "it runs from the command line OK", do you mean invocation of
/home/softouch/public_html/cgi-bin/scratch/hello.py gives sensible results?
Does your Web server recognize that .py is a CGI extension? What are the
permissions on /home/softouch/public_html/cgi-bin/scratch/hello.py?

Might your server have an issue with "Content-Type" vs. "Content-type"?
 
A

Amer Neely

Richie said:
[Amer]
Can you or anyone see anything in the posted code that might be the
cause?
#!/usr/bin/python
import cgitb; cgitb.enable(display=0, logdir=".")
import sys
sys.stderr = sys.stdout
print "Content-Type: text/html"
print

My guess would be that you don't have cgitb in your server environment, or
that you have a bogus one. Rearrange things like this:

#!/usr/bin/python
print "Content-Type: text/html"
print
import sys
sys.stderr = sys.stdout
import cgitb; cgitb.enable(display=0, logdir=".")

Nope. Same error. Is cgitb not in the standard distribution? Is it
needed to print HTML?

On my home PC I changed the Apache error logging to 'debug' and got this
nugget:
[Thu Sep 13 04:16:03 2007] [error] [client 0.0.0.0] (OS 2)The system
cannot find the file specified. : couldn't create child process:
720002: hello.py

I suspect it would be similar on my hosting server. Is this saying it
can't find hello.py or one of the modules?
 
R

Richie Hindle

[Amer]
#!/usr/bin/python
[...] On my home PC [...]
[Thu Sep 13 04:16:03 2007] [error] [client 0.0.0.0] (OS 2)The system
cannot find the file specified.

That's because on your home PC Python is somewhere like
C:\Python25\python.exe, not /usr/bin/python.

Are you sure /usr/bin/python is correct for your hosting environment?
 
A

Amer Neely

Richie said:
[Amer]
#!/usr/bin/python
[...] On my home PC [...]
[Thu Sep 13 04:16:03 2007] [error] [client 0.0.0.0] (OS 2)The system
cannot find the file specified.

That's because on your home PC Python is somewhere like
C:\Python25\python.exe, not /usr/bin/python.

Are you sure /usr/bin/python is correct for your hosting environment?

It's my understanding that the Windows shell doesn't pay much attention
to the shebang line if the file type is associated with the proper
application.

But I tried your suggestion and got the same results.
 
A

Amer Neely

Amer said:
Richie said:
[Amer]
#!/usr/bin/python
[...] On my home PC [...]
[Thu Sep 13 04:16:03 2007] [error] [client 0.0.0.0] (OS 2)The system
cannot find the file specified.

That's because on your home PC Python is somewhere like
C:\Python25\python.exe, not /usr/bin/python.

Are you sure /usr/bin/python is correct for your hosting environment?

It's my understanding that the Windows shell doesn't pay much attention
to the shebang line if the file type is associated with the proper
application.

But I tried your suggestion and got the same results.

My BAD! I changed the shebang all right, but entered a typo. With the
correct path it did work just fine.

So, perhaps the path to python on my host is wrong? I got that one right
from the techs.
 
T

Tim Golden

Amer said:
Richie said:
[Amer]
#!/usr/bin/python
[...] On my home PC [...]
[Thu Sep 13 04:16:03 2007] [error] [client 0.0.0.0] (OS 2)The system
cannot find the file specified.
That's because on your home PC Python is somewhere like
C:\Python25\python.exe, not /usr/bin/python.

Are you sure /usr/bin/python is correct for your hosting environment?

It's my understanding that the Windows shell doesn't pay much attention
to the shebang line if the file type is associated with the proper
application.

But I tried your suggestion and got the same results.

The Windows shell doesn't, but Apache does. (Haven't followed
this thread closely, but I seem to remember it has to do with
running CGI scripts).

TJG
 
A

Amer Neely

Tim said:
Amer said:
Richie said:
[Amer]
#!/usr/bin/python
[...] On my home PC [...]
[Thu Sep 13 04:16:03 2007] [error] [client 0.0.0.0] (OS 2)The system
cannot find the file specified.
That's because on your home PC Python is somewhere like
C:\Python25\python.exe, not /usr/bin/python.

Are you sure /usr/bin/python is correct for your hosting environment?

It's my understanding that the Windows shell doesn't pay much
attention to the shebang line if the file type is associated with the
proper application.

But I tried your suggestion and got the same results.

See my reply to this post - I had a typo in the shebang line and it now
works on my home PC.
The Windows shell doesn't, but Apache does. (Haven't followed
this thread closely, but I seem to remember it has to do with
running CGI scripts).

This seems to indicate that maybe my host needs to configure Apache to
run python scripts? But I didn't need to do anything with mine.
 
P

Paddy

I'm a complete newbie with Python, but have several years experience
with Perl in a web environment.

A question I have, if someone here is familiar with Perl, does Python
have something like Perl's 'here document'? I've just searched and read
some postings on generating HTML but they all seem to refer to various
template utilities. Is this the only way, or am I missing something? I'm
used to generating X/HTML by hand, which makes the here document in Perl
ideal for me. Also, many times a client already existing HTML code that
I can use in a script.

--
Amer Neely
w:www.webmechanic.softouch.on.ca/
Perl | MySQL programming for all data entry forms.
"Others make web sites. We make web sites work!"

Hi Amer,
Just as an aside, you might find this helpful:
http://wiki.python.org/moin/PerlPhrasebook
It has perl code with python equivalents/notes.

- Paddy.
 

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
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top