lionsoul2014/ip2region · error · ValueError
invalid ip address `{}`
Error message
invalid ip address `{}` What it means
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.
Source
Thrown at binding/python/ip2region/util.py:69
}}'''.format(
self.version,
self.indexPolicy,
self.createdAt,
self.startIndexPtr,
self.endIndexPtr,
self.ipVersion,
self.runtimePtrBytes
)
# ---
# ip parse and convert functions
def parse_ip(ip_string: str):
try:
return ipaddress.ip_address(ip_string).packed
except:
raise ValueError("invalid ip address `{}`".format(ip_string))
def ip_to_string(ip_bytes: bytes):
if isinstance(ip_bytes, bytes):
return str(ipaddress.ip_address(ip_bytes))
else:
raise ValueError("invalid bytes ip `{}`".format(ip_bytes))
def ip_compare(ip1: bytes, ip2: bytes):
if ip1 > ip2:
return 1
elif ip1 < ip2:
return -1
else:
return 0
def ip_sub_compare(ip1: bytes, buff: bytes, offset: int):
ip2 = buff[offset:offset+len(ip1)]
if ip1 > ip2:View on GitHub (pinned to c1a1fc7d59)
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
Example fix
# before
ip_bytes = util.parse_ip(user_input.strip()) # could be 'example.com'
# after
import ipaddress
def safe_parse(s):
try:
return ipaddress.ip_address(s.strip('[] ')).packed
except ValueError:
return None Defensive patterns
Strategy: try-catch
Validate before calling
import ipaddress
def is_valid_ip(s: str) -> bool:
try:
ipaddress.ip_address(s.strip('[] '))
return True
except ValueError:
return False Type guard
def is_ip_string(s) -> bool:
if not isinstance(s, str):
return False
try:
ipaddress.ip_address(s.strip('[] '))
return True
except ValueError:
return False Try / catch
try:
ip_bytes = util.parse_ip(raw)
except ValueError:
ip_bytes = None # treat as hostname/invalid input Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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]'.
Related errors
- invalid ip address `{}`
- invalid ip address `{}` ({} expected)
- invalid bytes ip `{}`
- invalid byte ip address with length=${ipBytes.length}
- invalid ip address (${version.name} expected)
AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02).
Data as JSON: /api/errors/f0a93eb3b97fa49e.
Report an issue: GitHub.