【问题标题】:Python passing a list of IPs:port via a command argument [duplicate]Python通过命令参数传递IP列表:端口[重复]
【发布时间】:2018-09-05 22:41:12
【问题描述】:

我正在尝试通过一个名为 HOSTS 的变量的命令参数解析 IP:ports 列表,以在函数中使用 - read_head_block_ints。

parser = argparse.ArgumentParser(description='chain info checker')
parser.add_argument('host', help='host address')
parser.add_argument('function', help='check to run', choices=ALLOWED_FUNCTIONS)
parser.add_argument('--hosts', dest='HOSTS', type=list)

def read_head_block_ints():
    for host in HOSTS:
        try:
            data = requests.get(URLS['get_info'].format(host=host), verify=False).json()  # returns data in JSON format
            HEAD_BLOCK_INTS.append(float(data['head_block_num']))   # this is the head_block INT
        except:
            print('Error cannot connect to host {host}'.format(host=host))

当我使用--hosts 1.2.3.4:888 5.6.7.8:8888 9.9.9.9:8888 调用脚本时

我收到以下error: unrecognized arguments: 5.6.7.8:8888 9.9.9.9:8888

【问题讨论】:

    标签: python


    【解决方案1】:
    parser = argparse.ArgumentParser(description='chain info checker')
    parser.add_argument('host', help='host address')
    parser.add_argument('function', help='check to run', choices=ALLOWED_FUNCTIONS)
    parser.add_argument('--hosts', dest='HOSTS', type=list)
    
    def read_head_block_ints():
        for host in HOSTS:
            try:
                data = requests.get(URLS['get_info'].format(host=host), verify=True).json()  # returns data in JSON format
                HEAD_BLOCK_INTS.append(float(data['head_block_num']))   # this is the head_block INT 
            except:
                print('Error cannot connect to host {host}'.format(host=host))
    

    把验证设为真

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 2016-09-05
      • 2018-03-03
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      相关资源
      最近更新 更多