Cannot open html templates within python scripts

Where Python makes a move against Perl. All about web programming right here.

Moderators: KDoiron, mawe

Cannot open html templates within python scripts

Postby nikos on Mon Mar 08, 2010 1:17 am

Hello,

I recently reguister anew account on freehostia.net which deos support python bu the problem is that it wont allo openign of html pages within python scripts.

for example

Code:
with open("test.html") as f:
data = f.read()


cannor run and produces internal server error.
I asked them to allo me to open at least html templates because iams eperating design from code but they say they cant.

So they told me to ask more experienced user or friends how to open an html fiel within my python script.

Any workarounds?
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby HbKai on Mon Mar 08, 2010 6:23 am

urllib2 (should) do this. Since it's similar to your code, are you importing urllib2?

Give this code a shot on its own and see if it can reply with python.org's page.
Code: Select all
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
HbKai
Python Fan
Python Fan
 
Posts: 5
Joined: Mon Feb 08, 2010 7:22 pm

Re: Cannot open html templates within python scripts

Postby nikos on Mon Mar 08, 2010 9:29 am

I just tried it and unfortunately i'm still getting the same "Internal Server Error".
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby avisser on Mon Mar 08, 2010 9:40 am

how are you executing this code? If it's from within a cgi script, make sure you get the path to the html file right and have the proper permissions. For Internal Server Errors, it's always a good idea to find out what the server logs have to say.
counselor: Fine, fine. But do you, do you have any qualifications?
Anchovy: Yes, I've got a hat.
counselor: A hat?
Anchovy: 'Yes, a hat. A lion taming hat. A hat with 'lion tamer' on it. I got it at Harrods. And it lights up saying 'lion tamer' in great big neon letters, so that you can tame them after dark when they're less stroppy.
avisser
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 2541
Joined: Sat Oct 28, 2006 3:03 pm
Location: Holland

Re: Cannot open html templates within python scripts

Postby nikos on Mon Mar 08, 2010 11:26 am

i execute this code like this: http://tech-nikos.gr/test.cgi

i had the permission set correctly to 755 so it could be executed properly.

Server logs say that "File does not exist"

the exact code iam trying to use is this:

Code: Select all
with open('test.html') as f:


because both test.html and test.cgi are located in the same folder:

Code: Select all
with open('./test.html') as f:


None of this were successfull to open the file and as you correclly pointed me now that we see that the erro has been produced because the server log cant for soem reason find the file:

[Mon Mar 08 19:20:04 2010] [error] [client 78.87.224.40] File does not exist: /home/nikos/public_html/404.shtml
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby nikos on Tue Mar 09, 2010 10:02 am

Any ideas left?!
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby waz on Tue Mar 09, 2010 11:36 am

nikos wrote:Any ideas left?!

Well if trying to access a file generates a 404 then something is definitely weird... It might be a configuration issue in mod_python or .htaccess, or a bad directory...


Quick and dirty approach would be to put the HTML code as a multi-line string in the Python file. If it's only for a few small pages this should be fine, but if it's for a larger project you could separate the templates into their own Python files and then import them!

Example of first way:

Code: Select all
template = """\
<html>
...
</html>"""

# rest of script that uses/prints template

Example of second way:

file "template1.py":
Code: Select all
template = """\
<html>
...
</html>"""

main script:
Code: Select all
import template1

# rest of script that uses/prints template1.template


By the way you might want to ask this question over on your host's forums.
҉ 囧 ҉
waz
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1348
Joined: Tue May 05, 2009 7:02 pm

Re: Cannot open html templates within python scripts

Postby nikos on Tue Mar 09, 2010 12:18 pm

Hello and thanks for the answer,

I have already asked webhosting people and they say admisn have se things up this way so for ppl not to open files within php/perl/python/cgi scripts due to possible security reason and thay asked me to ask a more experience user/frined and how can i go from there since i exaplined to them its only about openign an html template within a simple python script.

Now concering my question regardign your answer:

Both ways you described seem okey but i can't understand how would i fetch and handle the html templates data from python scrip's point.

for example:

Code: Select all
#!/usr/bin/python

import cgi
import template

print "Content-type: text/html\n"

