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>
相關文章
Python?numpy中np.random.seed()的詳細用法實例
在學習人工智能時,大量的使用了np.random.seed(),利用隨機數種子,使得每次生成的隨機數相同,下面這篇文章主要給大家介紹了關于Python?numpy中np.random.seed()的詳細用法,需要的朋友可以參考下2022-08-08Python使用到第三方庫PyMuPDF圖片與pdf相互轉換
今天為大家介紹個比較簡單的Python第三方庫PyMuPDF進行圖片和pdf之間的相互轉換,以下就是利用PyMuPDF進行pdf與圖片之間的互轉2019-05-05mac安裝python3后使用pip和pip3的區(qū)別說明
這篇文章主要介紹了mac安裝python3后使用pip和pip3的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09python使用xlrd實現檢索excel中某列含有指定字符串記錄的方法
這篇文章主要介紹了python使用xlrd實現檢索excel中某列含有指定字符串記錄的方法,涉及Python使用xlrd模塊檢索Excel的技巧,非常具有實用價值,需要的朋友可以參考下2015-05-05詳解PyQt5中textBrowser顯示print語句輸出的簡單方法
這篇文章主要介紹了詳解PyQt5中textBrowser顯示print語句輸出的簡單方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08