[nSLUG] Python and HTML

Frank Geitzler frank.geitzler at ns.sympatico.ca
Fri Jan 9 23:13:06 AST 2015


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/20150109/e94c782e/attachment-0001.html>


More information about the nSLUG mailing list