Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42b2f2e

Browse files
committed
#3550: socket APIs use bytes, not strings.
1 parent ad3489e commit 42b2f2e

1 file changed

Lines changed: 36 additions & 33 deletions

File tree

Doc/library/socket.rst

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ The module :mod:`socket` exports the following constants and functions:
372372
.. function:: inet_aton(ip_string)
373373

374374
Convert an IPv4 address from dotted-quad string format (for example,
375-
'123.45.67.89') to 32-bit packed binary format, as a string four characters in
375+
'123.45.67.89') to 32-bit packed binary format, as a bytes object four characters in
376376
length. This is useful when conversing with a program that uses the standard C
377377
library and needs objects of type :ctype:`struct in_addr`, which is the C type
378378
for the 32-bit packed binary this function returns.
@@ -387,23 +387,25 @@ The module :mod:`socket` exports the following constants and functions:
387387

388388
.. function:: inet_ntoa(packed_ip)
389389

390-
Convert a 32-bit packed IPv4 address (a string four characters in length) to its
391-
standard dotted-quad string representation (for example, '123.45.67.89'). This
392-
is useful when conversing with a program that uses the standard C library and
393-
needs objects of type :ctype:`struct in_addr`, which is the C type for the
394-
32-bit packed binary data this function takes as an argument.
390+
Convert a 32-bit packed IPv4 address (a bytes object four characters in
391+
length) to its standard dotted-quad string representation (for example,
392+
'123.45.67.89'). This is useful when conversing with a program that uses the
393+
standard C library and needs objects of type :ctype:`struct in_addr`, which
394+
is the C type for the 32-bit packed binary data this function takes as an
395+
argument.
395396

396-
If the string passed to this function is not exactly 4 bytes in length,
397-
:exc:`socket.error` will be raised. :func:`inet_ntoa` does not support IPv6, and
398-
:func:`getnameinfo` should be used instead for IPv4/v6 dual stack support.
397+
If the byte sequence passed to this function is not exactly 4 bytes in
398+
length, :exc:`socket.error` will be raised. :func:`inet_ntoa` does not
399+
support IPv6, and :func:`getnameinfo` should be used instead for IPv4/v6 dual
400+
stack support.
399401

400402

401403
.. function:: inet_pton(address_family, ip_string)
402404

403-
Convert an IP address from its family-specific string format to a packed, binary
404-
format. :func:`inet_pton` is useful when a library or network protocol calls for
405-
an object of type :ctype:`struct in_addr` (similar to :func:`inet_aton`) or
406-
:ctype:`struct in6_addr`.
405+
Convert an IP address from its family-specific string format to a packed,
406+
binary format. :func:`inet_pton` is useful when a library or network protocol
407+
calls for an object of type :ctype:`struct in_addr` (similar to
408+
:func:`inet_aton`) or :ctype:`struct in6_addr`.
407409

408410
Supported values for *address_family* are currently :const:`AF_INET` and
409411
:const:`AF_INET6`. If the IP address string *ip_string* is invalid,
@@ -416,9 +418,9 @@ The module :mod:`socket` exports the following constants and functions:
416418

417419
.. function:: inet_ntop(address_family, packed_ip)
418420

419-
Convert a packed IP address (a string of some number of characters) to its
421+
Convert a packed IP address (a bytes object of some number of characters) to its
420422
standard, family-specific string representation (for example, ``'7.10.0.5'`` or
421-
``'5aef:2b::8'``) :func:`inet_ntop` is useful when a library or network protocol
423+
``'5aef:2b::8'``). :func:`inet_ntop` is useful when a library or network protocol
422424
returns an object of type :ctype:`struct in_addr` (similar to :func:`inet_ntoa`)
423425
or :ctype:`struct in6_addr`.
424426

@@ -534,9 +536,9 @@ correspond to Unix system calls applicable to sockets.
534536
are defined in this module. If *buflen* is absent, an integer option is assumed
535537
and its integer value is returned by the function. If *buflen* is present, it
536538
specifies the maximum length of the buffer used to receive the option in, and
537-
this buffer is returned as a string. It is up to the caller to decode the
539+
this buffer is returned as a bytes object. It is up to the caller to decode the
538540
contents of the buffer (see the optional built-in module :mod:`struct` for a way
539-
to decode C structures encoded as strings).
541+
to decode C structures encoded as byte strings).
540542

541543

542544
.. method:: socket.ioctl(control, option)
@@ -569,7 +571,7 @@ correspond to Unix system calls applicable to sockets.
569571

570572
.. method:: socket.recv(bufsize[, flags])
571573

572-
Receive data from the socket. The return value is a string representing the
574+
Receive data from the socket. The return value is a bytes object representing the
573575
data received. The maximum amount of data to be received at once is specified
574576
by *bufsize*. See the Unix manual page :manpage:`recv(2)` for the meaning of
575577
the optional argument *flags*; it defaults to zero.
@@ -582,17 +584,17 @@ correspond to Unix system calls applicable to sockets.
582584

