9
9
10
10
A socket compatible interface with the Wiznet5k module.
11
11
12
- * Author(s): ladyada, Brent Rubell, Patrick Van Oosterwijck, Adam Cummick
12
+ * Author(s): ladyada, Brent Rubell, Patrick Van Oosterwijck, Adam Cummick, Martin Stephens
13
13
14
14
"""
15
15
from __future__ import annotations
@@ -39,7 +39,7 @@ def _is_ipv4_string(ipv4_address: str) -> bool:
39
39
40
40
:param: str ipv4_address: The string to test.
41
41
42
- :returns bool: True if a valid IPv4 address, False otherwise.
42
+ :return bool: True if a valid IPv4 address, False otherwise.
43
43
"""
44
44
octets = ipv4_address .split ("." , 3 )
45
45
if len (octets ) == 4 and "" .join (octets ).isdigit ():
@@ -58,9 +58,9 @@ def set_interface(iface: WIZNET5K) -> None:
58
58
_the_interface = iface
59
59
60
60
61
- def getdefaulttimeout ():
61
+ def getdefaulttimeout () -> Optional [ float ] :
62
62
"""
63
- Return the default timeout in seconds (float) for new socket objects. A value of
63
+ Return the default timeout in seconds for new socket objects. A value of
64
64
None indicates that new socket objects have no timeout. When the socket module is
65
65
first imported, the default is None.
66
66
"""
@@ -114,7 +114,7 @@ def inet_aton(ip_address: str) -> bytes:
114
114
115
115
:param str ip_address: The IPv4 address to convert.
116
116
117
- :returns bytes: The converted IPv4 address.
117
+ :return bytes: The converted IPv4 address.
118
118
"""
119
119
if not _is_ipv4_string (ip_address ):
120
120
raise ValueError ("The IPv4 address must be a dotted-quad string." )
@@ -131,7 +131,7 @@ def inet_ntoa(ip_address: Union[bytes, bytearray]) -> str:
131
131
132
132
:param Union[bytes, bytearray ip_address: The IPv4 address to convert.
133
133
134
- :returns str: The converted ip_address:
134
+ :return str: The converted ip_address:
135
135
"""
136
136
if len (ip_address ) != 4 :
137
137
raise ValueError ("The IPv4 address must be 4 bytes." )
@@ -152,7 +152,7 @@ def getaddrinfo(
152
152
host : str ,
153
153
port : int ,
154
154
family : int = 0 ,
155
- socktype : int = 0 ,
155
+ soc_type : int = 0 ,
156
156
proto : int = 0 ,
157
157
flags : int = 0 ,
158
158
) -> List [Tuple [int , int , int , str , Tuple [str , int ]]]:
@@ -164,23 +164,28 @@ def getaddrinfo(
164
164
None.
165
165
:param int port: Port number to connect to (0 - 65536).
166
166
:param int family: Ignored and hardcoded as 0x03 (the only family implemented) by the function.
167
- :param int socktype : The type of socket, either SOCK_STREAM (0x21) for TCP or SOCK_DGRAM (0x02)
168
- for UDP, defaults to 0x00 .
167
+ :param int soc_type : The type of socket, either SOCK_STREAM (0x21) for TCP or SOCK_DGRAM (0x02)
168
+ for UDP, defaults to 0 .
169
169
:param int proto: Unused in this implementation of socket.
170
170
:param int flags: Unused in this implementation of socket.
171
171
172
- :return List[Tuple[int, int, int, str, Tuple[str, int]]]: Address info entries.
172
+ :return List[Tuple[int, int, int, str, Tuple[str, int]]]: Address info entries in the form
173
+ (family, type, proto, canonname, sockaddr). In these tuples, family, type, proto are meant
174
+ to be passed to the socket() function. canonname will always be an empty string, sockaddr
175
+ is a tuple describing a socket address, whose format is (address, port), and is meant to be
176
+ passed to the socket.connect() method.
173
177
"""
174
178
if not isinstance (port , int ):
175
179
raise ValueError ("Port must be an integer" )
176
180
if not _is_ipv4_string (host ):
177
181
host = gethostbyname (host )
178
- return [(AF_INET , socktype , proto , "" , (host , port ))]
182
+ return [(AF_INET , soc_type , proto , "" , (host , port ))]
179
183
180
184
181
185
def gethostbyname (hostname : str ) -> str :
182
186
"""
183
- Lookup a host name's IPv4 address.
187
+ Translate a host name to IPv4 address format. The IPv4 address is returned as a string, such
188
+ as '100.50.200.5'. If the host name is an IPv4 address itself it is returned unchanged.
184
189
185
190
:param str hostname: Hostname to lookup.
186
191
@@ -214,7 +219,6 @@ def __init__(
214
219
defaults to SOCK_STREAM.
215
220
:param int proto: Unused, retained for compatibility.
216
221
:param Optional[int] fileno: Unused, retained for compatibility.
217
- :param Optional[int] socknum: Unused, retained for compatibility.
218
222
"""
219
223
if family != AF_INET :
220
224
raise RuntimeError ("Only AF_INET family supported by W5K modules." )
@@ -300,7 +304,7 @@ def bind(self, address: Tuple[Optional[str], int]) -> None:
300
304
:raises ValueError: If the IPv4 address specified is not the address
301
305
assigned to the WIZNET5K interface.
302
306
"""
303
- # Check is disabled to allow socket.accept to swap sockets.
307
+ # Check to see if the socket is bound is disabled to allow socket.accept to swap sockets.
304
308
# if self._listen_port:
305
309
# raise ConnectionError("The socket is already bound.")
306
310
if address [0 ]:
@@ -338,15 +342,9 @@ def accept(
338
342
self ,
339
343
) -> Tuple [socket , Tuple [str , int ]]:
340
344
"""
341
- Accept a connection.
345
+ Accept a connection. The socket must be bound to an address and listening for connections.
342
346
343
- The socket must be bound to an address and listening for connections.
344
-
345
- The return value is a pair (conn, address) where conn is a new
346
- socket object to send and receive data on the connection, and address is
347
- the address bound to the socket on the other end of the connection.
348
-
349
- :returns OptionalTuple[socket, Tuple[str, int]]: TThe return value is a pair
347
+ :return Tuple[socket, Tuple[str, int]]: The return value is a pair
350
348
(conn, address) where conn is a new socket object to send and receive data on
351
349
the connection, and address is the address bound to the socket on the other
352
350
end of the connection.
@@ -395,31 +393,30 @@ def connect(self, address: Tuple[str, int]) -> None:
395
393
396
394
def send (self , data : Union [bytes , bytearray ]) -> int :
397
395
"""
398
- Send data to the socket.
399
-
400
396
Send data to the socket. The socket must be connected to a remote socket.
401
397
Applications are responsible for checking that all data has been sent; if
402
398
only some of the data was transmitted, the application needs to attempt
403
399
delivery of the remaining data.
404
400
405
401
:param bytearray data: Data to send to the socket.
406
402
407
- :returns int: Number of bytes sent.
403
+ :return int: Number of bytes sent.
408
404
"""
409
405
bytes_sent = _the_interface .socket_write (self ._socknum , data , self ._timeout )
410
406
gc .collect ()
411
407
return bytes_sent
412
408
413
409
def sendto (self , data : bytearray , * flags_and_or_address : any ) -> int :
414
410
"""
415
- Connect to a remote socket and send data.
416
-
417
- :param bytearray data: Data to send to the socket.
411
+ Send data to the socket. The socket should not be connected to a remote socket, since the
412
+ destination socket is specified by address. Return the number of bytes sent..
418
413
419
414
Either:
415
+ :param bytearray data: Data to send to the socket.
420
416
:param [Tuple[str, int]] address: Remote socket as a (host, port) tuple.
421
417
422
418
Or:
419
+ :param bytearray data: Data to send to the socket.
423
420
:param int flags: Not implemented, kept for compatibility.
424
421
:param Tuple[int, Tuple(str, int)] address: Remote socket as a (host, port) tuple
425
422
"""
@@ -439,12 +436,13 @@ def recv(
439
436
flags : int = 0 ,
440
437
) -> bytes :
441
438
"""
442
- Receive data from the socket.
439
+ Receive data from the socket. The return value is a bytes object representing the data
440
+ received. The maximum amount of data to be received at once is specified by bufsize.
443
441
444
442
:param int bufsize: Maximum number of bytes to receive.
445
443
:param int flags: ignored, present for compatibility.
446
444
447
- :returns bytes: Data from the socket.
445
+ :return bytes: Data from the socket.
448
446
"""
449
447
stamp = time .monotonic ()
450
448
while not self ._available ():
@@ -488,13 +486,15 @@ def _embed_recv(
488
486
489
487
def recvfrom (self , bufsize : int , flags : int = 0 ) -> Tuple [bytes , Tuple [str , int ]]:
490
488
"""
491
- Receive data from the socket.
489
+ Receive data from the socket. The return value is a pair (bytes, address) where bytes is
490
+ a bytes object representing the data received and address is the address of the socket
491
+ sending the data.
492
492
493
493
:param int bufsize: Maximum number of bytes to receive.
494
494
:param int flags: Ignored, present for compatibility.
495
495
496
496
:return Tuple[bytes, Tuple[str, int]]: a tuple (bytes, address)
497
- where address is a tuple (ip , port)
497
+ where address is a tuple (address , port)
498
498
"""
499
499
return (
500
500
self .recv (bufsize ),
@@ -527,13 +527,14 @@ def recvfrom_into(
527
527
) -> Tuple [int , Tuple [str , int ]]:
528
528
"""
529
529
Receive data from the socket, writing it into buffer instead of creating a new bytestring.
530
+ The return value is a pair (nbytes, address) where nbytes is the number of bytes received
531
+ and address is the address of the socket sending the data.
530
532
531
533
:param bytearray buffer: Data buffer.
532
534
:param int nbytes: Maximum number of bytes to receive.
533
535
:param int flags: Unused, present for compatibility.
534
536
535
- :return Tuple[int, Tuple[str, int]]: A tuple (nbytes, address) where nbytes is the
536
- number of bytes received and address is a tuple (IPv4 address, port).
537
+ :return Tuple[int, Tuple[str, int]]: The number of bytes and address.
537
538
"""
538
539
return (
539
540
self .recv_into (buffer , nbytes ),
@@ -579,7 +580,10 @@ def _disconnect(self) -> None:
579
580
_the_interface .socket_disconnect (self ._socknum )
580
581
581
582
def close (self ) -> None :
582
- """Close the socket."""
583
+ """
584
+ Mark the socket closed. Once that happens, all future operations on the socket object
585
+ will fail. The remote end will receive no more data.
586
+ """
583
587
_the_interface .socket_close (self ._socknum )
584
588
585
589
def _available (self ) -> int :
@@ -608,12 +612,45 @@ def settimeout(self, value: Optional[float]) -> None:
608
612
609
613
def gettimeout (self ) -> Optional [float ]:
610
614
"""
611
- Timeout associated with socket operations.
615
+ Return the timeout in seconds (float) associated with socket operations, or None if no
616
+ timeout is set. This reflects the last call to setblocking() or settimeout().
612
617
613
618
:return Optional[float]: Timeout in seconds, or None if no timeout is set.
614
619
"""
615
620
return self ._timeout
616
621
622
+ def setblocking (self , flag : bool ) -> None :
623
+ """
624
+ Set blocking or non-blocking mode of the socket: if flag is false, the socket is set
625
+ to non-blocking, else to blocking mode.
626
+
627
+ This method is a shorthand for certain settimeout() calls:
628
+
629
+ sock.setblocking(True) is equivalent to sock.settimeout(None)
630
+ sock.setblocking(False) is equivalent to sock.settimeout(0.0)
631
+
632
+ :param bool flag: The blocking mode of the socket.
633
+
634
+ :raises TypeError: If flag is not a bool.
635
+
636
+ """
637
+ if flag is True :
638
+ self .settimeout (None )
639
+ elif flag is False :
640
+ self .settimeout (0.0 )
641
+ else :
642
+ raise TypeError ("Flag must be a boolean." )
643
+
644
+ def getblocking (self ) -> bool :
645
+ """
646
+ Return True if socket is in blocking mode, False if in non-blocking.
647
+
648
+ This is equivalent to checking socket.gettimeout() == 0.
649
+
650
+ :return bool: Blocking mode of the socket.
651
+ """
652
+ return self .gettimeout () == 0
653
+
617
654
@property
618
655
def family (self ) -> int :
619
656
"""Socket family (always 0x03 in this implementation)."""
0 commit comments