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