[nSLUG] Python and HTML

Frank Geitzler frank.geitzler at ns.sympatico.ca
Sat Jan 10 13:04:24 AST 2015


On Fri, 2015-01-09 at 23:59 -0400, Byron Winmill wrote:
> I tried it on my system, and the good news is that it worked.  I
> suspect the problem on your end has to do with the shebang directive.
> Try using "#!/usr/bin/python3" instead.  If that doesn't work, use
> "which python3" to locate your Python interpreter.
> 
> 

Thanks very much, Byron -and to several others who provided helpful
suggestions as well.  The final solution was to insert the directive
"#! /usr/bin/python3" as the first line of each python script, and to
issue the command "chmod +x " for each python script.
I am just new to python, alghough I have been working my way through
several books since June of last year (in my very limited spare time, of
course).
Incidentally, something useful mentioned in this book which helped to
find an error in the cgi script was to add two lines near the beginning
of each script (for debugging only, of course -to be removed before
production):
    import cgitb
    cgitb.enable()    # to enable cgi tracking 

> 
> (Incidentally: the server provides a 404 if the python file isn't
> found, and a 403 if the permissions are set incorrectly.)
> 
> 
> On Fri, Jan 9, 2015 at 11:13 PM, Frank Geitzler
> <frank.geitzler at ns.sympatico.ca> wrote:
> 
>         On Fri, 2015-01-09 at 18:46 -0400, Byron Winmill wrote: 
>         
>         > Since CGIHTTPRequestHandler is configured to use /cgi-bin by
>         > default, that shouldn't be the problem.
>         > 
>         > 
>         > Does your CGI script add HTTP headers?  This should look
>         > like:
>         > 
>         > 
>         > 
>         >         print("Content-Type: text/html") 
>         >         print() 
>         > 
>         > 
>         
>         Byron, the code for the CGI script 'generate_list.py' follows:
>         
>         #! /usr/local/bin/python3
>         import glob
>         
>         import athletemodel
>         import yate
>         
>         data_files = glob.glob("data/*.txt")
>         athletes = athletemodel.put_to_store(data_files)
>         
>         print(yate.start_response())
>         print(yate.include_header("Coach Kelly's List of Athletes"))
>         print(yate.start_form("generate_timing_data.py"))
>         print(yate.para("Select an athlete from the list to work
>         with:"))
>         for each_athlete in athletes:
>             print(yate.radio_button("which_athlete",
>         athletes[each_athlete].name))
>         print(yate.end_form("Select"))
>         print(yate.include_footer({"Home": "/index.html"}))
>         
>         yate.py is the template file provided by the book, as are all
>         the other files.  The problem appears to be that the
>         application is unable to find the file
>         'generate_timing_data.py', even though it is in the cgi-bin
>         directory which I put in my home folder, together with the
>         data, images, and templates folders.
>         The 'header.html' template follows:
>         <html>
>         <head>
>         <title>$title</title>
>         <link type="text/css" rel="stylesheet" href="/coach.css" />
>         </head>
>         <body>
>         <h1>$title</h1>
>         and the footer.html template is:
>         <p>
>         $links
>         </p>
>         </body>
>         </html>
>         Frank
>         
>         > The empty print statement is important since you need a
>         > blank line between the HTTP header and HTML document.
>         > 
>         > On Thu, Jan 8, 2015 at 11:35 PM, Frank Geitzler
>         > <frank.geitzler at ns.sympatico.ca> wrote:
>         > 
>         >         On Thu, 2015-01-08 at 17:50 -0400, Byron Winmill
>         >         wrote: 
>         >         
>         >         > Is the server configured to allow CGI programs in
>         >         > general, as well as to allow CGI programs in that
>         >         > particular directory?
>         >         
>         >         Byron, as far as I can tell, there should be no
>         >         restrictions.  
>         >         I used IDLE to determine my Python search path:
>         >         
>         >         >>> import sys
>         >         >>> sys.path
>         >         [‘/home/frank’, ‘/usr.bin’, ‘/usr/lib/python3.1’,
>         >         ‘/usr/lib/python3.1/plat-linux2’,
>         >         ‘/usr/lib/python3.1/lib-dynload’,
>         >         ‘/usr/lib/python3.1/dist-packages’,
>         >         ‘/usr/local/lib/python3.1/dist-packages’]
>         >         I stored the code for the server, simple_httpd.py in
>         >         the directory /home/frank, together with the other
>         >         sub-directories and files listed below:
>         >         
>         >         
>         >         The code for the server is:
>         >         
>         >         from http.server import HTTPServer,
>         >         CGIHTTPRequestHandler 
>         >         
>         >         import cgitb
>         >         cgitb.enable()
>         >         port = 8080
>         >         
>         >         httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
>         >         print("Starting simple_httpd on port: " + str(httpd.server_port))
>         >         httpd.serve_forever()
>         >         
>         >         The server is started in a terminal window by changing to the sub-directory "/home/frank" in which it resides, and issuing the following command (where the name of the file is simple_httpd.py):
>         >         	python3 simple_htppd.py
>         >         I then open FireFox and enter "http://localhost:8080/", at which point the form "index.html" is displayed.  That form is in the same directory as is the server code, as are 
>         >         the following files and sub-directories which are also required:
>         >         	folders; cgi-bin, data, images, templates
>         >         	files: coach.css,	favicon.ico
>         >         
>         >         The code for the entire application can be found as
>         >         part of the download for the examples of chapter 7
>         >         at:
>         >             http://www.headfirstlabs.com/books/hfpython/
>         >         if you care to dig that deeply into it, or I can
>         >         send individual files if desired -I just don't want
>         >         to fill up this thread too much.
>         >         Thanks for your suggestions.
>         >         Frank 
>         >         
>         >         
>         >         
>         >         > On Thu, Jan 8, 2015 at 9:52 AM, Frank Geitzler
>         >         > <frank.geitzler at ns.sympatico.ca> wrote: 
>         >         > 
>         >         >         Has anyone experience with Python generating HTML?  I have been
>         >         >         attempting to learn Python by working my way through a book called
>         >         >         HeadFirstPython, and Chapter 7 purports to create a HTML application
>         >         >         with a main form ("webapp/index.html") calling a subsidiary form
>         >         >         ("webapp/cgi-bin/generate_list.py"), which in turn will call additional
>         >         >         subsidiary forms or alternatively will return to the main form.  The
>         >         >         main form correctly appears when the simple local server is started and
>         >         >         the Firefox website is pointed to "http://localhost:8080/".  The
>         >         >         filename "cgi-bin/generate_list.py" is displayed when the mouse hovers
>         >         >         over the proper selection area, but when that area is selected, rather
>         >         >         than invoking generate_list.py, gedit is invoked and it attempts to
>         >         >         display the contemts of that file, but appears unable to locate it.  The
>         >         >         file "cgi-bin/generate_list.py" begins with #! /usr/local/bin/python3
>         >         >         and has been marked executable with the command "chmod +x
>         >         >         generate_list.py ".  
>         >         >         Any suggestions would be appreciated.
>         >         >         Frank
>         >         >         
>         >         >         _______________________________________________
>         >         >         nSLUG mailing list
>         >         >         nSLUG at nslug.ns.ca
>         >         >         http://nslug.ns.ca/mailman/listinfo/nslug
>         >         >         
>         >         >         
>         >         >         
>         >         > 
>         >         > 
>         >         > 
>         >         
>         >         
>         >         
>         >         
>         > 
>         > 
>         > 
>         
>         
>         
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nslug.ns.ca/pipermail/nslug/attachments/20150110/05268023/attachment.html>


More information about the nSLUG mailing list