583585
.. method:: socket.recvfrom(bufsize[, flags])
584586

585-
Receive data from the socket. The return value is a pair ``(string, address)``
586-
where *string* is a string representing the data received and *address* is the
587+
Receive data from the socket. The return value is a pair ``(bytes, address)``
588+
where *bytes* is a bytes object representing the data received and *address* is the
587589
address of the socket sending the data. See the Unix manual page
588590
:manpage:`recv(2)` for the meaning of the optional argument *flags*; it defaults
589591
to zero. (The format of *address* depends on the address family --- see above.)
590592

591593

592594
.. method:: socket.recvfrom_into(buffer[, nbytes[, flags]])
593595

594-
Receive data from the socket, writing it into *buffer* instead of creating a
595-
new string. The return value is a pair ``(nbytes, address)`` where *nbytes* is
596+
Receive data from the socket, writing it into *buffer* instead of creating a
597+
new bytestring. The return value is a pair ``(nbytes, address)`` where *nbytes* is
596598
the number of bytes received and *address* is the address of the socket sending
597599
the data. See the Unix manual page :manpage:`recv(2)` for the meaning of the
598600
optional argument *flags*; it defaults to zero. (The format of *address*
@@ -602,13 +604,13 @@ correspond to Unix system calls applicable to sockets.
602604
.. method:: socket.recv_into(buffer[, nbytes[, flags]])
603605

604606
Receive up to *nbytes* bytes from the socket, storing the data into a buffer
605-
rather than creating a new string. If *nbytes* is not specified (or 0),
607+
rather than creating a new bytestring. If *nbytes* is not specified (or 0),
606608
receive up to the size available in the given buffer. See the Unix manual page
607609
:manpage:`recv(2)` for the meaning of the optional argument *flags*; it defaults
608610
to zero.
609611

610612

611-
.. method:: socket.send(string[, flags])
613+
.. method:: socket.send(bytes[, flags])
612614

613615
Send data to the socket. The socket must be connected to a remote socket. The
614616
optional *flags* argument has the same meaning as for :meth:`recv` above.
@@ -617,17 +619,17 @@ correspond to Unix system calls applicable to sockets.
617619
application needs to attempt delivery of the remaining data.
618620

619621

620-
.. method:: socket.sendall(string[, flags])
622+
.. method:: socket.sendall(bytes[, flags])
621623

622624
Send data to the socket. The socket must be connected to a remote socket. The
623625
optional *flags* argument has the same meaning as for :meth:`recv` above.
624-
Unlike :meth:`send`, this method continues to send data from *string* until
626+
Unlike :meth:`send`, this method continues to send data from *bytes* until
625627
either all data has been sent or an error occurs. ``None`` is returned on
626628
success. On error, an exception is raised, and there is no way to determine how
627629
much data, if any, was successfully sent.
628630

629631

630-
.. method:: socket.sendto(string[, flags], address)
632+
.. method:: socket.sendto(bytes[, flags], address)
631633

632634
Send data to the socket. The socket should not be connected to a remote socket,
633635
since the destination socket is specified by *address*. The optional *flags*
@@ -693,9 +695,9 @@ in general it is recommended to call :meth:`settimeout` before calling
693695
Set the value of the given socket option (see the Unix manual page
694696
:manpage:`setsockopt(2)`). The needed symbolic constants are defined in the
695697
:mod:`socket` module (:const:`SO_\*` etc.). The value can be an integer or a
696-
string representing a buffer. In the latter case it is up to the caller to
697-
ensure that the string contains the proper bits (see the optional built-in
698-
module :mod:`struct` for a way to encode C structures as strings).
698+
bytes object representing a buffer. In the latter case it is up to the caller to
699+
ensure that the bytestring contains the proper bits (see the optional built-in
700+
module :mod:`struct` for a way to encode C structures as bytestrings).
699701

700702

701703
.. method:: socket.shutdown(how)
@@ -768,7 +770,7 @@ The first two examples support IPv4 only. ::
768770
PORT = 50007 # The same port as used by the server
769771
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
770772
s.connect((HOST, PORT))
771-
s.send('Hello, world')
773+
s.send(b'Hello, world')
772774
data = s.recv(1024)
773775
s.close()
774776
print('Received', repr(data))
@@ -787,7 +789,8 @@ sends traffic to the first one connected successfully. ::
787789
HOST = None # Symbolic name meaning all available interfaces
788790
PORT = 50007 # Arbitrary non-privileged port
789791
s = None
790-
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
792+
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
793+
socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
791794
af, socktype, proto, canonname, sa = res
792795
try:
793796
s = socket.socket(af, socktype, proto)
@@ -839,7 +842,7 @@ sends traffic to the first one connected successfully. ::
839842
if s is None:
840843
print('could not open socket')
841844
sys.exit(1)
842-
s.send('Hello, world')
845+
s.send(b'Hello, world')
843846
data = s.recv(1024)
844847
s.close()
845848
print('Received', repr(data))

0 commit comments

Comments
 (0)