Python adds recaptcha verification code for 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


# Get 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'

# Verification Code
Challenge = self. get_argument ('recaptcha _ challenge_field ')
# User input
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 ())
# Obtain the verification result. The returned result is directly output 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 codeThe Code is as follows:
Jb51.net <! DOCTYPE html>
Jb51.net Jb51.net Jb51.netjb51.net <title> reCaptcha Verification Code </title>
Jb51.net Jb51.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 =" 300 "width =" 500 "frameborder =" 0 "> </iframe> <br>
Jb51.netjb51.netjb51.net <textarea name = "recaptcha_challenge_field" rows = "3" cols = "40"> </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.