python为tornado添加recaptcha验证码功能

所属分类: 脚本专栏 / python 阅读数: 296
收藏 0 赞 0 分享

复制代码 代码如下:

    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 OrderedDict的使用案例解析

这篇文章主要介绍了Python OrderedDict的使用案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python计算n的阶乘的方法代码

在本篇文章里小编给各位整理的是关于python计算n的阶乘的相关知识点,需要的朋友们参考下。
收藏 0 赞 0 分享

DJANGO-URL反向解析REVERSE实例讲解

在本篇文章里小编给大家整理的是一篇关于DJANGO-URL反向解析REVERSE的相关知识点内容,需要的朋友们学习下。
收藏 0 赞 0 分享

python实现淘宝购物系统

这篇文章主要为大家详细介绍了python实现简易的淘宝购物系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Python aiohttp百万并发极限测试实例分析

这篇文章主要介绍了Python aiohttp百万并发极限测试,结合实例形式分析了Python异步编程基于aiohttp客户端高并发请求的相关操作技巧与使用注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

python单例模式原理与创建方法实例分析

这篇文章主要介绍了python单例模式原理与创建方法,结合实例形式分析了Python单例模式的概念、原理、定义、使用方法及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

python图的深度优先和广度优先算法实例分析

这篇文章主要介绍了python图的深度优先和广度优先算法,结合实例形式分析了图的深度优先算法与广度优先算法相关概念、原理、实现技巧与操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现树的深度优先遍历与广度优先遍历详解

这篇文章主要介绍了python实现树的深度优先遍历与广度优先遍历,详细分析了树的深度优先遍历与广度优先遍历原理及Python相关实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

通过字符串导入 Python 模块的方法详解

这篇文章主要介绍了通过字符串导入 Python 模块的方法详解,本文通过实例结合,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python用类实现文章敏感词的过滤方法示例

这篇文章主要介绍了python用类实现文章敏感词的过滤方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多