driver: Use inet_aton to check IPV4 address format

This commit is contained in:
linuswck 2024-04-19 15:36:27 +08:00
parent b73eacd234
commit 2df22d5dcb

View File

@ -93,15 +93,18 @@ class Device:
self._read_response = read_response
self._cmd_lock = cmd_lock
async def set_ip_settings(self, addr=[192, 168, 1, 128], port=1337, prefix_len=24, gateway=[192, 168, 1, 1]):
async def set_ip_settings(self, addr="192.168.1.128", port=1337, prefix_len=24, gateway="192.168.1.1"):
"""
After calling this fn, the ip settings are immediately saved into flash and will be effective on next reboot.
"""
if not(isinstance(addr, list) and isinstance(gateway, list)):
raise InvalidDataType
if not(len(addr) == 4 and len(gateway) == 4):
try:
socket.inet_aton(addr)
socket.inet_aton(gateway)
except OSError:
raise InvalidDataType
addr = addr.split(".")
gateway = gateway.split(".")
if not(isinstance(port, int) and isinstance(prefix_len, int)):
raise InvalidDataType