[Tutor] localhosting

Cameron Simpson cs at cskk.id.au
Thu Sep 6 18:06:46 EDT 2018


Note: replies inline below, and irrelevant verbiage trimmed. We prefer this 
format on this list: it makes it easy to respond point by point and makes the 
reply read like a conversation. Please consider adopting this style here.

Anyway, to your message:

On 06Sep2018 12:09, Roger Lea Scherer <rls4jc at gmail.com> wrote:
>Thank you all for your help. I am still chugging away at this problem. I've
>switched to Windows PowerShell Version 5.1 since I can select, copy, and
>paste more easily in PowerShell. (I can't figure out how to do that in
>Anaconda and I get the same errors in PowerShell.)
>
>I removed the "3" in "python3" and it works; along with moving the "www"
>folder to where python resides. Thanks for the help.

Now switch back to python3 and see if it still works, even temporarily. You 
want to isolate what was the fix and what was irrelevant.

>So it appears the local host is running correctly, but when I run this code:
>
>print("Hello World")
>
>Chrome does not render and I get an error message in PowerShell:
>127.0.0.1 - - [06/Sep/2018 11:22:46] "GET /cgi-bin/hello.py HTTP/1.1" 200 -
>127.0.0.1 - - [06/Sep/2018 11:22:46] command:
>C:\Users\Roger\AppData\Local\Programs\Python\Python37-32\python.exe -u
>C:\Users\Roger\documents\roger\python\www\cgi-bin\hello.py ""
>127.0.0.1 - - [06/Sep/2018 11:22:46] b'  File
>"C:\\Users\\Roger\\documents\\roger\\python\\www\\cgi-bin\\hello.py", line
>1\r\n    print "<html>"\r\n                 ^\r\nSyntaxError: Missing
>parentheses in call to \'print\'. Did you mean print("<html>")?\r\n'
>127.0.0.1 - - [06/Sep/2018 11:22:46] CGI script exit status 0x1

I'm guessing you "Hello World" print was after the start of the <HTML> tag.  
Look at the rrror message you've recited above: it is talking about line 1 of 
your hello.py file, which has this:

  print "<html>"

That is an old-style print _statement_, which will not work in Python 3. So 
your other print isn't the source of this error message: likely it is never 
reached.

Edit your python files to start with this import:

  from __future__ import print_function

That will cause python 2 to expect print to be a function (needing brackets) 
and not a statement, meaning the same syntax will be required for both python 2 
and 3, providing you with consistent behaviour.

Then change the print statement into a print function call:

  print("<html>")

>BUT when I run this code:
>def fib(n):
>    if n < 2:
>        return n
>    return fib(n-1) + fib(n-2)
>
>print(fib(15))

Note: python 3 print function.

>Chrome does not render but I DON'T get an error message, I get this:
>127.0.0.1 - - [06/Sep/2018 11:33:30] "GET /cgi-bin/recursive%20fibonacci.py
>HTTP/1.1" 200 -
>127.0.0.1 - - [06/Sep/2018 11:33:30] command:
>C:\Users\Roger\AppData\Local\Programs\Python\Python37-32\python.exe -u
>"C:\Users\Roger\documents\roger\python\www\cgi-bin\recursive fibonacci.py"
>""
>127.0.0.1 - - [06/Sep/2018 11:33:30] CGI script exited OK

There should be more than a bare number in the output. CGI scripts require one 
to provide HTTP response headers before the body text: that way they can return 
other stuff than HTML (eg an image or CSV data etc). Try putting these print 
calls at the start of that script:

  print('Content-Type: text/html')
  print()

which mark the rest of the script output as HTML and then provide a blank line 
to indicate the end of the headers. The objective is that your script produces 
this output:

  Content-Type: text/html

  325

That's a made up number, I haven't bothered to compute fib(15), sorry.

>So I'm confused about 2 things:
>1. Why does the one file cause an error in PowerShell (and Anaconda
>actually) when it seems to be the same as the other file which appears to
>run with no error, but just not render? and,

Because you're running the file with Python 2 by hand and Python 3 from the web 
server (see the "Python\Python37-32\python.exe" in the log?) Apply the 
suggested __future__ import and you'll get consistent behaviour.

>2. What am I missing: why won't it render since the instructions I have at
>the very beginning of this email say, basically, this is all I have to do
>to get it to render?
>
>I know this second question is maybe a little bit outside this forum, but I
>am struggling to make sense out of what little I know and, truly, this
>forum seems the nicest and most helpful I've encountered.

The output of a CGI script should be a valid HTTP response: you need some HTTP 
headers describing the output format and _then_ the core programme output. The 
minimal header is a Content-Type: header to denote the program result format.  
See above for how to stick one at the top of any of your CGI scripts.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list