图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
928 B

  1. import urllib.parse
  2. def collect_hosts(hosts):
  3. """
  4. Collect a set of hosts and an optional chroot from
  5. a string or a list of strings.
  6. """
  7. if isinstance(hosts, list):
  8. if hosts[-1].strip().startswith("/"):
  9. host_ports, chroot = hosts[:-1], hosts[-1]
  10. else:
  11. host_ports, chroot = hosts, None
  12. else:
  13. host_ports, chroot = hosts.partition("/")[::2]
  14. host_ports = host_ports.split(",")
  15. chroot = "/" + chroot if chroot else None
  16. result = []
  17. for host_port in host_ports:
  18. # put all complexity of dealing with
  19. # IPv4 & IPv6 address:port on the urlsplit
  20. res = urllib.parse.urlsplit("xxx://" + host_port)
  21. host = res.hostname
  22. if host is None:
  23. raise ValueError("bad hostname")
  24. port = int(res.port) if res.port else 2181
  25. result.append((host.strip(), port))
  26. return result, chroot