Convert a decimal octet into an integer. Args: octet_str: A string, the number to parse. Returns: The octet as an integer. Raises: ValueError: if the octet isn't strictly a decimal from [0..255].
(cls, octet_str)
| 1204 | |
| 1205 | @classmethod |
| 1206 | def _parse_octet(cls, octet_str): |
| 1207 | """Convert a decimal octet into an integer. |
| 1208 | |
| 1209 | Args: |
| 1210 | octet_str: A string, the number to parse. |
| 1211 | |
| 1212 | Returns: |
| 1213 | The octet as an integer. |
| 1214 | |
| 1215 | Raises: |
| 1216 | ValueError: if the octet isn't strictly a decimal from [0..255]. |
| 1217 | |
| 1218 | """ |
| 1219 | if not octet_str: |
| 1220 | raise ValueError("Empty octet not permitted") |
| 1221 | # Reject non-ASCII digits. |
| 1222 | if not (octet_str.isascii() and octet_str.isdigit()): |
| 1223 | msg = "Only decimal digits permitted in %r" |
| 1224 | raise ValueError(msg % octet_str) |
| 1225 | # We do the length check second, since the invalid character error |
| 1226 | # is likely to be more informative for the user |
| 1227 | if len(octet_str) > 3: |
| 1228 | msg = "At most 3 characters permitted in %r" |
| 1229 | raise ValueError(msg % octet_str) |
| 1230 | # Handle leading zeros as strict as glibc's inet_pton() |
| 1231 | # See security bug bpo-36384 |
| 1232 | if octet_str != '0' and octet_str[0] == '0': |
| 1233 | msg = "Leading zeros are not permitted in %r" |
| 1234 | raise ValueError(msg % octet_str) |
| 1235 | # Convert to integer (we know digits are legal) |
| 1236 | octet_int = int(octet_str, 10) |
| 1237 | if octet_int > 255: |
| 1238 | raise ValueError("Octet %d (> 255) not permitted" % octet_int) |
| 1239 | return octet_int |
| 1240 | |
| 1241 | @classmethod |
| 1242 | def _string_from_ip_int(cls, ip_int): |