Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/validators/ip_address.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:08 +0000

1"""IP Address.""" 

2# -*- coding: utf-8 -*- 

3 

4# standard 

5from ipaddress import ( 

6 NetmaskValueError, 

7 AddressValueError, 

8 IPv6Network, 

9 IPv6Address, 

10 IPv4Network, 

11 IPv4Address, 

12) 

13 

14# local 

15from .utils import validator 

16 

17 

18@validator 

19def ipv4(value: str, /, *, cidr: bool = True, strict: bool = False): 

20 """Returns whether a given value is a valid IPv4 address. 

21 

22 From Python version 3.9.5 leading zeros are no longer tolerated 

23 and are treated as an error. The initial version of ipv4 validator 

24 was inspired from [WTForms IPAddress validator][1]. 

25 

26 [1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py 

27 

28 Examples: 

29 >>> ipv4('123.0.0.7') 

30 # Output: True 

31 >>> ipv4('1.1.1.1/8') 

32 # Output: True 

33 >>> ipv4('900.80.70.11') 

34 # Output: ValidationFailure(func=ipv4, args={'value': '900.80.70.11'}) 

35 

36 Args: 

37 value: 

38 IP address string to validate. 

39 cidr: 

40 IP address string may contain CIDR annotation 

41 strict: 

42 If strict is True and host bits are set in the supplied address. 

43 Otherwise, the host bits are masked out to determine the 

44 appropriate network address. ref [IPv4Network][2]. 

45 [2]: https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Network 

46 

47 Returns: 

48 (Literal[True]): 

49 If `value` is a valid IPv4 address. 

50 (ValidationFailure): 

51 If `value` is an invalid IPv4 address. 

52 

53 Note: 

54 - *In version 0.14.0*: 

55 - Add supports for CIDR notation 

56 

57 > *New in version 0.2.0* 

58 """ 

59 if not value: 

60 return False 

61 try: 

62 if cidr and value.count("/") == 1: 

63 return IPv4Network(value, strict=strict) 

64 return IPv4Address(value) 

65 except (AddressValueError, NetmaskValueError): 

66 return False 

67 

68 

69@validator 

70def ipv6(value: str, /, *, cidr: bool = True, strict: bool = False): 

71 """Returns if a given value is a valid IPv6 address. 

72 

73 Including IPv4-mapped IPv6 addresses. The initial version of ipv6 validator 

74 was inspired from [WTForms IPAddress validator][1]. 

75 

76 [1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py 

77 

78 Examples: 

79 >>> ipv6('::ffff:192.0.2.128') 

80 # Output: True 

81 >>> ipv6('::1/128') 

82 # Output: True 

83 >>> ipv6('abc.0.0.1') 

84 # Output: ValidationFailure(func=ipv6, args={'value': 'abc.0.0.1'}) 

85 

86 Args: 

87 value: 

88 IP address string to validate. 

89 cidr: 

90 IP address string may contain CIDR annotation 

91 strict: 

92 If strict is True and host bits are set in the supplied address. 

93 Otherwise, the host bits are masked out to determine the 

94 appropriate network address. ref [IPv6Network][2]. 

95 [2]: https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv6Network 

96 

97 Returns: 

98 (Literal[True]): 

99 If `value` is a valid IPv6 address. 

100 (ValidationFailure): 

101 If `value` is an invalid IPv6 address. 

102 

103 Note: 

104 - *In version 0.14.0*: 

105 - Add supports for CIDR notation 

106 

107 > *New in version 0.2.0* 

108 """ 

109 if not value: 

110 return False 

111 try: 

112 if cidr and value.count("/") == 1: 

113 return IPv6Network(value, strict=strict) 

114 return IPv6Address(value) 

115 except (AddressValueError, NetmaskValueError): 

116 return False