我的代碼是從單點登錄功能,我給它一個字符串,而不是字節python 3打印字符串,並說字符串的類型是str?
from urllib.parse import unquote
import base64
payload = unquote(payload)
print(payload)
print(type(payload))
decoded = base64.decodestring(payload)
decodestring抱怨......
File "/Users/Jeff/Development/langalang/proj/discourse/views.py", line 38, in sso decoded = base64.decodestring(payload) File "/Users/Jeff/.virtualenvs/proj/lib/python3.6/base64.py", line 559, in decodestring return decodebytes(s) File "/Users/Jeff/.virtualenvs/proj/lib/python3.6/base64.py", line 551, in decodebytes _input_type_check(s) File "/Users/Jeff/.virtualenvs/proj/lib/python3.6/base64.py", line 520, in _input_type_check raise TypeError(msg) from err TypeError: expected bytes-like object, not str
這是罰款,但當我看看我的打印報表打印到終端上,我看到這...
b'bm9uY2U9NDI5NDg5OTU0NjU4MjAzODkyNTI=\n'
<class 'str'>
它似乎是說它是一串字節,但它表示它是一個字符串。
這是怎麼回事?
如果我添加一個encode()
有效載荷聲明我看到這個月底...
payload = unquote(payload).encode()
b"b'bm9uY2U9NDQxMTQ4MzIyNDMwNjU3MjcyMDM=\\n'"
<class 'bytes'>
編輯:添加,使有效載荷的方法
@patch("discourse.views.HttpResponseRedirect")
def test_sso_success(self, mock_redirect):
"""Test for the sso view"""
# Generating a random number, encoding for url, signing it with a hash
nonce = "".join([str(random.randint(0, 9)) for i in range(20)])
# The sso payload needs to be a dict of params
params = {"nonce": nonce}
payload = base64.encodestring(urlencode(params).encode())
print(payload.decode() + " tests")
key = settings.SSO_SECRET
h = hmac.new(key.encode(), payload, digestmod=hashlib.sha256)
signature = h.hexdigest()
url = reverse("discourse:sso") + "?sso=%s&sig=%s" % (payload, signature)
req = self.rf.get(url)
req.user = self.user
response = sso(req)
self.assertTrue(mock_redirect.called)
確切地說'有效載荷'是由_originally_定義的? –
什麼是「有效載荷」?你怎麼得到的?創建簡單的工作示例。 – furas
@leaf有效載荷創建功能添加到帖子。它是在django unittest中創建的 – deltaskelta