python2.7与3.7脚本转换_Python 3.7.2与Python 2.7.14和字符串

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?

The code in question:

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)

In Python2.7, the Authorization field value is a Unicode value : i.e. u":"; but in Python 3.7, I just get a ":"

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)

In 2.7, I get u'SharedKey :'; but in 3.7, I get 'SharedKey :'.

The former works.  The later gives me:

Traceback (most recent call last):

File "./test2.py", line 189, in

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.

AuthenticationFailedServer 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

Am I missing something?

Thanks

Ed

解决方案Hi,

Here's a very basic script:

acct_name = ""

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.

The same code run under Python 2.7works.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值