Skip to main content
 首页 » 编程设计

php之如何使用 urllib 对 url 进行编码

2025年05月04日22thcjp

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

请您参考如下方法:

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() 还是 quote(),请考虑带空格的数据:

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

PHP 的 rawurlencode() 生成后者,因此您应该使用 quote(safe='') 而不是 quote_plus()。