template = """\

<html>

<head>
   <title> %(title)s </title>
</head>

<body>
   <h2> %(message)s </h2>
   <h1> %(counter)i </h1>
</body>

</html>

"""


counter = 0

#Now if i could open the template in my host i would sue the following lines

#with open('test.html') as f:
#data = f.read

# Now that i cant how would i read the raw multi-line html above? by using
#data = template ?!?!
   
for x in range(11):
   counter = counter + x
      
data = data % { 'title': "Nikos!", 'message': "Χαιρετισμούς!", 'counter': counter }
print data
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby waz on Tue Mar 09, 2010 12:25 pm

You only need to import a file if you don't put it in your script; since you already have a template string in the script you can skip importing anything special in that regard.

Code: Select all
import cgi

print "Content-type: text/html\n"

template = """\
<html>
<head>
   <title> %(title)s </title>
</head>
<body>
   <h2> %(message)s </h2>
   <h1> %(counter)i </h1>
</body>
</html>"""

counter = 0
for x in range(11):
   counter = counter + x

print template % {'title': "Nikos!", 'message': "Χαιρετισμούς!", 'counter': counter}

I'm not sure what purpose counter serves here, but as you can see you avoid trying to open() a "template file" by defining the string directly in the source code.
҉ 囧 ҉
waz
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1348
Joined: Tue May 05, 2009 7:02 pm

Re: Cannot open html templates within python scripts

Postby nikos on Tue Mar 09, 2010 12:56 pm

Yes i tried method1 before i saw you answer and it worked! I shouldn't had use the 'data = template' statement since i already had the html source inside the variable 'template' there was no reason to re-assign the same html data into another variable 'data'. It was plain foolisj :-)
Also, i should have tried to execute it before i asked you(sorry for that i guess i was feeling kind of insecure)if was this to work like that.

Python is so cool! IO can't believe it handles html so easily as by:

a) openign it on a separate file (which due to admins security settigns wont worj for me)
b) putting WHOLE html data inside a variable!
c) save pure html data souce into a .py file and import it! (what a cool way to avoid opening, nast hack, i like it ;-)

Now iam having a little difficulty trying method 2 of yours.

template.py

Code: Select all
data = """


<html>
<head>
   <title> %(title)s </title>
</head>

<body>
   <h2> %(message)s </h2>
   <h1> %(counter)i </h1>
</body>
</html>


"""


test.cgi

Code: Select all
#!/usr/bin/python

import cgi
import template


counter = 0
   
for x in range(100):
   counter = counter + x
      
data = data % { 'title': "Nikos!", 'message': "Hello, world!!", 'counter': counter }
print (data)


result => http://tech-nikos.gr/test.cgi

What makes it produce an error?!
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby waz on Tue Mar 09, 2010 1:10 pm

Well, in your last snippet, what is data? It looks like you're trying to say:

Code: Select all
data = template.data % { 'title': "Nikos!", 'message': "Hello, world!!", 'counter': counter }
print (data)

See the difference? You can think of import as a container. To access the variable from the module, you have to say "from this module get me this variable" hence the syntax template.data.

Same thing had you wanted to, say, use the factorial() method of the math module:

Code: Select all
import math

print math.factorial(3)



By the way, an easy way to debug CGI scripts is to add these two lines:

Code: Select all
import cgitb
cgitb.enable()

that way when an error occurs it will print the stack trace on the page as if you had run it from the command line.
҉ 囧 ҉
waz
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1348
Joined: Tue May 05, 2009 7:02 pm

Re: Cannot open html templates within python scripts

Postby nikos on Tue Mar 09, 2010 2:21 pm

Thank you very much for the info but this 2nd method although is correct is not properly printed in both remote hosts opposes to method 1 which runs as expected

my code looks like this:

Code: Select all
#!/usr/bin/python

import cgitb
cgitb.enable()
import cgi
import template


counter = 0

for x in range(20):
   counter = counter + x
      
data = template.data % { 'title': "Nikos!", 'message': "Hello, world!!", 'counter': counter }

print (data)


See w

Remote Host No1 => http://tech-nikos.gr/test.cgi

Here it downloads test.cgi instead of executing it

Remote Host No2 => http://rqjbj.freehostia.com/test.cgi

Here it prints the souce of html filled with correct values opposed of running the generated html data

