_url.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. """
  2. websocket - WebSocket client library for Python
  3. Copyright (C) 2010 Hiroki Ohtani(liris)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1335 USA
  16. """
  17. import os
  18. import socket
  19. import struct
  20. from six.moves.urllib.parse import urlparse
  21. __all__ = ["parse_url", "get_proxy_info"]
  22. def parse_url(url):
  23. """
  24. parse url and the result is tuple of
  25. (hostname, port, resource path and the flag of secure mode)
  26. url: url string.
  27. """
  28. if ":" not in url:
  29. raise ValueError("url is invalid")
  30. scheme, url = url.split(":", 1)
  31. parsed = urlparse(url, scheme="ws")
  32. if parsed.hostname:
  33. hostname = parsed.hostname
  34. else:
  35. raise ValueError("hostname is invalid")
  36. port = 0
  37. if parsed.port:
  38. port = parsed.port
  39. is_secure = False
  40. if scheme == "ws":
  41. if not port:
  42. port = 80
  43. elif scheme == "wss":
  44. is_secure = True
  45. if not port:
  46. port = 443
  47. else:
  48. raise ValueError("scheme %s is invalid" % scheme)
  49. if parsed.path:
  50. resource = parsed.path
  51. else:
  52. resource = "/"
  53. if parsed.query:
  54. resource += "?" + parsed.query
  55. return hostname, port, resource, is_secure
  56. DEFAULT_NO_PROXY_HOST = ["localhost", "127.0.0.1"]
  57. def _is_ip_address(addr):
  58. try:
  59. socket.inet_aton(addr)
  60. except socket.error:
  61. return False
  62. else:
  63. return True
  64. def _is_subnet_address(hostname):
  65. try:
  66. addr, netmask = hostname.split("/")
  67. return _is_ip_address(addr) and 0 <= int(netmask) < 32
  68. except ValueError:
  69. return False
  70. def _is_address_in_network(ip, net):
  71. ipaddr = struct.unpack('I', socket.inet_aton(ip))[0]
  72. netaddr, bits = net.split('/')
  73. netmask = struct.unpack('I', socket.inet_aton(netaddr))[0] & ((2 << int(bits) - 1) - 1)
  74. return ipaddr & netmask == netmask
  75. def _is_no_proxy_host(hostname, no_proxy):
  76. if not no_proxy:
  77. v = os.environ.get("no_proxy", "").replace(" ", "")
  78. no_proxy = v.split(",")
  79. if not no_proxy:
  80. no_proxy = DEFAULT_NO_PROXY_HOST
  81. if hostname in no_proxy:
  82. return True
  83. elif _is_ip_address(hostname):
  84. return any([_is_address_in_network(hostname, subnet) for subnet in no_proxy if _is_subnet_address(subnet)])
  85. return False
  86. def get_proxy_info(
  87. hostname, is_secure, proxy_host=None, proxy_port=0, proxy_auth=None,
  88. no_proxy=None, proxy_type='http'):
  89. """
  90. try to retrieve proxy host and port from environment
  91. if not provided in options.
  92. result is (proxy_host, proxy_port, proxy_auth).
  93. proxy_auth is tuple of username and password
  94. of proxy authentication information.
  95. hostname: websocket server name.
  96. is_secure: is the connection secure? (wss)
  97. looks for "https_proxy" in env
  98. before falling back to "http_proxy"
  99. options: "http_proxy_host" - http proxy host name.
  100. "http_proxy_port" - http proxy port.
  101. "http_no_proxy" - host names, which doesn't use proxy.
  102. "http_proxy_auth" - http proxy auth information.
  103. tuple of username and password.
  104. default is None
  105. "proxy_type" - if set to "socks5" PySocks wrapper
  106. will be used in place of a http proxy.
  107. default is "http"
  108. """
  109. if _is_no_proxy_host(hostname, no_proxy):
  110. return None, 0, None
  111. if proxy_host:
  112. port = proxy_port
  113. auth = proxy_auth
  114. return proxy_host, port, auth
  115. env_keys = ["http_proxy"]
  116. if is_secure:
  117. env_keys.insert(0, "https_proxy")
  118. for key in env_keys:
  119. value = os.environ.get(key, None)
  120. if value:
  121. proxy = urlparse(value)
  122. auth = (proxy.username, proxy.password) if proxy.username else None
  123. return proxy.hostname, proxy.port, auth
  124. return None, 0, None