【发布时间】:2014-03-14 19:04:45
【问题描述】:
我一直在从事基于Python 3 的django 项目。我正在尝试合并captcha。我选择了django-recaptcha,但不幸的是该包不适用于python3。所以试图为python3定制它。我做了一些2to3 的东西,并根据需要进行了一些更改。除了url encoding for Request,一切似乎都正常。
执行 sn-p 会产生 POST data should be bytes or an iterable of bytes. It cannot be of type str. 异常。
def encode_if_necessary(s):
if isinstance(s, str):
return s.encode('utf-8')
return s
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
request = urllib.request.Request(
url= verify_url,
data=params,
headers={
"Content-type": "application/x-www-form-urlencoded",
"User-agent": "reCAPTCHA Python"
}
)
httpresp = urllib.request.urlopen(request)
所以我尝试对request中的url和其他内容进行编码-
request = urllib.request.Request(
url= encode_if_necessary(verify_url),
data=params,
headers={
"Content-type": encode_if_necessary("application/x-www-form-urlencoded"),
"User-agent": encode_if_necessary("reCAPTCHA Python")
}
)
但这会产生urlopen error unknown url type: b'http 异常。
有人知道怎么解决吗?任何帮助表示赞赏:)。
【问题讨论】:
标签: django python-3.x captcha