With method 1 the code was printed as expected in both remote hosts, why this behaviour here?

Is it perhaps the module has '.py' extension?!
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby nikos on Tue Mar 09, 2010 11:44 pm

Good day to you all folks!

Any idea waz why method 2 behaves likes these in my 2 remote web hosts when i try to execute them?
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby waz on Tue Mar 09, 2010 11:56 pm

Nope, sorry. Maybe it's a host thing, I've never had either problem...
҉ 囧 ҉
waz
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1348
Joined: Tue May 05, 2009 7:02 pm

Re: Cannot open html templates within python scripts

Postby nikos on Wed Mar 10, 2010 12:45 am

If you up0load and execute the exact same script to aremote webhosting of yours would it run properly?

Please can you try such a thing for me so to see if on another hosting it works ok?

If its not too much of a trouble....
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby nikos on Wed Mar 10, 2010 1:25 am

Iam suprised same exact script running locally in my win7 box produces same output as the 2nd remote host

http://rqjbj.freehostia.com/test.cgi

which is showing the correctly generated html data as text file contents though opposed of executing it.

Code: Select all
<html>
<head>
   <title> Nikos! </title>
</head>

<body>
   <h2> Hello, world!! </h2>
   <h1> 190 </h1>
</body>
</html>


Why? Method 1 was working flawlessly in both remoet servers and locally, why method 2 fails executing both propelry and remotely?
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby nikos on Wed Mar 10, 2010 1:43 am

No need to try!

i was missing a line:

print "Content-type: text/html\n"

so i guess the remote web server handled the html data as simple text document, one allow me to downloadign it instantly when requested and the other just show its contents :)

Locally though the script refuses to run at all, it says internal server error for both method 1 and 2. Any ideas on that?

is there a pythonic expression for the above line instead of this print?

and also here
Code: Select all
import cgitb
cgitb.enable()
import cgi
import template


do i have correctly placed the first two lines in my python scripts immediately after the shebang or i have to place the enable() some where else?

Whnat exactly did you say these lines do?
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby waz on Wed Mar 10, 2010 10:35 am

nikos wrote:Whnat exactly did you say these lines do?


waz wrote:By the way, an easy way to debug CGI scripts is to add these two lines:

Code: Select all
import cgitb
cgitb.enable()

that way when an error occurs it will print the stack trace on the page as if you had run it from the command line.

;) http://docs.python.org/library/cgitb.html


As far as "pythonic expressions," no, it's fine...

However, if your script did any redirection then you'd want to save that "Content-type ..." string until after the redirection logic. That is, when you print a content type string, you're telling the browser to get ready to display data, but if you had printed this instead:

Code: Select all
print "Location: google.com"

then instead of seeing any output on your site your browser would have redirected to Google's page. Obviously, trying to relocate this way after printing a content type string will nullify the redirection and make it appear as text in the browser.
҉ 囧 ҉
waz
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1348
Joined: Tue May 05, 2009 7:02 pm

Re: Cannot open html templates within python scripts

Postby nikos on Wed Mar 10, 2010 10:47 am

Yes i saw this answer when you first posted it but didn't understood it and still can't.

"that way when an error occurs it will print the stack trace on the page as if you had run it from the command line."

Stack trace?! What does it mean? It's all greek to me, although i'm greek ;)

a) Is this like Perl's use CGI::Carp qw(fatalsToBrowser);

b) Also can't the "print "Content-type: text/html\n"" be replaced by print header() like i did with perl?

c) Why the script won't run at all in localhost by estimate?
nikos
Python User
Python User
 
Posts: 86
Joined: Mon Mar 01, 2010 1:05 am

Re: Cannot open html templates within python scripts

Postby waz on Wed Mar 10, 2010 11:03 am

The stack trace is the error message(s) you see when the program crashes. For example:

Code: Select all
>>> int('3.14')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.14'


a) b) I don't know, I'm not familiar with Perl.

c) Probably an Apache configuration issue.
҉ 囧 ҉
waz
Ultimate Python Hacker
Ultimate Python Hacker
 
Posts: 1348
Joined: Tue May 05, 2009 7:02 pm

Next

Return to Web Programming

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Dreamlink Web hosting and Traduzioni Rumeno Italiano and ASSP Deluxe for cPanel.