[nSLUG] Python and HTML

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

On Fri, 2015-01-09 at 15:49 +0000, Hatem Nassrat wrote:
> Hi Frank,
> On Fri Jan 09 2015 at 11:36:06 AM Frank Geitzler
> <frank.geitzler at ns.sympatico.ca> wrote:
>             port = 8080
>             httpd = HTTPServer(('', port), CGIHTTPRequeseteHandler)
>             print("Starting simple_httpd on port: " +
>         str(httpd.server_port)
>             httpd.serve_forever()
> These lines actually start an HTTP Server (like apache, nginx in the
> sense that it talks HTTP) that only servers ONE thing, the
> CGIHTTPRequestHandler that you created. Basically saying, for this
> example in your book, you don't need any server ontop of the script.
> It will start a server for your script on port 8080, so if you go
> to http://localhost:8080 in your browser you should see the page you
> built.

Thanks, Hatem,  The Python code does start the localhost on port 8080,
and I do see the first page (index.html).  If before I start the local
server I personalize index.html by substituting my own name in place of
the coach's name.  When I key http://localhost:8080 into the FireFox
browser, before I press <Enter> the text "Welcome to Coach Kelly's
Website" (or "Welcome to Frank's Website"", in the personalized version)
and the line below it reads "localhost:8080".  When I  press <Enter> the
appropriate web page appears, either Coach Kelly's or Franks, but  when
I hover the mouse pointer over the highlighted text for the next step,
(captioned 'timing data' in the line "For now, all that you'll find here
is my athlete's timing data.  Enjoy!"), the line which pops up at the
bottom of the screen shows 'localhost:8080/cgi-bin/generate_list.py',
but unfortunately, when I click on the the highlighted text for the next
step, (captioned 'timing data'...), a pop-up message box captioned
'Opening generate_list.py' and stating 'You have chosen to open
generate_list.py which is a: Python script from localhost:8080".  What
should Firefox do with this file?
The default option shown in a drop-down list is Open with
gedit(default), and the other option in the drop-down (Other) produces a
list of applications (which does not include 'Python3').  The other
pushbutton choice is 'Save File' , and a checkbox is captioned  'Do this
automatically for files like this from now on".  When I Cancel this
window, and examine the terminal window, it shows the following:

frank at frank-laptop:~$ python3 simple_httpd.py
Starting simple_httpd on port: 8080
localhost - - [09/Jan/2015 13:12:00] "GET / HTTP/1.1" 200 -
localhost - - [09/Jan/2015 13:12:00] "GET /coach.css HTTP/1.1" 200 -
localhost - - [09/Jan/2015 13:12:00] "GET /images/coach-head.jpg
HTTP/1.1" 200 -
localhost - - [09/Jan/2015 13:13:23] "GET /cgi-bin/generate_list.py
HTTP/1.1" 200 -
Traceback (most recent call last):
  File "/usr/lib/python3.1/http/server.py", line 1058, in run_cgi
    os.execve(scriptfile, args, os.environ)
OSError: [Errno 2] No such file or directory
localhost - - [09/Jan/2015 13:13:23] CGI script exit status 0x7f00

I suspect that I should find Python3 (or Python3.2) in my root
directory, and specify that in response to the pop-up window, and
probably check the box that will indicate 'Do this automatically for
files like this from now on".   but I am not sure, and have not yet done

> To have many scripts in a cgi-bin directory, you will actually have to
> delete the lines mentioned above, and instead do something like
> this: https://wiki.python.org/moin/CgiScripts
> However, this would be ill-advised, since writing cgi scripts is a way
> of the past ... way way back. I would advise you to pick up a web
> framework ... there are simple ones (like bottle) or bigger ones
> (like django), of course there is a million and one web frameworks out
> there, I just mentioned two of the ones I like to use.
> My advice is once you get a hang of the cgi server as mentioned in
> your book you should look at bottle tutorial and you will hopefully
> see that it is an easy migration of you code.
> Thanks,
> -- Hatem
> _______________________________________________
> 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/5ce2836f/attachment.htm>

More information about the nSLUG mailing list