gpt4 book ai didi

sql-server - 在 Python 中,如何构造 URL 来测试我的 SQL Server 实例是否正在运行?

转载 作者:行者123 更新时间:2023-12-03 23:45:34 24 4
gpt4 key购买 nike

我将 Python 3.8 与 pytest-docker-compose 插件一起使用 -- https://pypi.org/project/pytest-docker-compose/ .有谁知道如何编写一个最终会告诉我 SQL Server 是否正在运行的 URL?
我有这个 docker-compose.yml 文件

version: "3.2"
services:

sql-server-db:
build: ./
container_name: sql-server-db
image: microsoft/mssql-server-linux:2017-latest
ports:
- "1433:1433"
environment:
SA_PASSWORD: "password"
ACCEPT_EULA: "Y"
但我不知道将什么 URL 传递给我的 Retry 对象以测试服务器是否正在运行。这失败了...
import pytest
import requests
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
...

@pytest.fixture(scope="function")
def wait_for_api(function_scoped_container_getter):
"""Wait for sql server to become responsive"""
request_session = requests.Session()
retries = Retry(total=5,
backoff_factor=0.1,
status_forcelist=[500, 502, 503, 504])
request_session.mount('http://', HTTPAdapter(max_retries=retries))

service = function_scoped_container_getter.get("sql-server-db").network_info[0]
api_url = "http://%s:%s/" % (service.hostname, service.host_port)
assert request_session.get(api_url)
return request_session, api_url
除了这个异常(exception)
raise ConnectionError(e, request=request)
E requests.exceptions.ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=1433): Max retries exceeded with url: / (Caused by ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')))

最佳答案

if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
你可以使用这样的东西,如果它被连接它就会输出

关于sql-server - 在 Python 中,如何构造 URL 来测试我的 SQL Server 实例是否正在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63000658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com