Python adds reCAPTCHA captcha function to tornado

Source: Internet
Author: User
Copy CodeThe code is as follows:


From urllib.request import Urlopen
From Urllib.parse import UrlEncode
Import Tornado.httpserver
Import Tornado.ioloop
Import Tornado.web


#获取key: Https://www.google.com/recaptcha/whyrecaptcha
PublicKey = ' Fill in your public key '
Privatekey = ' Fill in your private key '


Class Application (Tornado.web.Application):
def __init__ (self):
handlers = [
(R '/', Indexhandler)
]
Settings = Dict (
Template_path= "Templates",
)

Tornado.web.application.__init__ (self, handlers, **settings)


Class Indexhandler (Tornado.web.RequestHandler):
def get (self):
Self.render (' index.html ', Publickey=publickey)

Def post (self):
url = ' Http://www.google.com/recaptcha/api/verify '

#验证码
Challenge = self.get_argument (' Recaptcha_challenge_field ')
#用户输入
Response = self.get_argument (' Recaptcha_response_field ')

data = {
' Privatekey ': Privatekey,
' Remoteip ': self.request.remote_ip,
' Challenge ': Challenge,
' Response ': response
}

res = Urlopen (URL, data=urlencode (data). Encode ())
#获取验证结果, output the returned results directly to the page
Self.write (Res.read (). Decode ())


if __name__ = = ' __main__ ':
Server = Tornado.httpserver.HTTPServer (Application ())
Server.listen (10001)
Tornado.ioloop.IOLoop.instance (). Start ()

Templates/index.html
Copy the Code code as follows:


Jb51.net
Jb51.net
Jb51.net
Jb51.netjb51.net <title>reCAPTCHA Verification Code</title>
Jb51.net
Jb51.net
Jb51.netjb51.net
Jb51.net
Jb51.net
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.