我有这个 php 函数,我在 python 2.7 中开发相同的函数:
//PHP
$actionSLK = 'https://test.monsite.com/script.cgi';
$storeId = 'test';
$cartId = 'test2';
$totalAmountTx = '100';
$email = 'test@monsite.com';
$SLKSecretKey = 'secret';
$dataMD5=$actionSLK . $storeId . $cartId . $totalAmountTx . $email . $SLKSecretKey
$checksum=MD5(utf8entities(rawurlencode($dataMD5)));
#PYTHON:
from hashlib import md5
import urllib
actionSLK = 'https://test.monsite.com/script.cgi'
storeId = 'test'
cartId = 'test2'
totalAmountTx = '100'
email = 'test@monsite.com'
SLKSecretKey = 'secret'
dataMD5 = actionSLK + storeId + cartId + totalAmountTx + email + SLKSecretKey
checksum = md5(urllib.quote(dataMD5).encode('utf8')).hexdigest()
我发现的问题是计算的校验和不一样MD5
,然后我检查了编码的url(生成的一个:)'https://test.monsite.com/script.cgitesttest100test@monsite.comsecret'
,我们在这里:
//PHP
$checksum=MD5('https%3A%2F%2Ftest.monsite.com%2Fscript.cgitesttest100test%40monsite.comsecret');
#PYTHON
checksum = md5('https%3A//test.monsite.com/script.cgitesttest100test%40monsite.comsecret').hexdigest()
所以斜杠没有被编码,所以在生成不同的校验和时会发生错误。
urllib 中是否还有其他函数可以详细编码像这样的 url?