blob: 6598bf02c649b4d979a9451be063aa093d5a628d [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{socket} ---
Fred Drake318c0b11999-04-21 17:29:14 +00002 Low-level networking interface}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake318c0b11999-04-21 17:29:14 +00004\declaremodule{builtin}{socket}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Low-level networking interface.}
6
Fred Draked883ca11998-03-10 05:20:33 +00007
Fred Drakeaf8a0151998-01-14 14:51:31 +00008This module provides access to the BSD \emph{socket} interface.
Fred Drake38e5d272000-04-03 20:13:55 +00009It is available on all modern \UNIX{} systems, Windows, MacOS, BeOS,
10OS/2, and probably additional platforms.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011
12For an introduction to socket programming (in C), see the following
Fred Drake37f15741999-11-10 16:21:37 +000013papers: \citetitle{An Introductory 4.3BSD Interprocess Communication
14Tutorial}, by Stuart Sechrest and \citetitle{An Advanced 4.3BSD
15Interprocess Communication Tutorial}, by Samuel J. Leffler et al,
16both in the \citetitle{\UNIX{} Programmer's Manual, Supplementary Documents 1}
Fred Drake38e5d272000-04-03 20:13:55 +000017(sections PS1:7 and PS1:8). The platform-specific reference material
18for the various socket-related system calls are also a valuable source
19of information on the details of socket semantics. For \UNIX, refer
20to the manual pages; for Windows, see the WinSock (or Winsock 2)
21specification.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022
23The Python interface is a straightforward transliteration of the
24\UNIX{} system call and library interface for sockets to Python's
Fred Draked883ca11998-03-10 05:20:33 +000025object-oriented style: the \function{socket()} function returns a
Fred Drake318c0b11999-04-21 17:29:14 +000026\dfn{socket object}\obindex{socket} whose methods implement the
27various socket system calls. Parameter types are somewhat
28higher-level than in the C interface: as with \method{read()} and
29\method{write()} operations on Python files, buffer allocation on
30receive operations is automatic, and buffer length is implicit on send
31operations.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032
33Socket addresses are represented as a single string for the
Fred Draked883ca11998-03-10 05:20:33 +000034\constant{AF_UNIX} address family and as a pair
35\code{(\var{host}, \var{port})} for the \constant{AF_INET} address
36family, where \var{host} is a string representing
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000037either a hostname in Internet domain notation like
38\code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'},
39and \var{port} is an integral port number. Other address families are
40currently not supported. The address format required by a particular
41socket object is automatically selected based on the address family
42specified when the socket object was created.
43
Guido van Rossume4f347e1997-05-09 02:21:51 +000044For IP addresses, two special forms are accepted instead of a host
Fred Draked883ca11998-03-10 05:20:33 +000045address: the empty string represents \constant{INADDR_ANY}, and the string
Fred Drake318c0b11999-04-21 17:29:14 +000046\code{'<broadcast>'} represents \constant{INADDR_BROADCAST}.
Guido van Rossume4f347e1997-05-09 02:21:51 +000047
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048All errors raise exceptions. The normal exceptions for invalid
49argument types and out-of-memory conditions can be raised; errors
Fred Drake318c0b11999-04-21 17:29:14 +000050related to socket or address semantics raise the error
51\exception{socket.error}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052
Fred Drake318c0b11999-04-21 17:29:14 +000053Non-blocking mode is supported through the
54\method{setblocking()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055
Fred Draked883ca11998-03-10 05:20:33 +000056The module \module{socket} exports the following constants and functions:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000057
Fred Draked883ca11998-03-10 05:20:33 +000058
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059\begin{excdesc}{error}
60This exception is raised for socket- or address-related errors.
61The accompanying value is either a string telling what went wrong or a
62pair \code{(\var{errno}, \var{string})}
63representing an error returned by a system
Fred Drake318c0b11999-04-21 17:29:14 +000064call, similar to the value accompanying \exception{os.error}.
65See the module \refmodule{errno}\refbimodindex{errno}, which contains
Guido van Rossum8e1e68d1998-02-06 15:18:25 +000066names for the error codes defined by the underlying operating system.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067\end{excdesc}
68
69\begin{datadesc}{AF_UNIX}
70\dataline{AF_INET}
71These constants represent the address (and protocol) families,
Fred Draked883ca11998-03-10 05:20:33 +000072used for the first argument to \function{socket()}. If the
73\constant{AF_UNIX} constant is not defined then this protocol is
74unsupported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075\end{datadesc}
76
77\begin{datadesc}{SOCK_STREAM}
78\dataline{SOCK_DGRAM}
Guido van Rossum781db5d1994-08-05 13:37:36 +000079\dataline{SOCK_RAW}
80\dataline{SOCK_RDM}
81\dataline{SOCK_SEQPACKET}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082These constants represent the socket types,
Fred Draked883ca11998-03-10 05:20:33 +000083used for the second argument to \function{socket()}.
84(Only \constant{SOCK_STREAM} and
85\constant{SOCK_DGRAM} appear to be generally useful.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000086\end{datadesc}
87
Guido van Rossumed2bad81995-02-16 16:29:18 +000088\begin{datadesc}{SO_*}
89\dataline{SOMAXCONN}
90\dataline{MSG_*}
91\dataline{SOL_*}
92\dataline{IPPROTO_*}
93\dataline{IPPORT_*}
94\dataline{INADDR_*}
95\dataline{IP_*}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000096Many constants of these forms, documented in the \UNIX{} documentation on
Guido van Rossumed2bad81995-02-16 16:29:18 +000097sockets and/or the IP protocol, are also defined in the socket module.
Fred Draked883ca11998-03-10 05:20:33 +000098They are generally used in arguments to the \method{setsockopt()} and
99\method{getsockopt()} methods of socket objects. In most cases, only
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000100those symbols that are defined in the \UNIX{} header files are defined;
Guido van Rossumed2bad81995-02-16 16:29:18 +0000101for a few symbols, default values are provided.
102\end{datadesc}
103
Fred Drake5772c862000-08-16 14:21:42 +0000104\begin{funcdesc}{getfqdn}{\optional{name}}
105Return a fully qualified domain name for \var{name}.
106If \var{name} is omitted or empty, it is interpreted as the local
107host. To find the fully qualified name, the hostname returned by
108\function{gethostbyaddr()} is checked, then aliases for the host, if
109available. The first name which includes a period is selected. In
110case no fully qualified domain name is available, the hostname is
111returned.
Fred Drake8b2e8f82000-09-06 02:22:16 +0000112\versionadded{2.0}
Fred Drake5772c862000-08-16 14:21:42 +0000113\end{funcdesc}
114
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000115\begin{funcdesc}{gethostbyname}{hostname}
116Translate a host name to IP address format. The IP address is
117returned as a string, e.g., \code{'100.50.200.5'}. If the host name
Guido van Rossumcdf6af11998-08-07 18:07:36 +0000118is an IP address itself it is returned unchanged. See
Fred Drake318c0b11999-04-21 17:29:14 +0000119\function{gethostbyname_ex()} for a more complete interface.
Guido van Rossumcdf6af11998-08-07 18:07:36 +0000120\end{funcdesc}
121
122\begin{funcdesc}{gethostbyname_ex}{hostname}
123Translate a host name to IP address format, extended interface.
124Return a triple \code{(hostname, aliaslist, ipaddrlist)} where
125\code{hostname} is the primary host name responding to the given
126\var{ip_address}, \code{aliaslist} is a (possibly empty) list of
127alternative host names for the same address, and \code{ipaddrlist} is
128a list of IP addresses for the same interface on the same
129host (often but not always a single address).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000130\end{funcdesc}
131
Guido van Rossum781db5d1994-08-05 13:37:36 +0000132\begin{funcdesc}{gethostname}{}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000133Return a string containing the hostname of the machine where
134the Python interpreter is currently executing. If you want to know the
Fred Draked883ca11998-03-10 05:20:33 +0000135current machine's IP address, use \code{gethostbyname(gethostname())}.
136Note: \function{gethostname()} doesn't always return the fully qualified
137domain name; use \code{gethostbyaddr(gethostname())}
Guido van Rossumfe27a501997-01-11 17:04:56 +0000138(see below).
Guido van Rossum31cce971995-01-04 19:17:34 +0000139\end{funcdesc}
140
141\begin{funcdesc}{gethostbyaddr}{ip_address}
Fred Draked883ca11998-03-10 05:20:33 +0000142Return a triple \code{(\var{hostname}, \var{aliaslist},
143\var{ipaddrlist})} where \var{hostname} is the primary host name
144responding to the given \var{ip_address}, \var{aliaslist} is a
145(possibly empty) list of alternative host names for the same address,
146and \var{ipaddrlist} is a list of IP addresses for the same interface
147on the same host (most likely containing only a single address).
Fred Drake5772c862000-08-16 14:21:42 +0000148To find the fully qualified domain name, use the function
149\function{getfqdn()}.
Guido van Rossum781db5d1994-08-05 13:37:36 +0000150\end{funcdesc}
151
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000152\begin{funcdesc}{getprotobyname}{protocolname}
Fred Drake318c0b11999-04-21 17:29:14 +0000153Translate an Internet protocol name (e.g.\ \code{'icmp'}) to a constant
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000154suitable for passing as the (optional) third argument to the
Fred Draked883ca11998-03-10 05:20:33 +0000155\function{socket()} function. This is usually only needed for sockets
156opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket
157modes, the correct protocol is chosen automatically if the protocol is
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000158omitted or zero.
159\end{funcdesc}
160
Fred Draked883ca11998-03-10 05:20:33 +0000161\begin{funcdesc}{getservbyname}{servicename, protocolname}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000162Translate an Internet service name and protocol name to a port number
163for that service. The protocol name should be \code{'tcp'} or
164\code{'udp'}.
165\end{funcdesc}
166
Fred Draked883ca11998-03-10 05:20:33 +0000167\begin{funcdesc}{socket}{family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000168Create a new socket using the given address family, socket type and
Fred Draked883ca11998-03-10 05:20:33 +0000169protocol number. The address family should be \constant{AF_INET} or
170\constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM},
171\constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000172The protocol number is usually zero and may be omitted in that case.
173\end{funcdesc}
174
Fred Draked883ca11998-03-10 05:20:33 +0000175\begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000176Build a socket object from an existing file descriptor (an integer as
Fred Draked883ca11998-03-10 05:20:33 +0000177returned by a file object's \method{fileno()} method). Address family,
Fred Drake318c0b11999-04-21 17:29:14 +0000178socket type and protocol number are as for the \function{socket()} function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000179above. The file descriptor should refer to a socket, but this is not
180checked --- subsequent operations on the object may fail if the file
181descriptor is invalid. This function is rarely needed, but can be
182used to get or set socket options on a socket passed to a program as
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000183standard input or output (e.g.\ a server started by the \UNIX{} inet
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000184daemon).
185\end{funcdesc}
186
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000187\begin{funcdesc}{ntohl}{x}
Fred Drakec5aec051997-12-08 21:25:41 +0000188Convert 32-bit integers from network to host byte order. On machines
189where the host byte order is the same as network byte order, this is a
190no-op; otherwise, it performs a 4-byte swap operation.
191\end{funcdesc}
192
193\begin{funcdesc}{ntohs}{x}
194Convert 16-bit integers from network to host byte order. On machines
195where the host byte order is the same as network byte order, this is a
196no-op; otherwise, it performs a 2-byte swap operation.
197\end{funcdesc}
198
199\begin{funcdesc}{htonl}{x}
200Convert 32-bit integers from host to network byte order. On machines
201where the host byte order is the same as network byte order, this is a
202no-op; otherwise, it performs a 4-byte swap operation.
203\end{funcdesc}
204
205\begin{funcdesc}{htons}{x}
206Convert 16-bit integers from host to network byte order. On machines
207where the host byte order is the same as network byte order, this is a
208no-op; otherwise, it performs a 2-byte swap operation.
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000209\end{funcdesc}
210
Fred Drakee6fb1c41999-09-16 15:50:00 +0000211\begin{funcdesc}{inet_aton}{ip_string}
212Convert an IP address from dotted-quad string format
213(e.g.\ '123.45.67.89') to 32-bit packed binary format, as a string four
214characters in length.
215
216Useful when conversing with a program that uses the standard C library
217and needs objects of type \ctype{struct in_addr}, which is the C type
218for the 32-bit packed binary this function returns.
219
220If the IP address string passed to this function is invalid,
221\exception{socket.error} will be raised. Note that exactly what is
222valid depends on the underlying C implementation of
223\cfunction{inet_aton()}.
224\end{funcdesc}
225
226\begin{funcdesc}{inet_ntoa}{packed_ip}
227Convert a 32-bit packed IP address (a string four characters in
228length) to its standard dotted-quad string representation
229(e.g. '123.45.67.89').
230
231Useful when conversing with a program that uses the standard C library
232and needs objects of type \ctype{struct in_addr}, which is the C type
233for the 32-bit packed binary this function takes as an argument.
234
235If the string passed to this function is not exactly 4 bytes in
236length, \exception{socket.error} will be raised.
237\end{funcdesc}
238
Fred Drake5451d671997-10-13 21:31:02 +0000239\begin{datadesc}{SocketType}
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000240This is a Python type object that represents the socket object type.
Fred Draked883ca11998-03-10 05:20:33 +0000241It is the same as \code{type(socket(...))}.
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000242\end{datadesc}
243
Fred Drakeaa7524c2000-07-06 18:37:08 +0000244
245\begin{seealso}
246 \seemodule{SocketServer}{Classes that simplify writing network servers.}
247\end{seealso}
248
249
Fred Drakea94f6761999-08-05 13:41:04 +0000250\subsection{Socket Objects \label{socket-objects}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000251
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000252Socket objects have the following methods. Except for
Fred Draked883ca11998-03-10 05:20:33 +0000253\method{makefile()} these correspond to \UNIX{} system calls
254applicable to sockets.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000255
Fred Drake3f1c4721998-04-03 07:04:45 +0000256\begin{methoddesc}[socket]{accept}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000257Accept a connection.
258The socket must be bound to an address and listening for connections.
259The return value is a pair \code{(\var{conn}, \var{address})}
260where \var{conn} is a \emph{new} socket object usable to send and
261receive data on the connection, and \var{address} is the address bound
262to the socket on the other end of the connection.
Fred Drake3f1c4721998-04-03 07:04:45 +0000263\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000264
Fred Drake3f1c4721998-04-03 07:04:45 +0000265\begin{methoddesc}[socket]{bind}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000266Bind the socket to \var{address}. The socket must not already be bound.
Fred Drake7d686902000-04-04 17:48:30 +0000267(The format of \var{address} depends on the address family --- see
268above.) \strong{Note:} This method has historically accepted a pair
269of parameters for \constant{AF_INET} addresses instead of only a
Eric S. Raymond83210262001-01-10 19:34:52 +0000270tuple. This was never intentional and is no longer be available in
271Python 2.0.
Fred Drake3f1c4721998-04-03 07:04:45 +0000272\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000273
Fred Drake3f1c4721998-04-03 07:04:45 +0000274\begin{methoddesc}[socket]{close}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000275Close the socket. All future operations on the socket object will fail.
276The remote end will receive no more data (after queued data is flushed).
277Sockets are automatically closed when they are garbage-collected.
Fred Drake3f1c4721998-04-03 07:04:45 +0000278\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000279
Fred Drake3f1c4721998-04-03 07:04:45 +0000280\begin{methoddesc}[socket]{connect}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000281Connect to a remote socket at \var{address}.
Fred Draked883ca11998-03-10 05:20:33 +0000282(The format of \var{address} depends on the address family --- see
Fred Drake7d686902000-04-04 17:48:30 +0000283above.) \strong{Note:} This method has historically accepted a pair
284of parameters for \constant{AF_INET} addresses instead of only a
Eric S. Raymond83210262001-01-10 19:34:52 +0000285tuple. This was never intentional and is no longer available in
286Python 2.0 and later.
Fred Drake3f1c4721998-04-03 07:04:45 +0000287\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000288
Fred Drake3f1c4721998-04-03 07:04:45 +0000289\begin{methoddesc}[socket]{connect_ex}{address}
Guido van Rossumeefcba61997-12-09 19:47:24 +0000290Like \code{connect(\var{address})}, but return an error indicator
Fred Drakeb0bc7f21999-05-06 22:03:50 +0000291instead of raising an exception for errors returned by the C-level
292\cfunction{connect()} call (other problems, such as ``host not found,''
293can still raise exceptions). The error indicator is \code{0} if the
Fred Draked883ca11998-03-10 05:20:33 +0000294operation succeeded, otherwise the value of the \cdata{errno}
Fred Drake3f1c4721998-04-03 07:04:45 +0000295variable. This is useful, e.g., for asynchronous connects.
Fred Drake7d686902000-04-04 17:48:30 +0000296\strong{Note:} This method has historically accepted a pair of
297parameters for \constant{AF_INET} addresses instead of only a tuple.
Eric S. Raymond83210262001-01-10 19:34:52 +0000298This was never intentional and is no longer be available in Python
2992.0 and later.
Fred Drake3f1c4721998-04-03 07:04:45 +0000300\end{methoddesc}
Guido van Rossumf7790c61997-11-18 15:29:20 +0000301
Fred Drake3f1c4721998-04-03 07:04:45 +0000302\begin{methoddesc}[socket]{fileno}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000303Return the socket's file descriptor (a small integer). This is useful
Fred Draked883ca11998-03-10 05:20:33 +0000304with \function{select.select()}.
Fred Drake3f1c4721998-04-03 07:04:45 +0000305\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000306
Fred Drake3f1c4721998-04-03 07:04:45 +0000307\begin{methoddesc}[socket]{getpeername}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000308Return the remote address to which the socket is connected. This is
309useful to find out the port number of a remote IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000310(The format of the address returned depends on the address family ---
Guido van Rossum781db5d1994-08-05 13:37:36 +0000311see above.) On some systems this function is not supported.
Fred Drake3f1c4721998-04-03 07:04:45 +0000312\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000313
Fred Drake3f1c4721998-04-03 07:04:45 +0000314\begin{methoddesc}[socket]{getsockname}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000315Return the socket's own address. This is useful to find out the port
316number of an IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000317(The format of the address returned depends on the address family ---
Guido van Rossuma84ec511994-06-23 12:13:52 +0000318see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000319\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000320
Fred Drake3f1c4721998-04-03 07:04:45 +0000321\begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000322Return the value of the given socket option (see the \UNIX{} man page
Fred Draked883ca11998-03-10 05:20:33 +0000323\manpage{getsockopt}{2}). The needed symbolic constants
324(\constant{SO_*} etc.) are defined in this module. If \var{buflen}
Guido van Rossum470be141995-03-17 16:07:09 +0000325is absent, an integer option is assumed and its integer value
Guido van Rossum8df36371995-02-27 17:52:15 +0000326is returned by the function. If \var{buflen} is present, it specifies
327the maximum length of the buffer used to receive the option in, and
Guido van Rossum470be141995-03-17 16:07:09 +0000328this buffer is returned as a string. It is up to the caller to decode
Guido van Rossum8df36371995-02-27 17:52:15 +0000329the contents of the buffer (see the optional built-in module
Fred Drake318c0b11999-04-21 17:29:14 +0000330\refmodule{struct} for a way to decode C structures encoded as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000331\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000332
Fred Drake3f1c4721998-04-03 07:04:45 +0000333\begin{methoddesc}[socket]{listen}{backlog}
Guido van Rossum470be141995-03-17 16:07:09 +0000334Listen for connections made to the socket. The \var{backlog} argument
335specifies the maximum number of queued connections and should be at
336least 1; the maximum value is system-dependent (usually 5).
Fred Drake3f1c4721998-04-03 07:04:45 +0000337\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000338
Fred Drake3f1c4721998-04-03 07:04:45 +0000339\begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}}
Guido van Rossum470be141995-03-17 16:07:09 +0000340Return a \dfn{file object} associated with the socket. (File objects
Fred Drakea94f6761999-08-05 13:41:04 +0000341are described in \ref{bltin-file-objects}, ``File Objects.'')
Fred Draked883ca11998-03-10 05:20:33 +0000342The file object references a \cfunction{dup()}ped version of the
343socket file descriptor, so the file object and socket object may be
Fred Drakea94f6761999-08-05 13:41:04 +0000344closed or garbage-collected independently.
345\index{I/O control!buffering}The optional \var{mode}
Fred Draked883ca11998-03-10 05:20:33 +0000346and \var{bufsize} arguments are interpreted the same way as by the
347built-in \function{open()} function.
Fred Drake3f1c4721998-04-03 07:04:45 +0000348\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000349
Fred Drake3f1c4721998-04-03 07:04:45 +0000350\begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000351Receive data from the socket. The return value is a string representing
352the data received. The maximum amount of data to be received
353at once is specified by \var{bufsize}. See the \UNIX{} manual page
Fred Draked883ca11998-03-10 05:20:33 +0000354\manpage{recv}{2} for the meaning of the optional argument
355\var{flags}; it defaults to zero.
Fred Drake3f1c4721998-04-03 07:04:45 +0000356\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000357
Fred Drake3f1c4721998-04-03 07:04:45 +0000358\begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000359Receive data from the socket. The return value is a pair
360\code{(\var{string}, \var{address})} where \var{string} is a string
361representing the data received and \var{address} is the address of the
Guido van Rossum470be141995-03-17 16:07:09 +0000362socket sending the data. The optional \var{flags} argument has the
Fred Draked883ca11998-03-10 05:20:33 +0000363same meaning as for \method{recv()} above.
Guido van Rossum86751151995-02-28 17:14:32 +0000364(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000365\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000366
Fred Drake3f1c4721998-04-03 07:04:45 +0000367\begin{methoddesc}[socket]{send}{string\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000368Send data to the socket. The socket must be connected to a remote
Guido van Rossum470be141995-03-17 16:07:09 +0000369socket. The optional \var{flags} argument has the same meaning as for
Fred Draked883ca11998-03-10 05:20:33 +0000370\method{recv()} above. Returns the number of bytes sent.
Fred Drake3f1c4721998-04-03 07:04:45 +0000371\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000372
Fred Drake3f1c4721998-04-03 07:04:45 +0000373\begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000374Send data to the socket. The socket should not be connected to a
375remote socket, since the destination socket is specified by
Fred Draked883ca11998-03-10 05:20:33 +0000376\var{address}. The optional \var{flags} argument has the same
377meaning as for \method{recv()} above. Return the number of bytes sent.
Guido van Rossum86751151995-02-28 17:14:32 +0000378(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000379\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000380
Fred Drake3f1c4721998-04-03 07:04:45 +0000381\begin{methoddesc}[socket]{setblocking}{flag}
Guido van Rossum91951481994-09-07 14:39:14 +0000382Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
383the socket is set to non-blocking, else to blocking mode. Initially
384all sockets are in blocking mode. In non-blocking mode, if a
Fred Drake318c0b11999-04-21 17:29:14 +0000385\method{recv()} call doesn't find any data, or if a
386\method{send()} call can't immediately dispose of the data, a
387\exception{error} exception is raised; in blocking mode, the calls
388block until they can proceed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000389\end{methoddesc}
Guido van Rossum91951481994-09-07 14:39:14 +0000390
Fred Drake3f1c4721998-04-03 07:04:45 +0000391\begin{methoddesc}[socket]{setsockopt}{level, optname, value}
Fred Drake9a748aa2000-06-30 04:21:41 +0000392Set the value of the given socket option (see the \UNIX{} manual page
Fred Draked883ca11998-03-10 05:20:33 +0000393\manpage{setsockopt}{2}). The needed symbolic constants are defined in
394the \module{socket} module (\code{SO_*} etc.). The value can be an
Guido van Rossum8df36371995-02-27 17:52:15 +0000395integer or a string representing a buffer. In the latter case it is
396up to the caller to ensure that the string contains the proper bits
397(see the optional built-in module
Fred Drake318c0b11999-04-21 17:29:14 +0000398\refmodule{struct}\refbimodindex{struct} for a way to encode C
399structures as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000400\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000401
Fred Drake3f1c4721998-04-03 07:04:45 +0000402\begin{methoddesc}[socket]{shutdown}{how}
Fred Draked883ca11998-03-10 05:20:33 +0000403Shut down one or both halves of the connection. If \var{how} is
404\code{0}, further receives are disallowed. If \var{how} is \code{1},
405further sends are disallowed. If \var{how} is \code{2}, further sends
406and receives are disallowed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000407\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000408
Fred Draked883ca11998-03-10 05:20:33 +0000409Note that there are no methods \method{read()} or \method{write()};
410use \method{recv()} and \method{send()} without \var{flags} argument
411instead.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000412
Fred Drakeaa7524c2000-07-06 18:37:08 +0000413
414\subsection{Example \label{socket-example}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000415
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000416Here are two minimal example programs using the TCP/IP protocol:\ a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000417server that echoes all data that it receives back (servicing only one
418client), and a client using it. Note that a server must perform the
Fred Draked883ca11998-03-10 05:20:33 +0000419sequence \function{socket()}, \method{bind()}, \method{listen()},
420\method{accept()} (possibly repeating the \method{accept()} to service
421more than one client), while a client only needs the sequence
422\function{socket()}, \method{connect()}. Also note that the server
423does not \method{send()}/\method{recv()} on the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000424socket it is listening on but on the new socket returned by
Fred Draked883ca11998-03-10 05:20:33 +0000425\method{accept()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000426
Fred Drake19479911998-02-13 06:58:54 +0000427\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000428# Echo server program
Fred Drakeef52f602000-10-10 20:36:29 +0000429import socket
430
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000431HOST = '' # Symbolic name meaning the local host
Fred Drakeef52f602000-10-10 20:36:29 +0000432PORT = 50007 # Arbitrary non-privileged port
433s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Fred Drake3d69c0e2000-05-03 19:40:32 +0000434s.bind((HOST, PORT))
Guido van Rossum5da57551994-03-02 10:52:16 +0000435s.listen(1)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000436conn, addr = s.accept()
437print 'Connected by', addr
438while 1:
439 data = conn.recv(1024)
440 if not data: break
441 conn.send(data)
442conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000443\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000444
Fred Drake19479911998-02-13 06:58:54 +0000445\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000446# Echo client program
Fred Drakeef52f602000-10-10 20:36:29 +0000447import socket
448
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000449HOST = 'daring.cwi.nl' # The remote host
450PORT = 50007 # The same port as used by the server
Fred Drakeef52f602000-10-10 20:36:29 +0000451s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Fred Drake3d69c0e2000-05-03 19:40:32 +0000452s.connect((HOST, PORT))
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000453s.send('Hello, world')
454data = s.recv(1024)
455s.close()
456print 'Received', `data`
Fred Drake19479911998-02-13 06:58:54 +0000457\end{verbatim}