7 "validipaddr", "validipport", "validip", "validaddr",
9 "httpdate", "parsehttpdate",
10 "htmlquote", "htmlunquote", "websafe",
15 except ImportError: pass
17 def validipaddr(address):
19 Returns True if `address` is a valid IPv4 address.
21 >>> validipaddr('192.168.1.1')
23 >>> validipaddr('192.168.1.800')
25 >>> validipaddr('192.168.1')
29 octets = address.split('.')
33 if not (0 <= int(x) <= 255):
39 def validipport(port):
41 Returns True if `port` is a valid IPv4 port.
43 >>> validipport('9000')
45 >>> validipport('foo')
47 >>> validipport('1000000')
51 if not (0 <= int(port) <= 65535):
57 def validip(ip, defaultaddr="0.0.0.0", defaultport=8080):
58 """Returns `(ip_address, port)` from string `ip_addr_port`"""
66 elif validipaddr(ip[0]):
68 elif validipport(ip[0]):
71 raise ValueError, ':'.join(ip) + ' is not a valid IP address/port'
74 if not validipaddr(addr) and validipport(port):
75 raise ValueError, ':'.join(ip) + ' is not a valid IP address/port'
78 raise ValueError, ':'.join(ip) + ' is not a valid IP address/port'
81 def validaddr(string_):
83 Returns either (ip_address, port) or "/path/to/socket" from string_
85 >>> validaddr('/path/to/socket')
89 >>> validaddr('127.0.0.1')
91 >>> validaddr('127.0.0.1:8000')
94 Traceback (most recent call last):
96 ValueError: fff is not a valid IP address/port
101 return validip(string_)
105 Quotes a string for use in a URL.
107 >>> urlquote('://?f=1&j=1')
108 '%3A//%3Ff%3D1%26j%3D1'
111 >>> urlquote(u'\u203d')
114 if val is None: return ''
115 if not isinstance(val, unicode): val = str(val)
116 else: val = val.encode('utf-8')
117 return urllib.quote(val)
119 def httpdate(date_obj):
121 Formats a datetime object for use in HTTP headers.
124 >>> httpdate(datetime.datetime(1970, 1, 1, 1, 1, 1))
125 'Thu, 01 Jan 1970 01:01:01 GMT'
127 return date_obj.strftime("%a, %d %b %Y %H:%M:%S GMT")
129 def parsehttpdate(string_):
131 Parses an HTTP date into a datetime object.
133 >>> parsehttpdate('Thu, 01 Jan 1970 01:01:01 GMT')
134 datetime.datetime(1970, 1, 1, 1, 1, 1)
137 t = time.strptime(string_, "%a, %d %b %Y %H:%M:%S %Z")
140 return datetime.datetime(*t[:6])
144 Encodes `text` for raw use in HTML.
146 >>> htmlquote(u"<'&\">")
147 u'<'&">'
149 text = text.replace(u"&", u"&") # Must be done first!
150 text = text.replace(u"<", u"<")
151 text = text.replace(u">", u">")
152 text = text.replace(u"'", u"'")
153 text = text.replace(u'"', u""")
156 def htmlunquote(text):
158 Decodes `text` that's HTML quoted.
160 >>> htmlunquote(u'<'&">')
163 text = text.replace(u""", u'"')
164 text = text.replace(u"'", u"'")
165 text = text.replace(u">", u">")
166 text = text.replace(u"<", u"<")
167 text = text.replace(u"&", u"&") # Must be done last!
171 r"""Converts `val` so that it is safe for use in Unicode HTML.
173 >>> websafe("<'&\">")
174 u'<'&">'
177 >>> websafe(u'\u203d')
179 >>> websafe('\xe2\x80\xbd')
184 elif isinstance(val, str):
185 val = val.decode('utf-8')
186 elif not isinstance(val, unicode):
189 return htmlquote(val)
191 if __name__ == "__main__":