/python-packages/fle_utils/internet/functions.py

https://gitlab.com/gregtyka/ka-lite · Python · 110 lines · 84 code · 14 blank · 12 comment · 14 complexity · e79d4a222ab10749fe7660890753ed3d MD5 · raw file

  1. """
  2. For functions mucking with internet access
  3. """
  4. import ifcfg
  5. import os
  6. import platform
  7. import re
  8. import requests
  9. import socket
  10. from urlparse import parse_qs, urlsplit, urlunsplit
  11. from urllib import urlencode
  12. def am_i_online(url, expected_val=None, search_string=None, timeout=5, allow_redirects=True):
  13. """Test whether we are online or not.
  14. returns True or False.
  15. Eats all exceptions!
  16. """
  17. assert not (search_string and expected_val is not None), "Search string and expected value cannot both be set"
  18. try:
  19. if not search_string and expected_val is None:
  20. response = requests.head(url)
  21. else:
  22. response = requests.get(url, timeout=timeout, allow_redirects=allow_redirects)
  23. # Validate that response came from the requested url
  24. if response.status_code != 200:
  25. return False
  26. elif not allow_redirects and response.url != url:
  27. return False
  28. # Check the output, if expected values are specified
  29. if expected_val is not None:
  30. return expected_val == response.text
  31. elif search_string:
  32. return search_string in response.text
  33. return True
  34. except Exception as e:
  35. return False
  36. def generate_all_paths(path, base_path="/"):
  37. if not base_path.endswith("/"): # Must have trailing slash to work.
  38. base_path += "/"
  39. if path.endswith("/"): # Must NOT have trailing slash to work.
  40. path = path[0:-1]
  41. all_paths = []
  42. cur_path = base_path[0:-1]
  43. for dirname in path[len(base_path)-1:].split("/"): # start AFTER the base path
  44. cur_path += dirname + "/"
  45. all_paths.append(cur_path)
  46. return all_paths
  47. def set_query_params(url, param_dict):
  48. """Given a URL, set or replace a query parameter and return the
  49. modified URL.
  50. >>> set_query_params('http://example.com?foo=bar&biz=baz', {'foo': 'stuff'})
  51. 'http://example.com?foo=stuff&biz=baz'
  52. modified from http://stackoverflow.com/questions/4293460/how-to-add-custom-parameters-to-an-url-query-string-with-python
  53. """
  54. scheme, netloc, path, query_string, fragment = urlsplit(url)
  55. query_params = parse_qs(query_string, keep_blank_values=True)
  56. for param_name, param_value in param_dict.items():
  57. if param_value is None:
  58. del query_params[param_name]
  59. else:
  60. query_params[param_name] = [param_value]
  61. new_query_string = urlencode(query_params, doseq=True)
  62. return urlunsplit((scheme, netloc, path, new_query_string, fragment))
  63. def get_ip_addresses(include_loopback=True):
  64. """Get a list of all the IP addresses for adapters on the local system.
  65. You can specify to either include the loopback device (127.0.0.1) or not.
  66. """
  67. system = platform.system()
  68. if system.lower() in ["linux", "darwin", "macosx"]:
  69. # on Linux and OSX, use the ifcfg library to wrap ifconfig
  70. ips = [iface.get("inet") for iface in ifcfg.interfaces().values()]
  71. elif system.lower() == "windows":
  72. # on Windows, run ipconfig and parse the output
  73. ipconfig = os.popen("ipconfig /all").read()
  74. ips = [match[1] for match in re.findall("IP(v4)? Address[\.\: ]+([\d\.]+)", ipconfig)]
  75. else:
  76. ips = []
  77. # remove empty values for adapters without an IP
  78. ips = set(ips) - set([None, ""])
  79. if include_loopback:
  80. ips = ips.union(["127.0.0.1"])
  81. else:
  82. ips = ips - set(["127.0.0.1"])
  83. return list(ips)