Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Autogeneration of web pages Posted by pinks on 1 Apr 2003 at 6:55 PM
Hello python fans,

I am a bit new to python and already liking this great language. I was trying to autogenerate html pages from cgi scripts. Basically I need to write a Python program to take a file marked up using the following system:

'* filename title' - creates a file using the given filename (with '.html' appended) and gives it the given title, both at the top of the browser (in the <title> section) and as a top level heading (<h1>) on the page.
'**' represents a section heading (<h2>)
'.' represents a paragraph
'-' represents an element in an unordered list (<li> in a <ul>)
'--' represents an element in an unordered list which is in another list
Markups must begin on the first character of a line
Markups may span multiple lines

and convert it into valid XHTML 1.0.

PLEASE EMAIL ME IF YOU HAVE ANY QUERIES. I ALREADY HAVE SOME FUNCTIONS DEFINED LIKE get_file and put_file which will be needed for this program.

Thanks,
pinks.


Report
Re: Autogeneration of web pages Posted by infidel on 2 Apr 2003 at 7:16 AM
This message was edited by Moderator at 2003-4-2 7:17:20

: Hello python fans,
:
: I am a bit new to python and already liking this great language. I was trying to autogenerate html pages from cgi scripts. Basically I need to write a Python program to take a file marked up using the following system:
:
: '* filename title' - creates a file using the given filename (with '.html' appended) and gives it the given title, both at the top of the browser (in the <title> section) and as a top level heading (<h1>) on the page.
: '**' represents a section heading (<h2>)
: '.' represents a paragraph
: '-' represents an element in an unordered list (<li> in a <ul>)
: '--' represents an element in an unordered list which is in another list
: Markups must begin on the first character of a line
: Markups may span multiple lines
:
: and convert it into valid XHTML 1.0.
:
: PLEASE EMAIL ME IF YOU HAVE ANY QUERIES. I ALREADY HAVE SOME FUNCTIONS DEFINED LIKE get_file and put_file which will be needed for this program.

did you have any specific questions?

infidel



Report
Re: Autogeneration of web pages Posted by pinks on 2 Apr 2003 at 5:35 PM
I wanted to know how i start the python script; what are the variables needed to be defined.
Report
Re: Autogeneration of web pages Posted by infidel on 3 Apr 2003 at 7:23 AM
: I wanted to know how i start the python script; what are the variables needed to be defined.

Oh, well the simplest way to write Python cgi scripts is like this:

The first line should be the "shebang":

On a *NIX system:
#!/usr/bin/python


On a Windows system:
#!C:\Python22\python.exe


Of course, the path may be different on your web server.

Then you need to print the header that tells your browser to expect html:

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


Make sure you have the newline at the end!

After that, anything you print will be parsed by the browser as html. For example:

#!/usr/bin/python
print 'Content-type: text/html\n'
print """
<html>
    <head>
        <title>My first cgi script!</title>
    <head>
    <body>
        <h1>Hello, world!</h1>
    </body>
</html>
"""


Then you just drop that script in the cgi-bin on your server and call it with a regular URL in a browser.

Debugging cgi is rather frustrating, but there is a way in python to make it much easier to find code errors. Just after the shebang line (which must be the first line of the file) put this:

import cgitb; cgitb.enable()


This imports the cgi traceback module. If any exceptions are raised by your script, you will get a pretty html display of the code and a description of the error and the line that caused it rather than the unhelpful "500: Internal Server Error".


infidel




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.