didip / python-recaptcha
Simple python library for reCAPTCHA
Clone this repository (size: 3.6 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/didip/python-recaptcha/
| commit 0: | d35cf12f1fa8 |
| branch: | default |
| tags: | tip |
Initial commit
Changed (Δ3.2 KB):
raw changeset »
recaptcha.py (99 lines added, 0 lines removed)
1 |
import urllib2, urllib |
|
2 |
||
3 |
API_SSL_SERVER="https://api-secure.recaptcha.net" |
|
4 |
API_SERVER="http://api.recaptcha.net" |
|
5 |
VERIFY_SERVER="api-verify.recaptcha.net" |
|
6 |
||
7 |
class RecaptchaResponse(object): |
|
8 |
def __init__(self, is_valid, error_code=None): |
|
9 |
self.is_valid = is_valid |
|
10 |
self.error_code = error_code |
|
11 |
||
12 |
def displayhtml (public_key, use_ssl = False, error = None, color = 'red'): |
|
13 |
"""Gets the HTML to display for reCAPTCHA |
|
14 |
||
15 |
public_key -- The public api key |
|
16 |
use_ssl -- Should the request be sent over ssl? |
|
17 |
error -- An error message to display (from RecaptchaResponse.error_code)""" |
|
18 |
||
19 |
error_param = '' |
|
20 |
if error: error_param = '&error=%s' % error |
|
21 |
||
22 |
if use_ssl: server = API_SSL_SERVER |
|
23 |
else: server = API_SERVER |
|
24 |
||
25 |
if color not in ['red', 'white']: color = 'red' |
|
26 |
||
27 |
return """ |
|
28 |
<script type="text/javascript"> |
|
29 |
var RecaptchaOptions={theme:'%(Color)s'}; |
|
30 |
</script> |
|
31 |
||
32 |
<script type="text/javascript" src="%(ApiServer)s/challenge?k=%(PublicKey)s%(ErrorParam)s"></script> |
|
33 |
||
34 |
<noscript> |
|
35 |
<iframe src="%(ApiServer)s/noscript?k=%(PublicKey)s%(ErrorParam)s" height="300" width="500" frameborder="0"></iframe><br /> |
|
36 |
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> |
|
37 |
<input type='hidden' name='recaptcha_response_field' value='manual_challenge' /> |
|
38 |
</noscript> |
|
39 |
""" % { |
|
40 |
'ApiServer' : server, |
|
41 |
'PublicKey' : public_key, |
|
42 |
'ErrorParam' : error_param, |
|
43 |
'Color' : color |
|
44 |
} |
|
45 |
||
46 |
||
47 |
def submit (recaptcha_challenge_field, |
|
48 |
recaptcha_response_field, |
|
49 |
private_key, |
|
50 |
remoteip): |
|
51 |
""" |
|
52 |
Submits a reCAPTCHA request for verification. Returns RecaptchaResponse |
|
53 |
for the request |
|
54 |
||
55 |
recaptcha_challenge_field -- The value of recaptcha_challenge_field from the form |
|
56 |
recaptcha_response_field -- The value of recaptcha_response_field from the form |
|
57 |
private_key -- your reCAPTCHA private key |
|
58 |
remoteip -- the user's ip address |
|
59 |
""" |
|
60 |
||
61 |
if not (recaptcha_response_field and recaptcha_challenge_field and |
|
62 |
len (recaptcha_response_field) and len (recaptcha_challenge_field)): |
|
63 |
return RecaptchaResponse (is_valid = False, error_code = 'incorrect-captcha-sol') |
|
64 |
||
65 |
||
66 |
def encode_if_necessary(s): |
|
67 |
if isinstance(s, unicode): |
|
68 |
return s.encode('utf-8') |
|
69 |
return s |
|
70 |
||
71 |
params = urllib.urlencode ({ |
|
72 |
'privatekey': encode_if_necessary(private_key), |
|
73 |
'remoteip' : encode_if_necessary(remoteip), |
|
74 |
'challenge': encode_if_necessary(recaptcha_challenge_field), |
|
75 |
'response' : encode_if_necessary(recaptcha_response_field), |
|
76 |
}) |
|
77 |
||
78 |
request = urllib2.Request ( |
|
79 |
url = "http://%s/verify" % VERIFY_SERVER, |
|
80 |
data = params, |
|
81 |
headers = { |
|
82 |
"Content-type": "application/x-www-form-urlencoded", |
|
83 |
"User-agent": "reCAPTCHA Python" |
|
84 |
} |
|
85 |
) |
|
86 |
||
87 |
httpresp = urllib2.urlopen (request) |
|
88 |
||
89 |
return_values = httpresp.read ().splitlines (); |
|
90 |
httpresp.close(); |
|
91 |
||
92 |
return_code = return_values [0] |
|
93 |
||
94 |
if (return_code == "true"): |
|
95 |
return RecaptchaResponse (is_valid=True) |
|
96 |
else: |
|
97 |
return RecaptchaResponse (is_valid=False, error_code = return_values [1]) |
|
98 |
||
99 |
