首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python-3请求参数编码错误

Python-3请求参数编码错误
EN

Stack Overflow用户
提问于 2014-02-15 09:55:37
回答 2查看 1.4K关注 0票数 2

我一直在从事一个基于djangoPython 3项目。我正在尝试合并captcha。我选择了django-recaptcha,但不幸的是,这个包不能用于python3。所以试着为python3量身定做。我做了一些2to3的事情,并根据需要做了一些修改。除了url encoding for Request之外,一切看起来都很好。

下面的代码段会产生POST data should be bytes or an iterable of bytes. It cannot be of type str.异常。

代码语言:javascript
复制
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中对网址和其他东西进行编码-

代码语言:javascript
复制
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异常。

有人知道怎么修吗?任何帮助都很感激:)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-15 19:25:23

好的,我自己回答:P。

python的官方文档的一个例子中的提示为例,我将data排除在Request之外,并分别将request and data传递给urlopen()。下面是更新的片段-

代码语言:javascript
复制
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.

票数 2
EN

Stack Overflow用户

发布于 2014-02-15 19:26:25

您猜对了,您需要对数据进行编码,而不是以您的方式进行编码。

正如@Sheena在这就是答案中所写的,您需要两个步骤来编码您的数据:

代码语言:javascript
复制
data = urllib.parse.urlencode(values)
binary_data = data.encode('utf-8')
req = urllib.request.Request(url, binary_data)

也不要再给url打电话了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21795976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档