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