Skip to main content

Google ReCaptcha for Cherrypy.

Project description

Google ReCaptcha for cherrypy.

Installation

pip install marbaloo_recaptcha

Usage

# app.py
import cherrypy
import marbaloo_recaptcha
cherrypy.tools.recaptcha = marbaloo_recaptcha.Tool()


class Root(object):

    @cherrypy.expose
    def index(self, **kwargs):
        recaptcha = cherrypy.request.recaptcha
        if 'submit_recaptcha' in kwargs:
            if recaptcha.verify():
                return 'Success'
            else:
                return 'Fail'
        else:
            return '''
            <head>
                %s
            </head>
            <body>
                <form method="post">
                    %s
                    <input type="submit" name="submit_recaptcha"/>
                </form>
            </body>
            ''' % (recaptcha.get_html_head(), recaptcha.get_html_field(enable_no_script=True))

config = {
    'global': {
        "server.socket_host": "0.0.0.0",
        "server.socket_port": 9095,
    },
    '/': {
        'tools.recaptcha.on': True,
        'tools.recaptcha.remoteip': '',
        # For production level, uncomment this lines
        # 'tools.recaptcha.secret_key': 'MY_SECRET_KEY',
        # 'tools.recaptcha.site_key': 'MY_SITE_KEY',
    }
}
cherrypy.quickstart(Root(), '/', config)

Advanced Usage:

import cherrypy
import marbaloo_recaptcha
cherrypy.tools.recaptcha = marbaloo_recaptcha.Tool()


class Root(object):

    @cherrypy.expose
    def index(self, **kwargs):
        recaptcha = cherrypy.request.recaptcha
        if 'submit_recaptcha' in kwargs:
            if recaptcha.verify():
                return 'Success'
            else:
                return 'Fail'
        else:
            return '''
            <head>
                %s
            </head>
            <body>
                <form method="post">
                    %s
                    <input type="submit" name="submit_recaptcha"/>
                </form>
            </body>
            ''' % (recaptcha.get_html_head(defer=True,
                                           async=True,
                                           render='onload',
                                           onload='my_onload_javascript',
                                           hl='fa'),
                   recaptcha.get_html_field(data_attributes={
                                                                'data-theme': 'dark',
                                                                'data-size': 'compact',
                                                                'data-type': 'image'
                                                            },
                                            enable_no_script=True))

config = {
    'global': {
        "server.socket_host": "0.0.0.0",
        "server.socket_port": 9095,
    },
    '/': {
        'tools.recaptcha.on': True,
        'tools.recaptcha.remoteip': '',
        # For production level, uncomment this lines
        # 'tools.recaptcha.secret_key': 'MY_SECRET_KEY',
        # 'tools.recaptcha.site_key': 'MY_SITE_KEY',
    }
}
cherrypy.quickstart(Root(), '/', config)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marbaloo_recaptcha-0.1.0.tar.gz (3.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page