{"record":{"id":"f0a93eb3b97fa49e","repo":"lionsoul2014/ip2region","slug":"invalid-ip-address-f0a93e","errorCode":null,"errorMessage":"invalid ip address `{}`","messagePattern":"invalid ip address `(.+?)`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"binding/python/ip2region/util.py","lineNumber":69,"sourceCode":"}}'''.format(\n        self.version, \n        self.indexPolicy, \n        self.createdAt, \n        self.startIndexPtr, \n        self.endIndexPtr, \n        self.ipVersion, \n        self.runtimePtrBytes\n    )\n\n\n# ---\n# ip parse and convert functions\n\ndef parse_ip(ip_string: str):\n    try:\n        return ipaddress.ip_address(ip_string).packed\n    except:\n        raise ValueError(\"invalid ip address `{}`\".format(ip_string))\n\ndef ip_to_string(ip_bytes: bytes):\n    if isinstance(ip_bytes, bytes):\n        return str(ipaddress.ip_address(ip_bytes))\n    else:\n        raise ValueError(\"invalid bytes ip `{}`\".format(ip_bytes))\n\ndef ip_compare(ip1: bytes, ip2: bytes):\n    if ip1 > ip2:\n        return 1\n    elif ip1 < ip2:\n        return -1\n    else:\n        return 0\n\ndef ip_sub_compare(ip1: bytes, buff: bytes, offset: int):\n    ip2 = buff[offset:offset+len(ip1)]\n    if ip1 > ip2:","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/python/ip2region/util.py#L51-L87","documentation":"util.parse_ip() converts an IP string to packed bytes using ipaddress.ip_address(). If parsing fails for any reason (malformed text, hostname, empty string), the bare except re-raises a uniform ValueError with the offending input.","triggerScenarios":"Calling parse_ip (directly or via search()) with a hostname, empty string, '1.2.3.256', '1.2.3', leading/trailing whitespace, or IPv6 with invalid syntax.","commonSituations":"User-supplied IP from a form/log that is actually a hostname or garbage; splitting 'ip:port' strings and passing the port along; regional formats or brackets like '[1.2.3.4]'.","solutions":["Validate/normalize the IP string before parsing (strip whitespace, strip brackets/port)","Use a try/except around parse_ip and treat the input as non-IP (resolve hostname first if needed)","Prefer ipaddress.ip_address directly in your own code to get Python's specific exceptions (ValueError) with clearer messages"],"exampleFix":"# before\nip_bytes = util.parse_ip(user_input.strip())  # could be 'example.com'\n# after\nimport ipaddress\ndef safe_parse(s):\n    try:\n        return ipaddress.ip_address(s.strip('[] ')).packed\n    except ValueError:\n        return None","handlingStrategy":"try-catch","validationCode":"import ipaddress\ndef is_valid_ip(s: str) -> bool:\n    try:\n        ipaddress.ip_address(s.strip('[] '))\n        return True\n    except ValueError:\n        return False","typeGuard":"def is_ip_string(s) -> bool:\n    if not isinstance(s, str):\n        return False\n    try:\n        ipaddress.ip_address(s.strip('[] '))\n        return True\n    except ValueError:\n        return False","tryCatchPattern":"try:\n    ip_bytes = util.parse_ip(raw)\nexcept ValueError:\n    ip_bytes = None  # treat as hostname/invalid input","preventionTips":["Strip ports, brackets, and whitespace before parsing","Resolve hostnames to addresses before geo lookup","Log the raw input when parsing fails to spot upstream data issues"],"tags":["python","ip-address","parsing","valueerror"],"backgroundTag":"invalid-ip-address","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}