Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django Python-3请求参数编码错误_Django_Python 3.x_Captcha - Fatal编程技术网

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

Django Python-3请求参数编码错误,django,python-3.x,captcha,Django,Python 3.x,Captcha,我一直在从事一个基于python3的django项目。我正在尝试合并captcha。我选择了,但不幸的是,该软件包不适用于python3。所以试着为蟒蛇3量身定做。我做了一些2to3的工作,并根据需要做了一些更改。除了请求的url编码,其他一切似乎都很正常 下面的代码段生成的POST数据应该是字节或字节的可数。它不能是str类型。异常 def encode_if_necessary(s): if isinstance(s, str): return s.encode('u

我一直在从事一个基于
python3
django
项目。我正在尝试合并
captcha
。我选择了,但不幸的是,该软件包不适用于python3。所以试着为蟒蛇3量身定做。我做了一些
2to3
的工作,并根据需要做了一些更改。除了请求的
url编码
,其他一切似乎都很正常

下面的代码段生成的
POST数据应该是字节或字节的可数。它不能是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
-

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错误未知url类型:b'http
异常

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)

有人知道怎么修吗?感谢您的帮助:)。

好的,我会自己回答:p

根据上的一个示例,我将
数据
请求
中排除,并分别将
请求和数据
传递到
urlopen()
。下面是更新的代码段-

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)

尽管解决了这个问题,我仍然不知道2to3.py生成的代码为什么不起作用。根据文档,它应该是有效的。

您猜对了,您需要对数据进行编码,但不是按照您的方式进行编码

正如@Sheena在文章中所写,您需要两个步骤来编码数据:

data = urllib.parse.urlencode(values)
binary_data = data.encode('utf-8')
req = urllib.request.Request(url, binary_data)

不要重新编码url。

我也用两个步骤对数据进行了编码。你提到的代码很好用。谢谢你的反馈。啊,该死的,我刚才看到你在我回答之前30秒回答了自己!好吧,拍拍自己的背,投你一票!:)你能告诉我你为什么选择utf-8吗?接收服务器如何知道您发送了utf-8而不是拉丁语-1?我向自己的服务器发送了一个请求,所以我知道我在等待什么。我想你应该知道,通过API或给负责人的电子邮件。