Package tlib :: Package base :: Module NetworkHelper
[hide private]
[frames] | no frames]

Source Code for Module tlib.base.NetworkHelper

  1  import urlparse 
  2  import socket 
3 4 -class NetworkHelper(object):
5 """ 6 Helper functions for parsing URLs and getting network-related information 7 """ 8 9 @staticmethod
10 - def get_hostname_from_url(url):
11 """ 12 Extracts hostname from an URL in the format 13 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment> 14 @param url: URL to process 15 @type url: str 16 """ 17 p = urlparse.urlparse(url) 18 return p.hostname
19 20 @staticmethod
21 - def get_scheme_from_url(url):
22 """ 23 Extracts scheme from an URL in the format 24 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment> 25 @param url: URL to process 26 @type url: str 27 """ 28 p = urlparse.urlparse(url) 29 return p.scheme
30 31 @staticmethod
32 - def get_path_from_url(url):
33 """ 34 Extracts path from an URL in the format 35 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment> 36 @param url: URL to process 37 @type url: str 38 """ 39 p = urlparse.urlparse(url) 40 return p.path
41 42 @staticmethod
43 - def get_port_from_url(url):
44 """ 45 Extracts port from an URL in the format 46 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment> 47 @param url: URL to process 48 @type url: str 49 """ 50 p = urlparse.urlparse(url) 51 return p.port
52 53 @staticmethod
54 - def get_username_from_url(url):
55 """ 56 Extracts username from an URL in the format 57 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment> 58 @param url: URL to process 59 @type url: str 60 """ 61 p = urlparse.urlparse(url) 62 return p.username
63 64 @staticmethod
65 - def get_password_from_url(url):
66 """ 67 Extracts password from an URL in the format 68 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment> 69 @param url: URL to process 70 @type url: str 71 """ 72 p = urlparse.urlparse(url) 73 return p.password
74 75 @staticmethod
76 - def get_params_from_url(url):
77 """ 78 Extracts params from an URL in the format 79 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment> 80 @param url: URL to process 81 @type url: str 82 """ 83 p = urlparse.urlparse(url) 84 return p.params
85 86 @staticmethod
88 """ 89 Extracts query from an URL in the format 90 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment> 91 @param url: URL to process 92 @type url: str 93 """ 94 p = urlparse.urlparse(url) 95 return urlparse.parse_qs(p.query, keep_blank_values=True, strict_parsing=True)
96 97 @staticmethod
98 - def get_ip_from_hostname(hostname):
99 """ 100 Resolves a hostname and returns the corresponding IP address 101 @param hostname: Hostname to resolve 102 @type hostname: str 103 """ 104 return socket.gethostbyname(hostname)
105