1 import urlparse
2 import socket
3
4 from django.core.validators import URLValidator, EmailValidator
5 from django.core.exceptions import ValidationError
9 """
10 Helper functions for parsing URLs and getting network-related information
11 """
12
13 @staticmethod
15 """
16 Extracts hostname from an URL in the format
17 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
18 @param url: URL to process
19 @type url: str
20 """
21 p = urlparse.urlparse(url)
22 return p.hostname
23
24 @staticmethod
26 """
27 Extracts scheme from an URL in the format
28 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
29 @param url: URL to process
30 @type url: str
31 """
32 p = urlparse.urlparse(url)
33 return p.scheme
34
35 @staticmethod
37 """
38 Extracts path from an URL in the format
39 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
40 @param url: URL to process
41 @type url: str
42 """
43 p = urlparse.urlparse(url)
44 return p.path
45
46 @staticmethod
48 """
49 Extracts port from an URL in the format
50 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
51 @param url: URL to process
52 @type url: str
53 """
54 p = urlparse.urlparse(url)
55 return p.port
56
57 @staticmethod
59 """
60 Extracts username from an URL in the format
61 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
62 @param url: URL to process
63 @type url: str
64 """
65 p = urlparse.urlparse(url)
66 return p.username
67
68 @staticmethod
70 """
71 Extracts password from an URL in the format
72 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
73 @param url: URL to process
74 @type url: str
75 """
76 p = urlparse.urlparse(url)
77 return p.password
78
79 @staticmethod
81 """
82 Extracts params from an URL in the format
83 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
84 @param url: URL to process
85 @type url: str
86 """
87 p = urlparse.urlparse(url)
88 return p.params
89
90 @staticmethod
92 """
93 Extracts query from an URL in the format
94 <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
95 @param url: URL to process
96 @type url: str
97 """
98 p = urlparse.urlparse(url)
99 return urlparse.parse_qs(p.query, keep_blank_values=True, strict_parsing=True)
100
101 @staticmethod
103 """
104 Resolves a hostname and returns the corresponding IP address
105 @param hostname: Hostname to resolve
106 @type hostname: str
107 """
108 return socket.gethostbyname(hostname)
109
110 @staticmethod
112 """
113 Validates a URL
114 """
115
116 validator = URLValidator()
117 try:
118
119 validator(url)
120 return True
121 except ValidationError:
122 return False
123
124 @staticmethod
126 """
127 Validates an email
128 """
129
130 validator = EmailValidator()
131 try:
132
133 validator(email)
134 return True
135 except ValidationError:
136 return False
137