Skip to content

Commit b4f36e4

Browse files
authored
Merge pull request #23 from kevinaj/fix_ip_host
Check if host is an IPv4 address
2 parents 8ae1012 + dffa67c commit b4f36e4

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_socket.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
7575
"""
7676
if not isinstance(port, int):
7777
raise RuntimeError("Port must be an integer")
78+
if is_ipv4(host):
79+
return [(AF_INET, socktype, proto, "", (host, port))]
7880
return [(AF_INET, socktype, proto, "", (gethostbyname(host), port))]
7981

8082

@@ -88,6 +90,19 @@ def gethostbyname(hostname):
8890
return addr
8991

9092

93+
def is_ipv4(host):
94+
"""Checks if a host string is an IPv4 address.
95+
:param str host: host's name or ip
96+
"""
97+
octets = host.split(".", 3)
98+
if len(octets) != 4 or not "".join(octets).isdigit():
99+
return False
100+
for octet in octets:
101+
if int(octet) > 255:
102+
return False
103+
return True
104+
105+
91106
# pylint: disable=invalid-name
92107
class socket:
93108
"""A simplified implementation of the Python 'socket' class
@@ -275,9 +290,7 @@ def close(self):
275290
SOCKETS.remove(self.socknum)
276291

277292
def available(self):
278-
"""Returns how many bytes of data are available to be read from the socket.
279-
280-
"""
293+
"""Returns how many bytes of data are available to be read from the socket."""
281294
return _the_interface.socket_available(self.socknum, self._sock_type)
282295

283296
def settimeout(self, value):

0 commit comments

Comments
 (0)