python为tornado添加recaptcha验证码功能

yipeiwu_com5年前Python基础

复制代码 代码如下:

    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

复制代码 代码如下:
  
jb51.net<!DOCTYPE html>
jb51.net<html>
jb51.net<head>
jb51.netjb51.net<title>reCaptcha验证码</title>
jb51.net</head>
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</html>

相关文章

Python封装原理与实现方法详解

本文实例讲述了Python封装原理与实现方法。分享给大家供大家参考,具体如下: 【封装】  隐藏对象的属性和实现细节,仅对外提供公共访问方式。 【好处】 1. 将变化隔离; 2...

用python写扫雷游戏实例代码分享

扫雷是一个非常经典的WIN游戏,我们教给大家用python语言来写出这个游戏,以下是全部实例代码: #!/usr/bin/python #coding:utf-8 #python...

Django高级编程之自定义Field实现多语言

Django高级编程之自定义Field实现多语言

自定义数据库字段 扩展默认的models.CharField和models.TextField使之成为支持多语言的字段。 可以轻松实现复用,无需配置多余选项 from django....

Python处理RSS、ATOM模块FEEDPARSER介绍

由于Google reader的关闭,这段时间接触rss的东西相对多很多。试过qq的reader,不怎么样,阅读速度没有,是否阅读的标记也没有。其他网站的不想用,又要多注册账户。 找到p...

Python实现快速多线程ping的方法

本文实例讲述了Python实现快速多线程ping的方法。分享给大家供大家参考。具体如下: #!/usr/bin/python #_*_coding:utf-8_*_ # ''' 名称...