国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python為tornado添加recaptcha驗證碼功能

2019-11-25 18:30:14
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

    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

復制代碼 代碼如下:
  
VeVB.COm<!DOCTYPE html>
VeVB.COm<html>
VeVB.COm<head>
VeVB.COmVeVB.COm<title>reCaptcha驗證碼</title>
VeVB.COm</head>
VeVB.COm<body>
VeVB.COmVeVB.COm<form action="" method="post">
VeVB.COmVeVB.COm<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k={{ publickey }}"></script>
VeVB.COmVeVB.COm<noscript>
VeVB.COmVeVB.COmVeVB.COm<iframe src="http://www.google.com/recaptcha/api/noscript?k={{ publickey }}" height="300" width="500" frameborder="0"></iframe><br>
VeVB.COmVeVB.COmVeVB.COm<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
VeVB.COmVeVB.COmVeVB.COm<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
VeVB.COmVeVB.COm</noscript>
VeVB.COmVeVB.COm</form>
VeVB.COm</body>
VeVB.COm</html>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庆云县| 中卫市| 沅陵县| 衡阳县| 蓝山县| 东光县| 大同市| 河西区| 湖南省| 贵州省| 景谷| 乌鲁木齐市| 华池县| 哈巴河县| 兴和县| 肃宁县| 乐陵市| 逊克县| 连山| 安阳县| 双江| 延安市| 兴城市| 苗栗市| 乌兰察布市| 江源县| 余干县| 嵩明县| 乌鲁木齐市| 阳原县| 红桥区| 张掖市| 曲水县| 始兴县| 寻乌县| 沙湾县| 宿州市| 四会市| 揭阳市| 上高县| 方山县|