如何连接到CosmosDB Emulator';使用python的表存储?

如何连接到CosmosDB Emulator';使用python的表存储?,python,ssl-certificate,azure-cosmosdb,azure-cosmosdb-tables,Python,Ssl Certificate,Azure Cosmosdb,Azure Cosmosdb Tables,我正在本地运行Azure CosmosDB模拟器,我希望使用Python中的表接口。Microsoft提供azure cosmosdb表python sdk,用于连接表API[1]。我无法成功连接到模拟器,即使我能够连接到云中的实际CosmosDB帐户 版本号等: 视窗10 Python 3.6(CPython) azure cosmosdb表1.0.5 我用于尝试访问仿真器的代码: 导入日志 从azure.cosmosdb.table导入表服务 从nossl导入无ssl验证 logging

我正在本地运行Azure CosmosDB模拟器,我希望使用Python中的表接口。Microsoft提供azure cosmosdb表python sdk,用于连接表API[1]。我无法成功连接到模拟器,即使我能够连接到云中的实际CosmosDB帐户

版本号等:

  • 视窗10
  • Python 3.6(CPython)
  • azure cosmosdb表1.0.5
我用于尝试访问仿真器的代码:

导入日志
从azure.cosmosdb.table导入表服务
从nossl导入无ssl验证
logging.basicConfig(级别=logging.DEBUG)
连接={
“AccountName”:“devstoreaccount1”,
#“AccountEndpoint”:”https://localhost:8081/',
“AccountKey”:“C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2NQ9NDUVTQOBD4B8MGGYMBIZNQYMSECAGQY67XIW/Jw=”,
#'EndpointSuffix':'localhost:8081',
#'DefaultEndpointsProtocol':'https',
“TableEndpoint”:”https://localhost:8081',
}
connection_string=';'.join('{k}={v}'。connection.items()中k,v的格式(k=k,v=v)
打印(连接字符串)
没有ssl验证()时:
ts=表服务(
端点_后缀=”https://localhost:8081",
连接字符串=连接字符串,
_仿真=真)
ts.create_表(“mytable”)
no_ssl_-verification
上下文管理器禁用SDK使用的请求库中的所有ssl验证。它是从堆栈溢出[5]上此处的答案复制的

上面的示例代码产生以下错误:

AccountName=devstoreaccount1;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";TableEndpoint=https://localhost:8081
DEBUG:azure.cosmosdb.table._auth:String_to_sign=POST

application/json
Thu, 18 Apr 2019 13:50:42 GMT
/devstoreaccount1/Tables
INFO:azure.storage.common.storageclient:Client-Request-ID=f52a8b36-61e0-11e9-8fc0-a44cc8cee022 Outgoing request: Method=POST, Path=/Tables, Query={'timeout': None}, Headers={'Content-Type': 'application/json', 'Prefer': 'return-no-content', 'Accept': 'application/json;odata=minimalmetadata', 'DataServiceVersion': '3.0;NetFx', 'MaxDataServiceVersion': '3.0', 'Content-Length': '24', 'x-ms-version': '2018-03-28', 'User-Agent': 'Azure-Storage/1.4.0-None (Python CPython 3.6.4;
Windows 10)', 'x-ms-client-request-id': 'f52a8b36-61e0-11e9-8fc0-a44cc8cee022', 'x-ms-date': 'Thu, 18 Apr 2019 13:50:42 GMT', 'Authorization': 'REDACTED'}.
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): localhost:8081
DEBUG:urllib3.connectionpool:https://localhost:8081 "POST /Tables HTTP/1.1" 400 None
INFO:azure.storage.common.storageclient:Client-Request-ID=f52a8b36-61e0-11e9-8fc0-a44cc8cee022 Receiving Response: Server-Timestamp=Thu, 18 Apr 2019 13:50:42 GMT, HTTP Status Code=400, Message=Bad Request, Headers={'transfer-encoding': 'chunked', 'content-type': 'application/json', 'server': 'Microsoft-HTTPAPI/2.0',
'x-ms-activity-id': '00000000-0000-0000-0000-000000000000', 'x-ms-gatewayversion': 'version=2.2.0.0', 'date': 'Thu, 18 Apr 2019 13:50:42 GMT'}.
INFO:azure.storage.common.storageclient:Client-Request-ID=f52a8b36-61e0-11e9-8fc0-a44cc8cee022 Operation failed: checking if the operation should be retried. Current retry count=0, Server-Timestamp=Thu, 18 Apr 2019 13:50:42 GMT, HTTP status code=400, Exception=Bad Request{"code":"BadRequest","message":"Request url is invalid., Microsoft.Azure.Documents.Common/2.2.0.0"}.
ERROR:azure.storage.common.storageclient:Client-Request-ID=f52a8b36-61e0-11e9-8fc0-a44cc8cee022 Retry policy did not allow for a retry: Server-Timestamp=Thu, 18 Apr 2019 13:50:42 GMT, HTTP status code=400, Exception=Bad Request{"code":"BadRequest","message":"Request url is invalid., Microsoft.Azure.Documents.Common/2.2.0.0"}.
Traceback (most recent call last):
  File "c:/Users/josteb/sandbox/cosmoskvstore/examples.py", line 24, in <module>
    ts.create_table('mytable')
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\cosmosdb\table\tableservice.py", line 539, in create_table
    _dont_fail_on_exist(ex)
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\storage\common\_error.py", line 88, in _dont_fail_on_exist
    raise error
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\cosmosdb\table\tableservice.py", line 536, in create_table
    self._perform_request(request)
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\cosmosdb\table\tableservice.py", line 1106, in _perform_request
    return super(TableService, self)._perform_request(request, parser, parser_args, operation_context)
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\storage\common\storageclient.py", line 430, in _perform_request
    raise ex
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\storage\common\storageclient.py", line 358, in _perform_request
    raise ex
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\storage\common\storageclient.py", line 344, in _perform_request
    HTTPError(response.status, response.message, response.headers, response.body))
  File "C:\Users\josteb\.virtualenvs\cosmoskvstore-cPSP36p_\lib\site-packages\azure\storage\common\_error.py", line 115, in _http_error_handler
    raise ex
azure.common.AzureHttpError: Bad Request
{"code":"BadRequest","message":"Request url is invalid., Microsoft.Azure.Documents.Common/2.2.0.0"}
这项工作顺利完成,我可以在Emulator的data explorer中看到数据库已经创建

是否有人对CosmosDB emulator的表接口的正确连接字符串有引用

[1]

[2]

[3]

[4]

[5]

在PythonSDK for表中禁用证书验证的一种方法 储藏

在深入调试
create_table
函数之后,我发现您可以在requests包的session.py中使用
verify
属性

默认值为True,可以将其设置为False以跳过SSL验证

验证证书的一种方法

另一个名为cert的属性,可以配置为cert路径

此外,请参考本文件和本文件中的建议


谢谢。不幸的是,编辑请求源代码对我来说不是一个选项。但是,正如本文所述,猴子补丁解决了SSL问题,现在问题是找到正确的连接字符串。我已经更新了问题。您好,我可能会迟到,但我正在使用模拟器,不需要指定任何主机或其他内容。我知道st需要做
表服务(account\u name='devstoreaccount1',account\u key='the\u dev\u key',is\u emulated=True)
。顺便说一句,即使在azure上使用生产帐户,我也不需要指定任何主机。我与您的唯一区别是我正在运行Linux。