<s> import urllib2 , urllib <EOL> API_SSL_SERVER = "<STR_LIT>" <EOL> API_SERVER = "<STR_LIT>" <EOL> VERIFY_SERVER = "<STR_LIT>" <EOL> class RecaptchaResponse ( object ) : <EOL> def __init__ ( self , is_valid , error_code = None ) : <EOL> self . is_valid = is_valid <EOL> self . error_code = error_code <EOL> def displayhtml ( public_key , use_ssl = False , error = None ) : <EOL> """<STR_LIT>""" <EOL> error_param = '<STR_LIT>' <EOL> if error : <EOL> error_param = '<STR_LIT>' % error <EOL> if use_ssl : <EOL> server = API_SSL_SERVER <EOL> else : <EOL> server = API_SERVER <EOL> return """<STR_LIT>""" % { <EOL> '<STR_LIT>' : server , <EOL> '<STR_LIT>' : public_key , <EOL> '<STR_LIT>' : error_param , <EOL> } <EOL> def submit ( recaptcha_challenge_field , recaptcha_response_field , private_key , <EOL> remoteip ) : <EOL> """<STR_LIT>""" <EOL> if not ( recaptcha_response_field and recaptcha_challenge_field and <EOL> len ( recaptcha_response_field ) and len ( recaptcha_challenge_field ) ) : <EOL> return RecaptchaResponse ( is_valid = False , <EOL> error_code = '<STR_LIT>' ) <EOL> def encode_if_necessary ( s ) : <EOL> if isinstance ( s , unicode ) : <EOL> return s . encode ( '<STR_LIT:utf-8>' ) <EOL> return s <EOL> params = urllib . urlencode ( { <EOL> '<STR_LIT>' : encode_if_necessary ( private_key ) , <EOL> '<STR_LIT>' : encode_if_necessary ( remoteip ) , <EOL> '<STR_LIT>' : encode_if_necessary ( recaptcha_challenge_field ) , <EOL> '<STR_LIT>' : encode_if_necessary ( recaptcha_response_field ) , <EOL> } ) <EOL> request = urllib2 . Request ( <EOL> url = "<STR_LIT>" % VERIFY_SERVER , <EOL> data = params , <EOL> headers = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> ) <EOL> httpresp = urllib2 . urlopen ( request ) <EOL> return_values = httpresp . read ( ) . splitlines ( ) ; <EOL> httpresp . close ( ) ; <EOL> return_code = return_values [ <NUM_LIT:0> ] <EOL> if return_code == "<STR_LIT:true>" : <EOL> return RecaptchaResponse ( is_valid = True ) <EOL> else : <EOL> return RecaptchaResponse ( is_valid = False , error_code = return_values [ <NUM_LIT:1> ] ) </s>
