Autogeneration of web pages - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Autogeneration of web pages

pinkspinks Posts: 2Member
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 section) and as a top level heading (

) on the page.
'**' represents a section heading ()
'.' represents a paragraph
'-' represents an element in an unordered list ( in a )
'--' 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.


Comments

  • infidelinfidel Posts: 2,900Member
    [b][red]This message was edited by Moderator at 2003-4-2 7:17:20[/red][/b][hr]
    : 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 section) and as a top level heading (

    ) on the page.
    : '**' represents a section heading ()
    : '.' represents a paragraph
    : '-' represents an element in an unordered list ( in a )
    : '--' 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?

    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]



  • pinkspinks Posts: 2Member
    I wanted to know how i start the python script; what are the variables needed to be defined.
  • infidelinfidel Posts: 2,900Member
    : 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:
    [code]#!/usr/bin/python[/code]

    On a Windows system:
    [code]#!C:Python22python.exe[/code]

    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:

    [code]print 'Content-type: text/html
    '[/code]

    Make sure you have the newline at the end!

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

    [code]
    #!/usr/bin/python
    print 'Content-type: text/html
    '
    print """


    My first cgi script!

    Hello, world!



    """
    [/code]

    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:

    [code]import cgitb; cgitb.enable()[/code]

    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".


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

Sign In or Register to comment.