Python adds reCAPTCHA validation code to Tornado _python

Source: Internet
Author: User
Tags recaptcha validation urlencode

Copy Code code 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 ())
#获取验证结果, this directly outputs the returned results 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 Code code as follows:

jb51.net<! DOCTYPE html>
Jb51.netJb51.netJb51.netjb51.net<title>recaptcha Verification Code </title>
Jb51.netJb51.net<body>
Jb51.netjb51.net<form action= "" method= "POST" >
Jb51.netjb51.net<script type= "Text/javascript" src= "http://www.google.com/recaptcha/api/challenge?k={{ PublicKey}} "></script>
Jb51.netjb51.net<noscript>
Jb51.netjb51.netjb51.net<iframe src= "http://www.google.com/recaptcha/api/noscript?k={{PublicKey}" height= "Width=" frameborder= "0" ></iframe><br>
Jb51.netjb51.netjb51.net<textarea name= "Recaptcha_challenge_field" rows= "3" cols= "></textarea>"
Jb51.netjb51.netjb51.net<input type= "hidden" name= "Recaptcha_response_field" value= "Manual_challenge" >
Jb51.netjb51.net</noscript>
Jb51.netjb51.net</form>
Jb51.net</body>
Jb51.net

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.