1

我有这个 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?

4

3 回答 3

3

urllib.quote()通常用于对包括路径在内的 url 部分进行编码,因此默认情况下/被认为是安全字符。显式传递safe=''

>>> dataMD5
'https://test.monsite.com/script.cgitesttest2100test@monsite.comsecret'
>>> import urllib
>>> urllib.quote(dataMD5)
'https%3A//test.monsite.com/script.cgitesttest2100test%40monsite.comsecret'
>>> urllib.quote(dataMD5, safe='')
'https%3A%2F%2Ftest.monsite.com%2Fscript.cgitesttest2100test%40monsite.comsecret'

quote_plus()通常用于创建application/x-www-form-urlencoded数据,因此safe=''默认情况下。

要确定是否应该使用quote_plus()or quote(),请考虑带有空格的数据:

>>> urllib.quote_plus('/ /')
'%2F+%2F'
>>> urllib.quote('/ /', safe='')
'%2F%20%2F'

PHPrawurlencode()产生后者,因此您应该使用quote(safe='')而不是quote_plus().

于 2015-10-16T16:06:40.493 回答
2

您可以使用urllib.quote_plus()

>>> encoded = urllib.quote_plus("https://test.monsite.com/script.cgitesttest100test@monsite.comsecret")
>>> encoded
'https%3A%2F%2Ftest.monsite.com%2Fscript.cgitesttest100test%40monsite.comsecret'
于 2015-10-16T15:40:01.127 回答
2

使用urllib.quote_plus你可以实现它

actionSLK = "https://test.monsite.com/script.cgi"
urllib.quote_plus(actionSLK)
>>https%3A%2F%2Ftest.monsite.com%2Fscript.cgi
于 2015-10-16T15:40:03.830 回答