2323# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2424# POSSIBILITY OF SUCH DAMAGE.
2525
26+ import re
27+
28+ # sniff for python2.x / python3k compatibility "fixes'
29+ try :
30+ basestring = basestring
31+ except NameError :
32+ # 'basestring' is undefined, must be python3k
33+ basestring = str
34+
35+ try :
36+ bin = bin
37+ except NameError :
38+ # builtin bin function doesn't exist
39+ def bin (x ):
40+ """
41+ From http://code.activestate.com/recipes/219300/#c7
42+ """
43+ if x < 0 :
44+ return '-' + bin (- x )
45+ out = []
46+ if x == 0 :
47+ out .append ('0' )
48+ while x > 0 :
49+ out .append ('01' [x & 1 ])
50+ x >>= 1
51+ pass
52+ try :
53+ return '0b' + '' .join (reversed (out ))
54+ except NameError :
55+ out .reverse ()
56+ return '0b' + '' .join (out )
57+ # end bin
58+ # end compatibility "fixes'
59+
2660__all__ = (
2761 'cidr2block' ,
2862 'hex2ip' ,
6094 'TEST_NET_3' ,
6195)
6296
63-
64- import re
65-
66-
67- # sniff for python2.x / python3k compatibility "fixes'
68- try :
69- basestring = basestring
70- except NameError :
71- # 'basestring' is undefined, must be python3k
72- basestring = str
73-
74- try :
75- bin = bin
76- except NameError :
77- # builtin bin function doesn't exist
78- def bin (x ):
79- """
80- From http://code.activestate.com/recipes/219300/#c7
81- """
82- if x < 0 :
83- return '-' + bin (- x )
84- out = []
85- if x == 0 :
86- out .append ('0' )
87- while x > 0 :
88- out .append ('01' [x & 1 ])
89- x >>= 1
90- pass
91- try :
92- return '0b' + '' .join (reversed (out ))
93- except NameError :
94- out .reverse ()
95- return '0b' + '' .join (out )
96- # end bin
97- # end compatibility "fixes'
98-
99-
10097#: Regex for validating an IPv4 address
10198_DOTTED_QUAD_RE = re .compile (r'^(\d{1,3}\.){0,3}\d{1,3}$' )
10299
@@ -285,6 +282,10 @@ def validate_netmask(s):
285282 True
286283 >>> validate_netmask('128.0.0.1')
287284 False
285+ >>> validate_netmask('1.255.255.0')
286+ False
287+ >>> validate_netmask('0.255.255.0')
288+ False
288289
289290
290291 :param s: String to validate as a dotted-quad notation netmask.
@@ -293,7 +294,8 @@ def validate_netmask(s):
293294 :raises: TypeError
294295 """
295296 if validate_ip (s ):
296- mask = bin (ip2network (s ))[2 :]
297+ # Convert to binary string, strip '0b' prefix, 0 pad to 32 bits
298+ mask = bin (ip2network (s ))[2 :].zfill (32 )
297299 # all left most bits must be 1, all right most must be 0
298300 seen0 = False
299301 for c in mask :
0 commit comments