'"Can only POST to CGI Scripts" 501 error
I'm running a local CGIHttpServer in Python, and am using this python program to run something in that server:
''' submit data to form using robots '''
import urllib
import pprint
# hacking gullible app
url = "http://localhost:8000/cgi-bin/w5/captcha.example/vote_app/gullible_app.py"
def vote(lecturer):
params = urllib.urlencode({'lecturer': lecturer,'submit':'submit'})
f = urllib.urlopen(url, params)
pprint.pprint(f.fp.readlines())
vote("Ivo")
This tells me it can only POST to CGI scripts, which I find really strange because the python script opens in my web browser at that address just fine. So... it's running in my browser fine, but not when the python program attempts to POST to that URL. What is going on here? (There is VERY little out there on the internet about this - I've tried researching this problem to solve it myself but there's only 3-4 people mentioning this issue)
EDIT: Sorry guys! I did not understand GET and POST. I should have included this in the question - it is the python program "gullible_app.py". As you can see, the form does the "POST" operation
import cgi
import cgitb; cgitb.enable()
# form generation
# -------------------------------------------------------
def print_form():
print "Content-Type: text/html\n"
print '''
<html>
<body>
<form method="post" action="gullible_app.py">
<p>Select your favorite lecturer:</p>
<input type="radio" name="lecturer" value="harald" /> Harald
<input type="radio" name="lecturer" value="ivo" /> Ivo
<input type="submit" name="submit" />
</form>
</body>
</html>
'''
# response generation
# -------------------------------------------------------
def print_response():
print 'Content-Type: text/html\n'
print '<html><body>Thank you for your vote!</body></html>'
def main():
user_data = cgi.FieldStorage()
if "submit" in user_data: # user press "submit"
lecturer = user_data.getfirst("lecturer")
f = open( "cgi-bin\\w5\\captcha.example\\vote_app\\votes.txt", "a" )
f.write( lecturer+'\n' )
f.close()
print_response()
else: # display the form
print_form()
main()
Solution 1:[1]
The server side program (which you haven't given so we can't say for sure) only accepts GET requests and not POST requests to the url shown.
urllib makes urlopen act as a POST the way you've done it. See the docs http://docs.python.org/2/library/urllib.html#examples for an example of how to make a request that is a GET
Solution 2:[2]
The answer appears to be in the documentation.
Your error message: Error 501, “Can only POST to CGI scripts”, is output when trying to POST to a non-CGI url.
The big hint: run the CGI script, instead of serving it as a file, if it guesses it to be a CGI script
What you need to modify: cgi_directories
So, either put your (Python) CGI script in one of the default sub-directories, or modify the cgi_directories
to make it "guess" correctly that the URL is supposed to be a CGI script.
Solution 3:[3]
Sometimes there is permission issue. You need to check the permission for cgi script and make it executable.
chmod +x <cgi script>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vorsprung |
Solution 2 | Wesley Baugh |
Solution 3 | satyendrakarn |