recaptcha服务的python模块。
python-reCAPTCHA的Python项目详细描述
Recaptcha是一个免费的验证码服务,可以保护您的站点免受垃圾邮件的攻击, 计算机试图进行的恶意注册和其他形式的攻击 把自己伪装成一个人。
python recaptcha是一个python3.x模块,它提供了一个简单的 与reCAPTCHA API交互的方式。
安装
$ pip install python-reCAPTCHA
或
$ git clone https://github.com/rbika/python-reCAPTCHA.git $ cd python-reCAPTCHA $ python setup.py install $ cd .. && rm -rf python-reCAPTCHA
用法
要使用recaptcha,您需要为您的站点sign up for API keys。
一旦您注册了api密钥,向您的站点添加recaptcha包括 两步。
一。显示recaptcha小部件
通过调用generate_html函数生成小部件html。
>>> from recaptcha.captcha import generate_html >>> >>> public_key = "your_public_key" >>> recaptcha_html = generate_html(public_key) >>> >>> print(recaptcha_html) <script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=your_public_key"></script><noscript><iframe src="http://www.google.com/recaptcha/api/api/noscript?k=your_public_key"height="300" width="500" frameborder="0"></iframe><br><textarea name="recaptcha_challenge_field"rows="3" cols="40"></textarea><input type="hidden" name="recaptcha_response_field"value="manual_challenge"></noscript>
将生成的HTML放入表单中。如果你和Django Por一起工作 例如,您的表单如下:
<form action=""> {% autoscape on %}recaptcha_html{% endautoescape %} <input type="submit" value="Submit"> </form>
2.验证用户的anwser
在用户提交anwser之后,您应该获得所需的参数和 调用verify函数。又是一个django的例子。
>>> from recaptcha.captcha import verify >>> >>> private_key = "your_private_key" >>> remote_ip = request.META['REMOTE_ADDR'] >>> challenge = request.POST.get('recaptcha_challenge_field', '') >>> response = request.POST.get('recaptcha_response_field', '') >>> >>> result = verify(private_key, remote_ip, challenge, response) >>> >>> print(result) {'success': False, 'error': 'incorrect-captcha-sol'}
文档
文件将很快提供。