Alright I will answer this myself :P.
Taking hints from an example at python's official documentation, I excluded data
from Request
and separately passed request and data
to urlopen()
. Below is the updated snippet -
params = urllib.parse.urlencode({
'privatekey': encode_if_necessary(private_key),
'remoteip': encode_if_necessary(remoteip),
'challenge': encode_if_necessary(recaptcha_challenge_field),
'response': encode_if_necessary(recaptcha_response_field),
})
if use_ssl:
verify_url = "https://%s/recaptcha/api/verify" % VERIFY_SERVER
else:
verify_url = "http://%s/recaptcha/api/verify" % VERIFY_SERVER
# do not add data to Request instead pass it separately to urlopen()
data = params.encode('utf-8')
request = urllib.request.Request(verify_url)
request.add_header("Content-type","application/x-www-form-urlencoded")
request.add_header("User-agent", "reCAPTCHA Python")
httpresp = urllib.request.urlopen(request, data)
Despite of solving the problem I still do not know why the code generated by 2to3.py did not work. According to the documentation it should have worked.