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