我有這個PHP函數,我是在Python 2.7版深化發展同前面編碼網址:如何使用的urllib
//PHP
$actionSLK = 'https://test.monsite.com/script.cgi';
$storeId = 'test';
$cartId = 'test2';
$totalAmountTx = '100';
$email = '[email protected]';
$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 = '[email protected]'
SLKSecretKey = 'secret'
dataMD5 = actionSLK + storeId + cartId + totalAmountTx + email + SLKSecretKey
checksum = md5(urllib.quote(dataMD5).encode('utf8')).hexdigest()
,我發現是計算校驗和是不一樣的MD5
的問題,然後我檢查了編碼的URL(生成一個:'https://test.monsite.com/[email protected]'
),我們在這裏:
//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?
如果輸入數據包含空格,則不正確:'rawurlencode()'產生'%20',而不是'+'; [使用'報價(安全='')'代替](http://stackoverflow.com/a/33174922/4279) – jfs
是的,你是對的我只是想過他的例子,我應該刪除我的答案? – DreadfulWeather