遺憾地說,但這個模塊,同時它工作得很好,幾乎完全是無證,它的佈局是一點點迷惑那些我們的人在安裝後更喜歡使用「>> help(modulename)」。我會舉一個使用cherrypy的例子,然後做一些與cgi相關的評論。
captcha.py包含兩個功能和一類:
您首先需要將完整的路徑導入capcha.py,然後創建一些處理顯示和處理響應的函數。
from recaptcha.client import captcha
class Main(object):
@cherrypy.expose
def display_recaptcha(self, *args, **kwargs):
public = "public_key_string_you_got_from_recaptcha"
captcha_html = captcha.displayhtml(
public,
use_ssl=False,
error="Something broke!")
# You'll probably want to add error message handling here if you
# have been redirected from a failed attempt
return """
<form action="validate">
%s
<input type=submit value="Submit Captcha Text" \>
</form>
"""%captcha_html
# send the recaptcha fields for validation
@cherrypy.expose
def validate(self, *args, **kwargs):
# these should be here, in the real world, you'd display a nice error
# then redirect the user to something useful
if not "recaptcha_challenge_field" in kwargs:
return "no recaptcha_challenge_field"
if not "recaptcha_response_field" in kwargs:
return "no recaptcha_response_field"
recaptcha_challenge_field = kwargs["recaptcha_challenge_field"]
recaptcha_response_field = kwargs["recaptcha_response_field"]
# response is just the RecaptchaResponse container class. You'll need
# to check is_valid and error_code
response = captcha.submit(
recaptcha_challenge_field,
recaptcha_response_field,
"private_key_string_you_got_from_recaptcha",
cherrypy.request.headers["Remote-Addr"],)
if response.is_valid:
#redirect to where ever we want to go on success
raise cherrypy.HTTPRedirect("success_page")
if response.error_code:
# this tacks on the error to the redirect, so you can let the
# user knowwhy their submission failed (not handled above,
# but you are smart :-))
raise cherrypy.HTTPRedirect(
"display_recaptcha?error=%s"%response.error_code)
這將是非常如果使用CGI相同,只是用REMOTE_ADDR環境變量,我使用CherryPy的request.headers和使用字段存儲做你檢查。
有沒有神奇的,模塊只是遵循文檔: https://developers.google.com/recaptcha/docs/display
驗證錯誤,你可能需要處理: https://developers.google.com/recaptcha/docs/verify
嗨方丈, 我是新來的Python,你能解釋一下如何更詳細地使用下載的軟件包? – 2009-09-17 23:06:21
您應該將其安裝爲常規python包。 我建議你閱讀一些關於python的入門課程,如果你對所有這些東西都很陌生。你可以嘗試http://diveintopython.org/toc/index.html或http://docs.python.org/tutorial/index.html作爲一個很好的起點。 – abbot 2009-09-18 18:58:10