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 常用 PEP8 编码规范详解

Python 常用 PEP8 编码规范 代码布局 缩进 每级缩进用4个空格。 括号中使用垂直隐式缩进或使用悬挂缩进。 EXAMPLE: # (垂直隐式缩进)对准左括号 f...

正则给header的冒号两边参数添加单引号(Python请求用)

正则给header的冒号两边参数添加单引号(Python请求用)

正则给header的冒号两边参数添加单引号(Python请求用) 直接从浏览器Chrome复制header值如下: Host: kyfw.12306.cn Connection:...

pip指定python位置安装软件包的方法

由于电脑上安装了多个版本的pip,以及不同的pip对应不同的python,因此有时候使用pip install安装某个包时,可能会没有安装在想要的位置。 具体而言,在我电脑上,运行pip...

Pytorch 之修改Tensor部分值方式

一:背景引入 对于一张图片,怎样修改局部像素值? 二:利用Tensor方法 比如输入全零tensor,可认为为黑色图片 >>> n=torch.FloatT...

多版本Python共存的配置方法

多版本Python共存的配置方法

同一台PC中,如果同时安装Python2和Python3环境,会在执行python命令时出现错误,操作系统会找到默认的配置进行执行。怎么样才能让python2和python3一起工作又互...