【问题标题】:Python ArgumentParser to Influxdb-ClientPython ArgumentParser 到 Influxdb-Client
【发布时间】:2021-05-10 07:51:57
【问题描述】:

我有一个将测试数据发送到 Influxdb 实例的测试脚本。为此,我正在利用 InfluxDBClient 来促进这一点。为了让这个脚本在我的本地机器和我的虚拟机上可用,我设置了参数。

参数脚本如下所示:

import argparse

parser = argparse.ArgumentParser(description='Setup for Automated Test')
parser.add_argument("--grafana_ip", dest="grafana_ip", action="store", default="localhost", help="The IP of the Grafana instance")
parser.add_argument("--grafana_port", dest="grafana_port", action="store", default="8086", help="The PORT of the Grafana instance")

args, unknown = parser.parse_known_args()

测试脚本设置如下:

from argument_onstream import args

client = InfluxDBClient(host=args.grafana_ip, port=args.grafana_port, database='TEST')

*do test*

我遇到的问题是,当我从命令行运行测试脚本时,如下所示:

python test.py --grafana_ip="10.10.10.10" --grafana_port="8086"

我收到以下错误:

E           requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8086): Max retries exceeded with url: /write?db=TEST (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd2011b42b0>: Failed to establish a new connection: [Errno 111] Connection refused'))

但是,当我在测试脚本中运行一个简单的打印时,我得到了正确的输出:

10.10.10.10 8086

有人知道可能出了什么问题吗?

【问题讨论】:

  • 使用print(args)查看解析器做了什么。如果您喜欢这些值,那么这不是argparse 问题。
  • @hpaulj 感谢您的意见。我确实在问题中声明运行简单的打印会返回正确的值。所以,我同意你的观点,这并不是一个 argparse 问题。仍然不确定故障在哪里。

标签: python selenium command-line-arguments argparse influxdb-python


【解决方案1】:

为了解决这个问题,我创建了一个 conftest.py 文件,而不是使用 argparse。

问题是因为我正在运行一个名为 test.py 的启动脚本 (main.py),并且在 test.py 中我从 main.py 导入了变量。当 test.py 导入 main.py 时,在 CL 中设置的 argparse 变量被恢复为默认值。

下面的例子 argparse.py

import argparse

parser = argparse.ArgumentParser(description='Setup for Automated Test')
parser.add_argument("--grafana_ip", dest="grafana_ip", action="store", default="localhost", help="The IP of the Grafana instance")
parser.add_argument("--grafana_port", dest="grafana_port", action="store", default="8086", help="The PORT of the Grafana instance")

args, unknown = parser.parse_known_args()

main.py

from argparse import args

*setup for test*

test.py

from main.py import stuff
from argparse import args

*do test*

我认为这是一个循环导入问题,以及编写不佳的代码。

为了纠正这个问题,我删除了 main.py 和 argparse.py 文件。添加了 conftest.py 文件,并允许 conftest.py 将变量传递给 test.py。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多