Post to Tornado server

2019-06-15 17:43发布

I am trying to post to my Tornado Web Server but keep getting a 405 Error. Not sure what is going wrong. I am fairly new to python but I have been searching up on this for about a month and finally decided to give it a go.

Tornado Web Server:

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
import os
import string
from time import sleep
from datetime import datetime
import hashlib
import json

from tornado.options import define, options

define("port", default=8001, help="run on the given port", type=int)


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

class LoginHandler(tornado.web.RequestHandler):
    def get(self):
        email_address = self.get_argument('email')
        password = self.get_argument('password')
        if email_address == '':
            login_response = "{'error': true, 'msg': 'Please enter your email address.'}"
        elif password == '':
            login_response = "{'error': true, 'msg': 'Please enter your password.'}"
        else:
            login_response = "{'error': true, 'msg': 'Thank You.'}"
            self.response.headers['Content-Type'] = "application/json"
            self.response.out.write(json.dumps(login_response))



def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        (r"/", MainHandler),
        (r"/login", LoginHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()


if __name__ == "__main__":
    main()

Html:

    <form id="login_form" action="http://www.mydomain.com/server/login" method="post">
    <label class="grey" for="log">Email Address</label><br />
    <input type="text" name="email" style="width:99%" id="email"><br>
    <label class="grey" for="pwd">Password</label><br />
    <input type="password" name="password" style="width:99%" id="password"><br />
    <label>
    <input name="remember" type="checkbox" checked="checked" value="1" /> &nbsp;Remember me</label><br /><br />
    <input type="submit" id="loginbtn" name="login" value="LOGIN" class="button"><br />
    <a href="forgotpassword.php">Forgot your password?</a>
    </form>

1条回答
男人必须洒脱
2楼-- · 2019-06-15 18:42

You will have to change the LoginHandler to accept a POST request.

Like so:

class LoginHandler(tornado.web.RequestHandler):
    def post(self):
        email_address = self.get_argument('email', '')
        password = self.get_argument('password', '')

        if not email_address:
            login_response = {
                'error': True, 
                'msg': 'Please enter your email address.'
            }
        elif not password:
            login_response = {
                'error': True, 
                'msg': 'Please enter your password.'
            }
        else:
            login_response = {
                'error': True, 
                'msg': 'Thank You.'
            }

        self.write(login_response)
查看更多
登录 后发表回答