Python通过命令参数传递IP列表:端口[重复]
Posted
技术标签:
【中文标题】Python通过命令参数传递IP列表:端口[重复]【英文标题】:Python passing a list of IPs:port via a command argument [duplicate] 【发布时间】: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
【问题讨论】:
【参考方案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))
把验证设为真
【讨论】:
以上是关于Python通过命令参数传递IP列表:端口[重复]的主要内容,如果未能解决你的问题,请参考以下文章
python argh/argparse:如何将列表作为命令行参数传递?