Mi spiace dirlo, ma questo modulo, mentre funziona bene, è quasi del tutto privi di documenti, ed è il layout è un po 'di confusione per coloro di noi che preferiscono usare ">> help (modulename)" dopo l'installazione. Darò un esempio usando cherrypy e poi fare alcuni commenti relativi a cgi.
captcha.py contiene due funzioni e una classe:
display_html: che restituisce il familiare "scatola reCaptcha"
presentare: che presenta i valori inseriti dall'utente in background
RecapchaResponse: che è una classe contenitore che contiene la risposta dal reCaptcha
Per prima cosa è necessario importare il percorso completo in capcha.py, quindi creare un paio di funzioni che gestiscono la visualizzazione e la gestione della risposta.
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)
Sarà più o meno lo stesso se si utilizza CGI, basta usare la variabile d'ambiente REMOTE_ADDR dove ho usato request.headers e utilizzo campo di stoccaggio di cherrypy per fare i vostri controlli.
Non c'è magia, il modulo segue solo la documentazione: https://developers.google.com/recaptcha/docs/display
errori di convalida si potrebbe aver bisogno di gestire: https://developers.google.com/recaptcha/docs/verify
Hi Abate, Sono nuovo di Python, si può spiegare come utilizzare il pacchetto scaricato in maggiori dettagli? –
Dovresti installarlo come un normale pacchetto python. Ti consiglio di leggere un corso introduttivo su Python se sei nuovo a tutte queste cose. Puoi provare http://diveintopython.org/toc/index.html o http://docs.python.org/tutorial/index.html come un buon punto di partenza. – abbot