python为tornado添加recaptcha验证码功能

所属分类: 脚本专栏 / python 阅读数: 297
收藏 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简单操作sqlite3的方法示例

这篇文章主要介绍了Python简单操作sqlite3的方法,结合实例形式分析了Python针对sqlite3数据库的读取、创建、增删改查等基本操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现遍历目录的方法【测试可用】

这篇文章主要介绍了Python实现遍历目录的方法,涉及Python针对目录与文件的遍历、判断、读取相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python条件变量之生产者与消费者操作实例分析

这篇文章主要介绍了python条件变量之生产者与消费者操作,结合具体实例形式分析了Python条件变量的概念、原理、及线程操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

利用pyinstaller或virtualenv将python程序打包详解

这篇文章主要给大家介绍了利用pyinstaller将python程序打包的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python多线程经典问题之乘客做公交车算法实例

这篇文章主要介绍了Python多线程经典问题之乘客做公交车算法,简单描述了乘客坐公交车问题并结合实例形式分析了Python多线程实现乘客坐公交车算法的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python编程实现删除VC临时文件及Debug目录的方法

这篇文章主要介绍了Python编程实现删除VC临时文件及Debug目录的方法,涉及Python针对文件与目录的遍历、删除等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python3中dict(字典)的使用方法示例

这篇文章主要介绍了python3中dict(字典)的使用方法,文中给出了详细的功能列举,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python中.py文件打包成exe可执行文件详解

这篇文章主要给大家介绍了在Python中.py文件打包成exe可执行文件的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python编程之event对象的用法实例分析

这篇文章主要介绍了Python编程之event对象的用法,结合实例形式分析了event对象在线程通信中的作用与使用方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Python爬取网页中的图片(搜狗图片)详解

没想到python是如此强大,令人着迷,以前看见图片总是一张一张复制粘贴,现在好了,学会python就可以用程序将一张张图片,保存下来。下面这篇文章主要给大家介绍了利用Python3.6爬取搜狗图片网页中图片的相关资料,需要的朋友可以参考下。
收藏 0 赞 0 分享
查看更多