python为tornado添加recaptcha验证码功能


Posted in Python onFebruary 26, 2014
    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

   
3water.com<!DOCTYPE html>
3water.com<html>
3water.com<head>
3water.com3water.com<title>reCaptcha验证码</title>
3water.com</head>
3water.com<body>
3water.com3water.com<form action="" method="post">
3water.com3water.com<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k={{ publickey }}"></script>
3water.com3water.com<noscript>
3water.com3water.com3water.com<iframe src="http://www.google.com/recaptcha/api/noscript?k={{ publickey }}" height="300" width="500" frameborder="0"></iframe><br>
3water.com3water.com3water.com<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
3water.com3water.com3water.com<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
3water.com3water.com</noscript>
3water.com3water.com</form>
3water.com</body>
3water.com</html>
Python 相关文章推荐
python实现随机密码字典生成器示例
Apr 09 Python
python通过装饰器检查函数参数数据类型的方法
Mar 13 Python
Python简单实现enum功能的方法
Apr 25 Python
Python使用requests及BeautifulSoup构建爬虫实例代码
Jan 24 Python
python自动登录12306并自动点击验证码完成登录的实现源代码
Apr 25 Python
python 搭建简单的http server,可直接post文件的实例
Jan 03 Python
在Python中构建增广矩阵的实现方法
Jul 01 Python
python生成requirements.txt的两种方法
Sep 18 Python
python 回溯法模板详解
Feb 26 Python
Python通过两个dataframe用for循环求笛卡尔积
Apr 29 Python
Python3 使用pip安装git并获取Yahoo金融数据的操作
Apr 08 Python
Python 读取千万级数据自动写入 MySQL 数据库
Jun 28 Python
python实现博客文章爬虫示例
Feb 26 #Python
python处理中文编码和判断编码示例
Feb 26 #Python
python实现网页链接提取的方法分享
Feb 25 #Python
python3模拟百度登录并实现百度贴吧签到示例分享(百度贴吧自动签到)
Feb 24 #Python
python实现socket客户端和服务端简单示例
Feb 24 #Python
python抓取网页内容示例分享
Feb 24 #Python
使用python装饰器验证配置文件示例
Feb 24 #Python
You might like
超强分页类2.0发布,支持自定义风格,默认4种显示模式
2007/01/02 PHP
php中的filesystem文件系统函数介绍及使用示例
2014/02/13 PHP
YII实现分页的方法
2014/07/09 PHP
Dwz与thinkphp整合下的数据导出到Excel实例
2014/12/04 PHP
PHP使用PDO 连接与连接管理操作实例分析
2020/04/21 PHP
在b/s开发中经常用到的javaScript技术
2006/08/23 Javascript
js里的prototype使用示例
2010/11/19 Javascript
JQuery中SetTimeOut传参问题探讨
2013/05/10 Javascript
关于页面嵌入swf覆盖div层的问题的解决方法
2014/02/11 Javascript
用js将内容复制到剪贴板兼容浏览器
2014/03/18 Javascript
Extjs的FileUploadField文件上传出现了两个上传按钮
2014/04/29 Javascript
JavaScript数组对象实现增加一个返回随机元素的方法
2015/07/27 Javascript
jQuery采用连缀写法实现的折叠菜单效果
2015/09/18 Javascript
jQuery实现点击行选中或取消CheckBox的方法
2016/08/01 Javascript
详解Vue整合axios的实例代码
2017/06/21 Javascript
extjs简介_动力节点Java学院整理
2017/07/17 Javascript
antd-mobile ListView长列表的数据更新遇到的坑
2020/04/08 Javascript
[04:02]2014DOTA2国际邀请赛 BBC每日综述中国战队将再度登顶
2014/07/21 DOTA
Python单链表的简单实现方法
2014/09/23 Python
python的类方法和静态方法
2014/12/13 Python
基于Django模板中的数字自增(详解)
2017/09/05 Python
pygame游戏之旅 添加碰撞效果的方法
2018/11/20 Python
Python爬虫文件下载图文教程
2018/12/23 Python
Python使用itchat 功能分析微信好友性别和位置
2019/08/05 Python
html5指南-6.如何创建离线web应用程序实现离线访问
2013/01/07 HTML / CSS
AE美国鹰日本官方网站: American Eagle Outfitters
2016/12/10 全球购物
白色公司:The White Company
2017/10/11 全球购物
素食餐饮项目创业计划书
2014/02/02 职场文书
关于青春的演讲稿
2014/05/05 职场文书
会计求职自荐信
2014/06/20 职场文书
农业项目建议书
2014/08/25 职场文书
幼儿园教师的自我评价范文
2014/09/17 职场文书
2014年仓库保管员工作总结
2014/12/03 职场文书
户外亲子活动总结
2015/05/08 职场文书
党员转正介绍人意见
2015/06/03 职场文书
2016年元旦寄语
2015/08/17 职场文书