I have coded a simple WSGI Server using Google app engine. Here's the code for it:
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('''
<html><head><title>BLAH</title></head>
<body>
<form type = "input" action = "/post" method = post>
<input type = text name = "name">
<input type = submit value = "Submit">
</form>
<hr>
</body>
</html>
''')
class EntrySubmission(webapp2.RequestHandler):
def post(self):
n = self.request.get('name')
self.response.write('''<html><head><title>%s</title></head>
<body>
<h1>Welcome %s</h1>
<br>How are you today?
<br><a href = "/">Back</a>
<hr>
</body>
</html>''' % (n,n))
app = webapp2.WSGIApplication([
('/', MainHandler),
('/post',EntrySubmission),
], debug=True)
The server works fine when I manually input the value. But when I input the value using python it gives a 405 response. What could be the problem? The code for input via python:
import requests,json
url = 'http://localhost:9080/'
header = {'content-type' : 'application/json'}
d = {'name' : 'Foo'}
r = requests.post(url,data = json.dumps(d),headers = header)
And the same problem occurs if I use urrlib2 instead of requests. I even tried using it without sending the headers.