0

This is the file I'm trying to access the data from, guessAnswer.py:

#!/usr/bin/python
print 'content-type: text/html'
print

import cgi
import cgitb; cgitb.enable()

userAnswer = cgi.FieldStorage()
userAnswer['userAnswer'].value

So basically, I printed HTML from file1.py, and it is read successfully on the server.

print '''<br> <form action='guessAnswer.py'>
              <input name="userAnswer">
          </form>
     </b> </center> </h2>'''

However, I can't seem to get the data inputted by the user at from guessAnswer.py, because file1.py is a python file.

How do I access the user data then? Or is this code fine and some other part of my program is causing the problem?

When I run the file in command prompt, at the end of the error, I get:

Traceback (most recent call last):
  File "guessAnswer.py", line 115, in &lt;module&gt;
    print '&lt;center&gt; &lt;h1&gt; Your guess is &lt;b&gt; ' + guessTestTakers(hw22.someHighSchool,     userInput['userAnswer'].value) + '&lt;/b&gt
t;/h1&gt; &lt;/center&gt;'
  File "C:\Python27\lib\cgi.py", line 540, in __getitem__
    raise KeyError, key
KeyError: 'userAnswer'
0

1 Answer 1

1

At least when you run it with the command prompt, you know that guessAnswer.py is running (because it gives an error in that file). Do you know whether it runs when you run it using the browser? Set execute (and read?) permission for all users on guessAnswer.py. Make sure its path can be found; maybe insert an explicit path or entire URL pointing to guessAnswer.py in 1.py, at least as a test.

Consider importing guessAnswer.py into 1.py. (I'm not sure whether this would work or not.) You would need to set the path (PYTHONPATH in UNIX) to make sure it can be found.

See cgi. Note the "Common problems and solutions" section near the bottom; for example: "Most HTTP servers buffer the output from CGI scripts until the script is completed. This means that it is not possible to display a progress report on the client’s display while the script is running."

Or this might be the problem: "Form fields containing empty strings are ignored and do not appear in the dictionary; to keep such values, provide a true value for the optional keep_blank_values keyword parameter when creating the FieldStorage instance." That is, you could change it to userAnswer = cgi.FieldStorage(keep_blank_values=True)

Try inserting a prompt like "Please enter your guess:" inside the form tags, like "First name:" in the example here: http://www.w3schools.com/tags/att_form_action.asp Do you see the prompt when you run it with the browser?

What actually happens when you run it with the browser? Do you get a chance to enter data? What do you actually see? What is the error message or other result that lets you know that your script doesn't see the data?

Please give more detail of what error message you see when it says "Internal Server Error".

[What I had said earlier was: Please clarify. Is the user running a browser? Is guessAnswer.py the program you're running? Does the browser cause this program to run, and if so how? Or are you running the program from the command line? Is the second set of code you've given the contents of 1.py? Give an example of what the user input is. What did you see? (An error message? A blank web page? A command that hangs?)]

Sign up to request clarification or add additional context in comments.

2 Comments

All valid questions, but this should really be a comment rather than an answer.
I'm running a browser on file1.py, which prints out the HTML to be interpreted. guessAnswer.py is the file I'm trying to get the data to. I'm running this entire thing on a private server. I see an error message that says: Internal Server Error, etc. That happens when the python file isn't running properly, as in, with syntax errors. So, I figured something is wrong, and this is the only thing I can pinpoint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.