python为tornado添加recaptcha验证码功能

 更新时间:2014年02月26日 14:08:18   作者:  
tornado作为微框架,并没有自带验证码组件,recaptcha是著名的验证码解决方案,简单易用,被很多公司运用来防止恶意注册和评论。tornado添加recaptchaHA非常容易

复制代码 代码如下:

    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>

相关文章

  • 使用Pandas如何读取多个分隔方式的文件

    使用Pandas如何读取多个分隔方式的文件

    这篇文章主要介绍了使用Pandas如何读取多个分隔方式的文件问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • 使用python把xmind转换成excel测试用例的实现代码

    使用python把xmind转换成excel测试用例的实现代码

    这篇文章主要介绍了使用python把xmind转换成excel测试用例的实现代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • Anaconda3中的Jupyter notebook添加目录插件的实现

    Anaconda3中的Jupyter notebook添加目录插件的实现

    这篇文章主要介绍了Anaconda3中的Jupyter notebook添加目录插件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • TensorFlow自定义损失函数来预测商品销售量

    TensorFlow自定义损失函数来预测商品销售量

    这篇文章主要介绍了TensorFlow自定义损失函数——预测商品销售量,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • Django celery实现异步任务操作,并在后台运行(守护进程)

    Django celery实现异步任务操作,并在后台运行(守护进程)

    这篇文章主要介绍了Django celery实现异步任务操作,并在后台运行(守护进程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • python的Jenkins接口调用方式

    python的Jenkins接口调用方式

    这篇文章主要介绍了python的Jenkins接口调用方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • 使用OpenCV实现逐帧获取视频图片

    使用OpenCV实现逐帧获取视频图片

    这篇文章主要为大家详细介绍了如何使用OpenCV实现逐帧获取视频中的图片用来标注,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • Python利用sched模块实现定时任务

    Python利用sched模块实现定时任务

    今天我们来介绍一下Python当中的定时任务,主要用到的模块是sched,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-04-04
  • python for 循环获取index索引的方法

    python for 循环获取index索引的方法

    今天小编就为大家分享一篇python for 循环获取index索引的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • Python中def的用法以及def是什么意思详解

    Python中def的用法以及def是什么意思详解

    这篇文章主要介绍了Python中def的用法以及def是什么意思的相关资料,文中介绍了Python中函数的定义和使用方法,还给出了详细的代码示例,需要的朋友可以参考下
    2024-10-10

最新评论