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

Skip to content

Commit c9908c4

Browse files
committed
Document IPv6 changes. Contributed by itojun.
1 parent cbce202 commit c9908c4

2 files changed

Lines changed: 206 additions & 29 deletions

File tree

Doc/lib/libftplib.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ \subsection{FTP Objects \label{ftp-objects}}
191191

192192
\begin{methoddesc}{transfercmd}{cmd\optional{, rest}}
193193
Initiate a transfer over the data connection. If the transfer is
194-
active, send a \samp{PORT} command and the transfer command specified
194+
active, send a \samp{EPRT} or \samp{PORT} command and the transfer command specified
195195
by \var{cmd}, and accept the connection. If the server is passive,
196-
send a \samp{PASV} command, connect to it, and start the transfer
196+
send a \samp{EPSV} or \samp{PASV} command, connect to it, and start the transfer
197197
command. Either way, return the socket for the connection.
198198

199199
If optional \var{rest} is given, a \samp{REST} command is

Doc/lib/libsocket.tex

Lines changed: 204 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ \section{\module{socket} ---
1919
of information on the details of socket semantics. For \UNIX, refer
2020
to the manual pages; for Windows, see the WinSock (or Winsock 2)
2121
specification.
22+
For IPv6-ready APIs, readers may want to refer to RFC2553 titled
23+
\cite{Basic Socket Interface Extensions for IPv6}.
2224

2325
The Python interface is a straightforward transliteration of the
2426
\UNIX{} system call and library interface for sockets to Python's
@@ -30,20 +32,40 @@ \section{\module{socket} ---
3032
receive operations is automatic, and buffer length is implicit on send
3133
operations.
3234

33-
Socket addresses are represented as a single string for the
34-
\constant{AF_UNIX} address family and as a pair
35-
\code{(\var{host}, \var{port})} for the \constant{AF_INET} address
36-
family, where \var{host} is a string representing
37-
either a hostname in Internet domain notation like
38-
\code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'},
39-
and \var{port} is an integral port number. Other address families are
40-
currently not supported. The address format required by a particular
41-
socket object is automatically selected based on the address family
42-
specified when the socket object was created.
43-
44-
For IP addresses, two special forms are accepted instead of a host
35+
Socket addresses are represented as follows:
36+
A single string is used for the \constant{AF_UNIX} address family.
37+
A pair \code{(\var{host}, \var{port})} is used for the
38+
\constant{AF_INET} address family, where \var{host} is a string
39+
representing either a hostname in Internet domain notation like
40+
\code{'daring.cwi.nl'} or an IPv4 address like \code{'100.50.200.5'},
41+
and \var{port} is an integral port number.
42+
For \constant{AF_INET6} address family, a four-tuple
43+
\code{(\var{host}, \var{port}, \var{flowinfo}, \var{scopeid})} is
44+
used, where \var{flowinfo} and \var{scopeid} represents
45+
\code{sin6_flowinfo} and \code{sin6_scope_id} member in
46+
\constant{struct sockaddr_in6} in C.
47+
For \module{socket} module methods, \var{flowinfo} and \var{scopeid}
48+
can be omitted just for backward compatibility. Note, however,
49+
omission of \var{scopeid} can cause problems in manipulating scoped
50+
IPv6 addresses. Other address families are currently not supported.
51+
The address format required by a particular socket object is
52+
automatically selected based on the address family specified when the
53+
socket object was created.
54+
55+
For IPv4 addresses, two special forms are accepted instead of a host
4556
address: the empty string represents \constant{INADDR_ANY}, and the string
4657
\code{'<broadcast>'} represents \constant{INADDR_BROADCAST}.
58+
The behavior is not available for IPv6 for backward compatibility,
59+
therefore, you may want to avoid these if you intend to support IPv6 with
60+
your Python programs.
61+
62+
If you use a hostname in the \var{host} portion of IPv4/v6 socket
63+
address, the program may show a nondeterministic behavior, as Python
64+
uses the first address returned from the DNS resolution. The socket
65+
address will be resolved differently into an actual IPv4/v6 address,
66+
depending on the results from DNS resolution and/or the host
67+
configuration. For deterministic behavior use a numeric address in
68+
\var{host} portion.
4769

4870
All errors raise exceptions. The normal exceptions for invalid
4971
argument types and out-of-memory conditions can be raised; errors
@@ -57,7 +79,7 @@ \section{\module{socket} ---
5779

5880

5981
\begin{excdesc}{error}
60-
This exception is raised for socket- or address-related errors.
82+
This exception is raised for socket-related errors.
6183
The accompanying value is either a string telling what went wrong or a
6284
pair \code{(\var{errno}, \var{string})}
6385
representing an error returned by a system
@@ -66,8 +88,29 @@ \section{\module{socket} ---
6688
names for the error codes defined by the underlying operating system.
6789
\end{excdesc}
6890

91+
\begin{excdesc}{herror}
92+
This exception is raised for address-related errors, i.e. for
93+
functions that use \var{h_errno} in C API, including
94+
\function{gethostbyname_ex} and \function{gethostbyaddr}.
95+
96+
The accompanying value is a pair \code{(\var{h_errno}, \var{string})}
97+
representing an error returned by a library call. \var{string}
98+
represents the description of \var{h_errno}, as returned by
99+
\cfunction{hstrerror} C API.
100+
\end{excdesc}
101+
102+
\begin{excdesc}{gaierror}
103+
This exception is raised for address-related errors, for
104+
\function{getaddrinfo} and \function{getnameinfo}.
105+
The accompanying value is a pair \code{(\var{error}, \var{string})}
106+
representing an error returned by a library call.
107+
\var{string} represents the description of \var{error}, as returned
108+
by \cfunction{gai_strerror} C API.
109+
\end{excdesc}
110+
69111
\begin{datadesc}{AF_UNIX}
70112
\dataline{AF_INET}
113+
\dataline{AF_INET6}
71114
These constants represent the address (and protocol) families,
72115
used for the first argument to \function{socket()}. If the
73116
\constant{AF_UNIX} constant is not defined then this protocol is
@@ -93,6 +136,10 @@ \section{\module{socket} ---
93136
\dataline{IPPORT_*}
94137
\dataline{INADDR_*}
95138
\dataline{IP_*}
139+
\dataline{IPV6_*}
140+
\dataline{EAI_*}
141+
\dataline{AI_*}
142+
\dataline{NI_*}
96143
Many constants of these forms, documented in the \UNIX{} documentation on
97144
sockets and/or the IP protocol, are also defined in the socket module.
98145
They are generally used in arguments to the \method{setsockopt()} and
@@ -101,6 +148,34 @@ \section{\module{socket} ---
101148
for a few symbols, default values are provided.
102149
\end{datadesc}
103150

151+
\begin{funcdesc}{getaddrinfo}{host, port\optional{, family, socktype, proto, flags}}
152+
153+
Resolves the \var{host}/\var{port} argument, into a sequence of
154+
5-tuples that contain all the necessary argument for the sockets
155+
manipulation. \var{host} is a domain name, a string representation of
156+
IPv4/v6 address or \code{None}.
157+
\var{port} is a string service name (like \code{``http''}), a numeric
158+
port number or \code{None}.
159+
160+
The rest of the arguments are optional and must be numeric if
161+
specified. For \var{host} and \var{port}, by passing either an empty
162+
string or \code{None}, you can pass \code{NULL} to the C API. The
163+
\function{getaddrinfo()} function returns a list of 5-tuples with
164+
the following structure:
165+
166+
\code{(\var{family}, \var{socktype}, \var{proto}, \var{canonname}, \var{sockaddr})}.
167+
168+
\var{family}, \var{socktype}, \var{proto} are all integer and are meant to
169+
be passed to the \function{socket()} function.
170+
\var{canonname} is a string representing the canonical name of the \var{host}.
171+
It can be a numeric IPv4/v6 address when \code{AI_CANONNAME} is specified
172+
for a numeric \var{host}.
173+
\var{sockaddr} is a tuple describing a socket address, as described above.
174+
See \code{Lib/httplib.py} and other library files
175+
for a typical usage of the function.
176+
\versionadded{2.2}
177+
\end{funcdesc}
178+
104179
\begin{funcdesc}{getfqdn}{\optional{name}}
105180
Return a fully qualified domain name for \var{name}.
106181
If \var{name} is omitted or empty, it is interpreted as the local
@@ -113,26 +188,33 @@ \section{\module{socket} ---
113188
\end{funcdesc}
114189

115190
\begin{funcdesc}{gethostbyname}{hostname}
116-
Translate a host name to IP address format. The IP address is
191+
Translate a host name to IPv4 address format. The IPv4 address is
117192
returned as a string, e.g., \code{'100.50.200.5'}. If the host name
118-
is an IP address itself it is returned unchanged. See
193+
is an IPv4 address itself it is returned unchanged. See
119194
\function{gethostbyname_ex()} for a more complete interface.
195+
\function{gethostbyname()} does not support IPv6 name resolution, and
196+
\function{getaddrinfo()} should be used instead for IPv4/v6 dual stack support.
120197
\end{funcdesc}
121198

122199
\begin{funcdesc}{gethostbyname_ex}{hostname}
123-
Translate a host name to IP address format, extended interface.
200+
Translate a host name to IPv4 address format, extended interface.
124201
Return a triple \code{(hostname, aliaslist, ipaddrlist)} where
125202
\code{hostname} is the primary host name responding to the given
126203
\var{ip_address}, \code{aliaslist} is a (possibly empty) list of
127204
alternative host names for the same address, and \code{ipaddrlist} is
128-
a list of IP addresses for the same interface on the same
205+
a list of IPv4 addresses for the same interface on the same
129206
host (often but not always a single address).
207+
\function{gethostbyname_ex()} does not support IPv6 name resolution, and
208+
\function{getaddrinfo()} should be used instead for IPv4/v6 dual stack support.
130209
\end{funcdesc}
131210

132211
\begin{funcdesc}{gethostname}{}
133212
Return a string containing the hostname of the machine where
134-
the Python interpreter is currently executing. If you want to know the
135-
current machine's IP address, use \code{gethostbyname(gethostname())}.
213+
the Python interpreter is currently executing.
214+
If you want to know the current machine's IP address, you may want to use
215+
\code{gethostbyname(gethostname())}.
216+
This operation assumes that there is a valid address-to-host mapping for
217+
the host, and the assumption does not always hold.
136218
Note: \function{gethostname()} doesn't always return the fully qualified
137219
domain name; use \code{gethostbyaddr(gethostname())}
138220
(see below).
@@ -143,10 +225,21 @@ \section{\module{socket} ---
143225
\var{ipaddrlist})} where \var{hostname} is the primary host name
144226
responding to the given \var{ip_address}, \var{aliaslist} is a
145227
(possibly empty) list of alternative host names for the same address,
146-
and \var{ipaddrlist} is a list of IP addresses for the same interface
228+
and \var{ipaddrlist} is a list of IPv4/v6 addresses for the same interface
147229
on the same host (most likely containing only a single address).
148230
To find the fully qualified domain name, use the function
149231
\function{getfqdn()}.
232+
\function{gethostbyaddr} supports both IPv4 and IPv6.
233+
\end{funcdesc}
234+
235+
\begin{funcdesc}{getnameinfo}{sockaddr, flags}
236+
Translate a socket address \var{sockaddr} into a 2-tuple
237+
\code{(\var{host}, \var{port})}.
238+
Depending on the settings of \var{flags}, the result can contain a
239+
fully-qualified domain name or numeric address representation in
240+
\var{host}. Similarly, \var{port} can contain a string port name or a
241+
numeric port number.
242+
\versionadded{2.2}
150243
\end{funcdesc}
151244

152245
\begin{funcdesc}{getprotobyname}{protocolname}
@@ -166,7 +259,7 @@ \section{\module{socket} ---
166259

167260
\begin{funcdesc}{socket}{family, type\optional{, proto}}
168261
Create a new socket using the given address family, socket type and
169-
protocol number. The address family should be \constant{AF_INET} or
262+
protocol number. The address family should be \constant{AF_INET}, \constant{AF_INET6} or
170263
\constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM},
171264
\constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
172265
The protocol number is usually zero and may be omitted in that case.
@@ -209,22 +302,25 @@ \section{\module{socket} ---
209302
\end{funcdesc}
210303

211304
\begin{funcdesc}{inet_aton}{ip_string}
212-
Convert an IP address from dotted-quad string format
305+
Convert an IPv4 address from dotted-quad string format
213306
(e.g.\ '123.45.67.89') to 32-bit packed binary format, as a string four
214307
characters in length.
215308

216309
Useful when conversing with a program that uses the standard C library
217310
and needs objects of type \ctype{struct in_addr}, which is the C type
218311
for the 32-bit packed binary this function returns.
219312

220-
If the IP address string passed to this function is invalid,
313+
If the IPv4 address string passed to this function is invalid,
221314
\exception{socket.error} will be raised. Note that exactly what is
222315
valid depends on the underlying C implementation of
223316
\cfunction{inet_aton()}.
317+
318+
\function{inet_aton} does not support IPv6, and
319+
\function{getnameinfo()} should be used instead for IPv4/v6 dual stack support.
224320
\end{funcdesc}
225321

226322
\begin{funcdesc}{inet_ntoa}{packed_ip}
227-
Convert a 32-bit packed IP address (a string four characters in
323+
Convert a 32-bit packed IPv4 address (a string four characters in
228324
length) to its standard dotted-quad string representation
229325
(e.g. '123.45.67.89').
230326

@@ -234,6 +330,9 @@ \section{\module{socket} ---
234330

235331
If the string passed to this function is not exactly 4 bytes in
236332
length, \exception{socket.error} will be raised.
333+
334+
\function{inet_ntoa} does not support IPv6, and
335+
\function{getnameinfo()} should be used instead for IPv4/v6 dual stack support.
237336
\end{funcdesc}
238337

239338
\begin{datadesc}{SocketType}
@@ -306,14 +405,14 @@ \subsection{Socket Objects \label{socket-objects}}
306405

307406
\begin{methoddesc}[socket]{getpeername}{}
308407
Return the remote address to which the socket is connected. This is
309-
useful to find out the port number of a remote IP socket, for instance.
408+
useful to find out the port number of a remote IPv4/v6 socket, for instance.
310409
(The format of the address returned depends on the address family ---
311410
see above.) On some systems this function is not supported.
312411
\end{methoddesc}
313412

314413
\begin{methoddesc}[socket]{getsockname}{}
315414
Return the socket's own address. This is useful to find out the port
316-
number of an IP socket, for instance.
415+
number of an IPv4/v6 socket, for instance.
317416
(The format of the address returned depends on the address family ---
318417
see above.)
319418
\end{methoddesc}
@@ -413,7 +512,7 @@ \subsection{Socket Objects \label{socket-objects}}
413512

414513
\subsection{Example \label{socket-example}}
415514

416-
Here are two minimal example programs using the TCP/IP protocol:\ a
515+
Here are four minimal example programs using the TCP/IP protocol:\ a
417516
server that echoes all data that it receives back (servicing only one
418517
client), and a client using it. Note that a server must perform the
419518
sequence \function{socket()}, \method{bind()}, \method{listen()},
@@ -424,6 +523,8 @@ \subsection{Example \label{socket-example}}
424523
socket it is listening on but on the new socket returned by
425524
\method{accept()}.
426525

526+
The first two examples support IPv4 only.
527+
427528
\begin{verbatim}
428529
# Echo server program
429530
import socket
@@ -455,3 +556,79 @@ \subsection{Example \label{socket-example}}
455556
s.close()
456557
print 'Received', `data`
457558
\end{verbatim}
559+
560+
The next two examples are identical to the above two, but support both
561+
IPv4 and IPv6.
562+
The server side will listen to the first address family available
563+
(it should listen to both instead).
564+
On most of IPv6-ready systems, IPv6 will take precedence
565+
and the server may not accept IPv4 traffic.
566+
The client side will try to connect to the all addresses returned as a result
567+
of the name resolution, and sends traffic to the first one connected
568+
successfully.
569+
570+
\begin{verbatim}
571+
# Echo server program
572+
import socket
573+
import sys
574+
575+
HOST = '' # Symbolic name meaning the local host
576+
PORT = 50007 # Arbitrary non-privileged port
577+
s = None
578+
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
579+
af, socktype, proto, canonname, sa = res
580+
try:
581+
s = socket.socket(af, socktype, proto)
582+
except socket.error, msg:
583+
s = None
584+
continue
585+
try:
586+
s.bind(sa)
587+
s.listen(1)
588+
except socket.error, msg:
589+
s.close()
590+
s = None
591+
continue
592+
break
593+
if s is None:
594+
print 'could not open socket'
595+
sys.exit(1)
596+
conn, addr = s.accept()
597+
print 'Connected by', addr
598+
while 1:
599+
data = conn.recv(1024)
600+
if not data: break
601+
conn.send(data)
602+
conn.close()
603+
\end{verbatim}
604+
605+
\begin{verbatim}
606+
# Echo client program
607+
import socket
608+
import sys
609+
610+
HOST = 'daring.cwi.nl' # The remote host
611+
PORT = 50007 # The same port as used by the server
612+
s = None
613+
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
614+
af, socktype, proto, canonname, sa = res
615+
try:
616+
s = socket.socket(af, socktype, proto)
617+
except socket.error, msg:
618+
s = None
619+
continue
620+
try:
621+
s.connect(sa)
622+
except socket.error, msg:
623+
s.close()
624+
s = None
625+
continue
626+
break
627+
if s is None:
628+
print 'could not open socket'
629+
sys.exit(1)
630+
s.send('Hello, world')
631+
data = s.recv(1024)
632+
s.close()
633+
print 'Received', `data`
634+
\end{verbatim}

0 commit comments

Comments
 (0)