python為tornado添加recaptcha驗(yàn)證碼功能
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é)果輸出到頁(yè)面
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驗(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?numpy中np.random.seed()的詳細(xì)用法實(shí)例
在學(xué)習(xí)人工智能時(shí),大量的使用了np.random.seed(),利用隨機(jī)數(shù)種子,使得每次生成的隨機(jī)數(shù)相同,下面這篇文章主要給大家介紹了關(guān)于Python?numpy中np.random.seed()的詳細(xì)用法,需要的朋友可以參考下2022-08-08Pytest?fixture及conftest相關(guān)詳解
這篇文章主要介紹了Pytest?fixture及conftest相關(guān)詳解,fixture是在測(cè)試函數(shù)運(yùn)行前后,由pytest執(zhí)行的外殼函數(shù),更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-09-09Python使用到第三方庫(kù)PyMuPDF圖片與pdf相互轉(zhuǎn)換
今天為大家介紹個(gè)比較簡(jiǎn)單的Python第三方庫(kù)PyMuPDF進(jìn)行圖片和pdf之間的相互轉(zhuǎn)換,以下就是利用PyMuPDF進(jìn)行pdf與圖片之間的互轉(zhuǎn)2019-05-05mac安裝python3后使用pip和pip3的區(qū)別說(shuō)明
這篇文章主要介紹了mac安裝python3后使用pip和pip3的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09python使用xlrd實(shí)現(xiàn)檢索excel中某列含有指定字符串記錄的方法
這篇文章主要介紹了python使用xlrd實(shí)現(xiàn)檢索excel中某列含有指定字符串記錄的方法,涉及Python使用xlrd模塊檢索Excel的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05詳解PyQt5中textBrowser顯示print語(yǔ)句輸出的簡(jiǎn)單方法
這篇文章主要介紹了詳解PyQt5中textBrowser顯示print語(yǔ)句輸出的簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08深入理解Python變量的數(shù)據(jù)類型和存儲(chǔ)
這篇文章主要介紹了Python變量的數(shù)據(jù)類型和存儲(chǔ),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Python爬蟲(chóng)包BeautifulSoup實(shí)例(三)
這篇文章主要為大家詳細(xì)介紹了Python爬蟲(chóng)包BeautifulSoup實(shí)例,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2018-06-06圖解python全局變量與局部變量相關(guān)知識(shí)
這篇文章主要介紹了圖解python全局變量與局部變量相關(guān)知識(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11