python為tornado添加recaptcha驗(yàn)證碼功能
更新時(shí)間:2014年02月26日 14:08:18 作者:
tornado作為微框架,并沒有自帶驗(yàn)證碼組件,recaptcha是著名的驗(yàn)證碼解決方案,簡單易用,被很多公司運(yùn)用來防止惡意注冊和評(píng)論。tornado添加recaptchaHA非常容易
復(fù)制代碼 代碼如下:
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'
#驗(yàn)證碼
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())
#獲取驗(yàn)證結(jié)果,這里直接將返回結(jié)果輸出到頁面
self.write(res.read().decode())
if __name__ == '__main__':
server = tornado.httpserver.HTTPServer(Application())
server.listen(10001)
tornado.ioloop.IOLoop.instance().start()
templates/index.html
復(fù)制代碼 代碼如下:
jb51.net<!DOCTYPE html>
jb51.net<html>
jb51.net<head>
jb51.netjb51.net<title>reCaptcha驗(yàn)證碼</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>
相關(guān)文章
使用python把xmind轉(zhuǎn)換成excel測試用例的實(shí)現(xiàn)代碼
這篇文章主要介紹了使用python把xmind轉(zhuǎn)換成excel測試用例的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Anaconda3中的Jupyter notebook添加目錄插件的實(shí)現(xiàn)
這篇文章主要介紹了Anaconda3中的Jupyter notebook添加目錄插件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
TensorFlow自定義損失函數(shù)來預(yù)測商品銷售量
這篇文章主要介紹了TensorFlow自定義損失函數(shù)——預(yù)測商品銷售量,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Django celery實(shí)現(xiàn)異步任務(wù)操作,并在后臺(tái)運(yùn)行(守護(hù)進(jìn)程)
這篇文章主要介紹了Django celery實(shí)現(xiàn)異步任務(wù)操作,并在后臺(tái)運(yùn)行(守護(hù)進(jìn)程),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python利用sched模塊實(shí)現(xiàn)定時(shí)任務(wù)
今天我們來介紹一下Python當(dāng)中的定時(shí)任務(wù),主要用到的模塊是sched,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-04-04
python for 循環(huán)獲取index索引的方法
今天小編就為大家分享一篇python for 循環(huán)獲取index索引的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02

