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

我有一个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.'}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值