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 validating emails and URLs
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 @param url: URL to validate
116 @type url: str
117 @rtype: bool
118 """
119
120 validator = URLValidator()
121 try:
122
123 validator(url)
124 return True
125 except ValidationError:
126 return False
127
128 @staticmethod
130 """
131 Validates an email
132
133 @param email: eMail to validate
134 @type email: str
135 @rtype: bool
136 """
137
138 validator = EmailValidator()
139 try:
140
141 validator(email)
142 return True
143 except ValidationError:
144 return False
145
146 @staticmethod
148 """
149 Validates if a URL is relative or absolute
150
151 @param url: URL to validate
152 @type url: str
153 @rtype: bool
154 """
155 return bool(urlparse.urlparse(url).netloc)
156