如果内容包含扩展ASCII字符,base64.urlsafe_b64encode不正确

2024-09-30 22:11:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python(2.7)客户机,可以与phpapi通信。Python客户机获取一些UTF-8编码的XML,将其转换为JSON,然后将其发送到API。还有其他的东西,但是为了简洁起见,我们可以这么说。在

我发现,如果XML中的任何字符串包含扩展的ASCII字符,如“,”,'(排版师的引号)、•(项目符号),以及我能想到的任何其他字符,客户机都无法正确地将数据发送到API。如果XML数据不包含扩展字符,它会将数据发布到API,但是它确实包含扩展字符,那么POST数据是空的。在

我知道这是一个编码问题,但我只能确定这一点json.dumps文件并没有把dict中的所有unicode值弄乱。在

如果XML具有HTML实体的扩展字符,则会出现相同的问题。在

我知道当数据通过base64.urlsafe_b64encode传递时就会发生这种情况。我知道我错过了一些东西,但我不确定我错在哪里。在

以下是我所要做的简化:

import urllib3
import base64
import hmac
import json
import hashlib

def signString(string_to_sign, shared_secret):
        return hmac.new(shared_secret, string_to_sign, hashlib.sha512).hexdigest()

def send_to_api(action, payload):

    SHARED_SECRET = 'my_secret'

    json_payload = json.dumps(payload, ensure_ascii=False).strip(' \t\n\r')
    json_payload = json_payload.encode('utf-8')

    signature = signString(json_payload, SHARED_SECRET)
    encoded_signature = base64.urlsafe_b64encode(signature.strip(' \t\n\r'))
    encoded_payload = base64.urlsafe_b64encode(json_payload)

    #post = 'v=1.4.8&data={}'.format(encoded_payload)
    headers = { 'Action' : action, 'Signature': encoded_signature }
    pool = urllib3.connectionpool.HTTPSConnectionPool("mysite.com", maxsize=1, block=True, headers=headers, retries=10)
    request = pool.request('POST', '/api/api.v7.php', fields={'v': '1.4.6', 'data': encoded_payload})
    data = request.read()
    request.close()

##etc

你知道我会错过什么吗?在

编辑

下面是我通过payload参数传递给send_to_api的源字典:

^{pr2}$

这是它在运行后的样子json.dumps文件,当它变成字符串时:

{"description": "Before joining together as Fake Tears, Larissa Loyva and Elisha May Rembold were already established singers and songwriters in their own right. The former released two spectral albums under the name Kellarissa, toured the world as a live member of Destroyer and How to Dress Well, and played in past Mint Records acts P:ano and The Choir Practice; the latter leads the folk-rock band the Lost Lovers Brigade and is a member of Shimmering Stars. The pair first collaborated when Loyva joined the lineup of Lost Lovers, and honed their chemistry during a stint as backup vocalists in the funky local ensemble COOL TV.\nTheir current project dates back to 2012, when Loyva and Rembold got together as part of a larger ensemble. \u201cI thought I\u2019d make an all-woman supergroup,\u201d Loyva remembers. \u201cWe started with five, and then, after a couple of months and a couple of practices, we were down to two."}

今天早上我注意到的一件事是,如果在发送dict到send_to_api()函数之前打印dict,它的unicode字符串是lau'my unicode string',但是如果我在send_to_api()函数中打印有效负载参数,它就不再是unicode了,就像上面的源字典:

{'description': 'The pair first collaborated when Loyva joined the lineup of Lost Lovers, and honed their chemistry during a stint as backup vocalists in the funky local ensemble COOL TV.\nTheir current project dates back to 2012, when Loyva and Rembold got together as part of a larger ensemble. \xe2\x80\x9cI thought I\xe2\x80\x99d make an all-woman supergroup,\xe2\x80\x9d Loyva remembers. \xe2\x80\x9cWe started with five, and then, after a couple of months and a couple of practices, we were down to two.'}

Tags: andoftheto数据importapijson