def request(*args, **kwargs): """ Make a HTTP request with a proxy if configured. """ if getattr(settings, 'RECAPTCHA_PROXY', False): proxy = ProxyHandler({ 'http': settings.RECAPTCHA_PROXY, 'https': settings.RECAPTCHA_PROXY, }) opener = build_opener(proxy) return opener.open(*args, **kwargs) else: return urlopen(*args, **kwargs)
def recaptcha_request(params): request_object = Request( url="https://%s/recaptcha/api/siteverify" % getattr(settings, "RECAPTCHA_DOMAIN", DEFAULT_RECAPTCHA_DOMAIN), data=params, headers={ "Content-type": "application/x-www-form-urlencoded", "User-agent": "reCAPTCHA Django" }) # Add proxy values to opener if needed. opener_args = [] proxies = getattr(settings, "RECAPTCHA_PROXY", {}) if proxies: opener_args = [ProxyHandler(proxies)] opener = build_opener(*opener_args) # Get response from POST to Google endpoint. return opener.open(request_object, timeout=getattr(settings, "RECAPTCHA_VERIFY_REQUEST_TIMEOUT", 10))