File: net.py

package info (click to toggle)
webpy 1%3A0.62-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 640 kB
  • sloc: python: 6,880; makefile: 153; sh: 1
file content (279 lines) | stat: -rw-r--r-- 6,384 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
Network Utilities
(from web.py)
"""


import datetime
import re
import socket
import time

try:
    from urllib.parse import quote
except ImportError:
    from urllib import quote

__all__ = [
    "validipaddr",
    "validip6addr",
    "validipport",
    "validip",
    "validaddr",
    "urlquote",
    "httpdate",
    "parsehttpdate",
    "htmlquote",
    "htmlunquote",
    "websafe",
]


def validip6addr(address):
    """
    Returns True if `address` is a valid IPv6 address.

        >>> validip6addr('::')
        True
        >>> validip6addr('aaaa:bbbb:cccc:dddd::1')
        True
        >>> validip6addr('1:2:3:4:5:6:7:8:9:10')
        False
        >>> validip6addr('12:10')
        False
    """
    try:
        socket.inet_pton(socket.AF_INET6, address)
    except (socket.error, AttributeError, ValueError):
        return False

    return True


def validipaddr(address):
    """
    Returns True if `address` is a valid IPv4 address.

        >>> validipaddr('192.168.1.1')
        True
        >>> validipaddr('192.168. 1.1')
        False
        >>> validipaddr('192.168.1.800')
        False
        >>> validipaddr('192.168.1')
        False
    """
    try:
        octets = address.split(".")
        if len(octets) != 4:
            return False

        for x in octets:
            if " " in x:
                return False

            if not (0 <= int(x) <= 255):
                return False
    except ValueError:
        return False
    return True


def validipport(port):
    """
    Returns True if `port` is a valid IPv4 port.

        >>> validipport('9000')
        True
        >>> validipport('foo')
        False
        >>> validipport('1000000')
        False
    """
    try:
        if not (0 <= int(port) <= 65535):
            return False
    except ValueError:
        return False
    return True


def validip(ip, defaultaddr="0.0.0.0", defaultport=8080):
    """
    Returns `(ip_address, port)` from string `ip_addr_port`

        >>> validip('1.2.3.4')
        ('1.2.3.4', 8080)
        >>> validip('80')
        ('0.0.0.0', 80)
        >>> validip('192.168.0.1:85')
        ('192.168.0.1', 85)
        >>> validip('::')
        ('::', 8080)
        >>> validip('[::]:88')
        ('::', 88)
        >>> validip('[::1]:80')
        ('::1', 80)

    """
    addr = defaultaddr
    port = defaultport

    # Matt Boswell's code to check for ipv6 first
    match = re.search(r"^\[([^]]+)\](?::(\d+))?$", ip)  # check for [ipv6]:port
    if match:
        if validip6addr(match.group(1)):
            if match.group(2):
                if validipport(match.group(2)):
                    return (match.group(1), int(match.group(2)))
            else:
                return (match.group(1), port)
    else:
        if validip6addr(ip):
            return (ip, port)
    # end ipv6 code

    ip = ip.split(":", 1)
    if len(ip) == 1:
        if not ip[0]:
            pass
        elif validipaddr(ip[0]):
            addr = ip[0]
        elif validipport(ip[0]):
            port = int(ip[0])
        else:
            raise ValueError(":".join(ip) + " is not a valid IP address/port")
    elif len(ip) == 2:
        addr, port = ip
        if not validipaddr(addr) or not validipport(port):
            raise ValueError(":".join(ip) + " is not a valid IP address/port")
        port = int(port)
    else:
        raise ValueError(":".join(ip) + " is not a valid IP address/port")
    return (addr, port)


def validaddr(string_):
    """
    Returns either (ip_address, port) or "/path/to/socket" from string_

        >>> validaddr('/path/to/socket')
        '/path/to/socket'
        >>> validaddr('8000')
        ('0.0.0.0', 8000)
        >>> validaddr('127.0.0.1')
        ('127.0.0.1', 8080)
        >>> validaddr('127.0.0.1:8000')
        ('127.0.0.1', 8000)
        >>> validip('[::1]:80')
        ('::1', 80)
        >>> validaddr('fff')
        Traceback (most recent call last):
            ...
        ValueError: fff is not a valid IP address/port
    """
    if "/" in string_:
        return string_
    else:
        return validip(string_)


def urlquote(val):
    """
    Quotes a string for use in a URL.

        >>> urlquote('://?f=1&j=1')
        '%3A//%3Ff%3D1%26j%3D1'
        >>> urlquote(None)
        ''
        >>> urlquote(u'\u203d')
        '%E2%80%BD'
    """
    if val is None:
        return ""

    val = str(val).encode("utf-8")
    return quote(val)


def httpdate(date_obj):
    """
    Formats a datetime object for use in HTTP headers.

        >>> import datetime
        >>> httpdate(datetime.datetime(1970, 1, 1, 1, 1, 1))
        'Thu, 01 Jan 1970 01:01:01 GMT'
    """
    return date_obj.strftime("%a, %d %b %Y %H:%M:%S GMT")


def parsehttpdate(string_):
    """
    Parses an HTTP date into a datetime object.

        >>> parsehttpdate('Thu, 01 Jan 1970 01:01:01 GMT')
        datetime.datetime(1970, 1, 1, 1, 1, 1)
    """
    try:
        t = time.strptime(string_, "%a, %d %b %Y %H:%M:%S %Z")
    except ValueError:
        return None
    return datetime.datetime(*t[:6])


def htmlquote(text):
    r"""
    Encodes `text` for raw use in HTML.

        >>> htmlquote(u"<'&\">")
        u'&lt;&#39;&amp;&quot;&gt;'
    """
    text = text.replace(u"&", u"&amp;")  # Must be done first!
    text = text.replace(u"<", u"&lt;")
    text = text.replace(u">", u"&gt;")
    text = text.replace(u"'", u"&#39;")
    text = text.replace(u'"', u"&quot;")
    return text


def htmlunquote(text):
    r"""
    Decodes `text` that's HTML quoted.

        >>> htmlunquote(u'&lt;&#39;&amp;&quot;&gt;')
        u'<\'&">'
    """
    text = text.replace(u"&quot;", u'"')
    text = text.replace(u"&#39;", u"'")
    text = text.replace(u"&gt;", u">")
    text = text.replace(u"&lt;", u"<")
    text = text.replace(u"&amp;", u"&")  # Must be done last!
    return text


def websafe(val):
    r"""
    Converts `val` so that it is safe for use in Unicode HTML.

        >>> websafe("<'&\">")
        u'&lt;&#39;&amp;&quot;&gt;'
        >>> websafe(None)
        u''
        >>> websafe(u'\u203d') == u'\u203d'
        True
    """
    if val is None:
        return u""

    if isinstance(val, bytes):
        val = val.decode("utf-8")
    elif not isinstance(val, str):
        val = str(val)

    return htmlquote(val)


if __name__ == "__main__":
    import doctest

    doctest.testmod()