@@ -75,6 +75,8 @@ def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
75
75
"""
76
76
if not isinstance (port , int ):
77
77
raise RuntimeError ("Port must be an integer" )
78
+ if is_ipv4 (host ):
79
+ return [(AF_INET , socktype , proto , "" , (host , port ))]
78
80
return [(AF_INET , socktype , proto , "" , (gethostbyname (host ), port ))]
79
81
80
82
@@ -88,6 +90,19 @@ def gethostbyname(hostname):
88
90
return addr
89
91
90
92
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
+
91
106
# pylint: disable=invalid-name
92
107
class socket :
93
108
"""A simplified implementation of the Python 'socket' class
@@ -275,9 +290,7 @@ def close(self):
275
290
SOCKETS .remove (self .socknum )
276
291
277
292
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."""
281
294
return _the_interface .socket_available (self .socknum , self ._sock_type )
282
295
283
296
def settimeout (self , value ):
0 commit comments