概述
复制代码 代码如下:
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 = '填入你的 public key'
privatekey = '填入你的 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())
#获取验证结果,这里直接将返回结果输出到页面
self.write(res.read().decode())
if __name__ == '__main__':
server = tornado.httpserver.HTTPServer(Application())
server.listen(10001)
tornado.ioloop.IOLoop.instance().start()
templates/index.html
复制代码 代码如下:
uoften.com<!DOCTYPE html>
uoften.com<html>
uoften.com<head>
uoften.comuoften.com<title>reCaptcha验证码</title>
uoften.com</head>
uoften.com<body>
uoften.comuoften.com<form action="" method="post">
uoften.comuoften.com<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k={{ publickey }}"></script>
uoften.comuoften.com<noscript>
uoften.comuoften.comuoften.com<iframe src="http://www.google.com/recaptcha/api/noscript?k={{ publickey }}" height="300" width="500" frameborder="0"></iframe><br>
uoften.comuoften.comuoften.com<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
uoften.comuoften.comuoften.com<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
uoften.comuoften.com</noscript>
uoften.comuoften.com</form>
uoften.com</body>
uoften.com</html>
最后
以上就是火星上苗条为你收集整理的python为tornado添加recaptcha验证码功能的全部内容,希望文章能够帮你解决python为tornado添加recaptcha验证码功能所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复