Python 3.7.2与Python 2.7.14和字符串 [英] Python 3.7.2 vs. Python 2.7.14 and strings
问题描述
我已经在Python 2.7安装和我自己的python 3.7和I'上用azure / azure-storage-python / azure-storage-common / azure / storage / common / _auth.py进行了一些测试。我想知道API服务器对request.header的
授权字段期望什么样的字符串?
I've been doing some testing with azure/azure-storage-python/azure-storage-common/azure/storage/common/_auth.py on both Python 2.7 installations and my own python 3.7 and I'm wondering what kind of string does the API server expect for the request.header's Authorization field?
有问题的代码:
def _add_authorization_header(self, request, string_to_sign):
try:
signature = _sign_string(self.account_key, string_to_sign)
auth_string = 'SharedKey ' + self.account_name + ':' + signature
request.headers['Authorization'] = auth_string
except Exception as ex:
# Wrap any error that occurred as signing error
# Doing so will clarify/locate the source of problem
raise _wrap_exception(ex, AzureSigningError)
在Python2.7中,授权字段值是Unicode值:即u<< storage account>:< code>" ;;但是在Python 3.7中,我只得到一个"< storage account>:< code>"
In Python2.7, the Authorization field value is a Unicode value : i.e. u"<storage account>:<code>"; but in Python 3.7, I just get a "<storage account>:<code>"
在Python 3.x中,字符串已经是Unicode(虽然它没有显示我在"auth_string = ..."行之后进行打印时,即
In Python 3.x, strings are already Unicode (though it doesn't show that when I do a print after the "auth_string = ..." line, i.e.
def _add_authorization_header(self, request, string_to_sign):
try:
signature = _sign_string(self.account_key, string_to_sign)
auth_string = 'SharedKey ' + self.account_name + ':' + signature
print(auth_string)
request.headers['Authorization'] = auth_string
except Exception as ex:
# Wrap any error that occurred as signing error
# Doing so will clarify/locate the source of problem
raise _wrap_exception(ex, AzureSigningError)
在2.7中,我得到u'SharedKey< accountName>:< signature>';但在3.7中,我得到'SharedKey< accountName> ;:< signature>'。
In 2.7, I get u'SharedKey <accountName>:<signature>'; but in 3.7, I get 'SharedKey <accountName>:<signature>'.
前者有效。 Th e后来给了我:
The former works. The later gives me:
Traceback (most recent call last):
File "./test2.py", line 189, in <module>
print(bcx.exists('vhds'))
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\blob\baseblobservice.py", line 1686, in exists
_dont_fail_not_exist(ex)
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\common\_error.py", line 97, in _dont_fail_not_exist
raise error
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\blob\baseblobservice.py", line 1682, in exists
self._perform_request(request, expected_errors=expected_errors)
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\common\storageclient.py", line 430, in _perform_request
raise ex
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\common\storageclient.py", line 358, in _perform_request
raise ex
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\common\storageclient.py", line 344, in _perform_request
HTTPError(response.status, response.message, response.headers, response.body))
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\common\_error.py", line 115, in _http_error_handler
raise ex
azure.common.AzureHttpError: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
<?xml version="1.0" encoding="utf-8"?>
<Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:[redacted]
Time:2019-03-06T14:57:37.5078291Z</Message></Error>
我错过了什么?
谢谢
Ed
推荐答案
这是一个非常基本的脚本:
Here's a very basic script:
acct_name = "<account name>"
acct_key = "<acct_key>" # from storage account's Access Key list (Used key1)
blck = BlockBlobService(account_name=acct_name,
account_key=acct_key)
blck.exists("testing") # it exists in the storage acct.
然后我得到同样的错误。
Then I get the same error.
相同的代码在Python 2.7works下运行。
The same code run under Python 2.7works.
这篇关于Python 3.7.2与Python 2.7.14和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!