blob: c6476b9b962ff6f69f4057a6e23a046d9bc6f13f [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{socket} ---
2 Low-level networking interface.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{socket}
4
5\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.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00009It is available on \UNIX{} systems that support this interface.
10
11For an introduction to socket programming (in C), see the following
12papers: \emph{An Introductory 4.3BSD Interprocess Communication
13Tutorial}, by Stuart Sechrest and \emph{An Advanced 4.3BSD Interprocess
14Communication Tutorial}, by Samuel J. Leffler et al, both in the
15\UNIX{} Programmer's Manual, Supplementary Documents 1 (sections PS1:7
16and PS1:8). The \UNIX{} manual pages for the various socket-related
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000017system calls are also a valuable source of information on the details of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000018socket semantics.
19
20The Python interface is a straightforward transliteration of the
21\UNIX{} system call and library interface for sockets to Python's
Fred Draked883ca11998-03-10 05:20:33 +000022object-oriented style: the \function{socket()} function returns a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023\dfn{socket object} whose methods implement the various socket system
Barry Warsawd44be3f1997-01-03 20:19:05 +000024calls. Parameter types are somewhat higher-level than in the C
Fred Draked883ca11998-03-10 05:20:33 +000025interface: as with \method{read()} and \method{write()} operations on
26Python files, buffer allocation on receive operations is automatic,
27and buffer length is implicit on send operations.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000028
29Socket addresses are represented as a single string for the
Fred Draked883ca11998-03-10 05:20:33 +000030\constant{AF_UNIX} address family and as a pair
31\code{(\var{host}, \var{port})} for the \constant{AF_INET} address
32family, where \var{host} is a string representing
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033either a hostname in Internet domain notation like
34\code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'},
35and \var{port} is an integral port number. Other address families are
36currently not supported. The address format required by a particular
37socket object is automatically selected based on the address family
38specified when the socket object was created.
39
Guido van Rossume4f347e1997-05-09 02:21:51 +000040For IP addresses, two special forms are accepted instead of a host
Fred Draked883ca11998-03-10 05:20:33 +000041address: the empty string represents \constant{INADDR_ANY}, and the string
42\code{"<broadcast>"} represents \constant{INADDR_BROADCAST}.
Guido van Rossume4f347e1997-05-09 02:21:51 +000043
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000044All errors raise exceptions. The normal exceptions for invalid
45argument types and out-of-memory conditions can be raised; errors
46related to socket or address semantics raise the error \code{socket.error}.
47
Guido van Rossum470be141995-03-17 16:07:09 +000048Non-blocking mode is supported through the \code{setblocking()}
49method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000050
Fred Draked883ca11998-03-10 05:20:33 +000051The module \module{socket} exports the following constants and functions:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052
Fred Draked883ca11998-03-10 05:20:33 +000053
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054\begin{excdesc}{error}
55This exception is raised for socket- or address-related errors.
56The accompanying value is either a string telling what went wrong or a
57pair \code{(\var{errno}, \var{string})}
58representing an error returned by a system
Guido van Rossum8e1e68d1998-02-06 15:18:25 +000059call, similar to the value accompanying \code{os.error}.
60See the module \module{errno}\refbimodindex{errno}, which contains
61names for the error codes defined by the underlying operating system.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062\end{excdesc}
63
64\begin{datadesc}{AF_UNIX}
65\dataline{AF_INET}
66These constants represent the address (and protocol) families,
Fred Draked883ca11998-03-10 05:20:33 +000067used for the first argument to \function{socket()}. If the
68\constant{AF_UNIX} constant is not defined then this protocol is
69unsupported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000070\end{datadesc}
71
72\begin{datadesc}{SOCK_STREAM}
73\dataline{SOCK_DGRAM}
Guido van Rossum781db5d1994-08-05 13:37:36 +000074\dataline{SOCK_RAW}
75\dataline{SOCK_RDM}
76\dataline{SOCK_SEQPACKET}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000077These constants represent the socket types,
Fred Draked883ca11998-03-10 05:20:33 +000078used for the second argument to \function{socket()}.
79(Only \constant{SOCK_STREAM} and
80\constant{SOCK_DGRAM} appear to be generally useful.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081\end{datadesc}
82
Guido van Rossumed2bad81995-02-16 16:29:18 +000083\begin{datadesc}{SO_*}
84\dataline{SOMAXCONN}
85\dataline{MSG_*}
86\dataline{SOL_*}
87\dataline{IPPROTO_*}
88\dataline{IPPORT_*}
89\dataline{INADDR_*}
90\dataline{IP_*}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000091Many constants of these forms, documented in the \UNIX{} documentation on
Guido van Rossumed2bad81995-02-16 16:29:18 +000092sockets and/or the IP protocol, are also defined in the socket module.
Fred Draked883ca11998-03-10 05:20:33 +000093They are generally used in arguments to the \method{setsockopt()} and
94\method{getsockopt()} methods of socket objects. In most cases, only
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000095those symbols that are defined in the \UNIX{} header files are defined;
Guido van Rossumed2bad81995-02-16 16:29:18 +000096for a few symbols, default values are provided.
97\end{datadesc}
98
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000099\begin{funcdesc}{gethostbyname}{hostname}
100Translate a host name to IP address format. The IP address is
101returned as a string, e.g., \code{'100.50.200.5'}. If the host name
Guido van Rossumcdf6af11998-08-07 18:07:36 +0000102is an IP address itself it is returned unchanged. See
103\code{gethostbyname_ex} for a more complete interface.
104\end{funcdesc}
105
106\begin{funcdesc}{gethostbyname_ex}{hostname}
107Translate a host name to IP address format, extended interface.
108Return a triple \code{(hostname, aliaslist, ipaddrlist)} where
109\code{hostname} is the primary host name responding to the given
110\var{ip_address}, \code{aliaslist} is a (possibly empty) list of
111alternative host names for the same address, and \code{ipaddrlist} is
112a list of IP addresses for the same interface on the same
113host (often but not always a single address).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000114\end{funcdesc}
115
Guido van Rossum781db5d1994-08-05 13:37:36 +0000116\begin{funcdesc}{gethostname}{}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000117Return a string containing the hostname of the machine where
118the Python interpreter is currently executing. If you want to know the
Fred Draked883ca11998-03-10 05:20:33 +0000119current machine's IP address, use \code{gethostbyname(gethostname())}.
120Note: \function{gethostname()} doesn't always return the fully qualified
121domain name; use \code{gethostbyaddr(gethostname())}
Guido van Rossumfe27a501997-01-11 17:04:56 +0000122(see below).
Guido van Rossum31cce971995-01-04 19:17:34 +0000123\end{funcdesc}
124
125\begin{funcdesc}{gethostbyaddr}{ip_address}
Fred Draked883ca11998-03-10 05:20:33 +0000126Return a triple \code{(\var{hostname}, \var{aliaslist},
127\var{ipaddrlist})} where \var{hostname} is the primary host name
128responding to the given \var{ip_address}, \var{aliaslist} is a
129(possibly empty) list of alternative host names for the same address,
130and \var{ipaddrlist} is a list of IP addresses for the same interface
131on the same host (most likely containing only a single address).
Guido van Rossumfe27a501997-01-11 17:04:56 +0000132To find the fully qualified domain name, check \var{hostname} and the
133items of \var{aliaslist} for an entry containing at least one period.
Guido van Rossum781db5d1994-08-05 13:37:36 +0000134\end{funcdesc}
135
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000136\begin{funcdesc}{getprotobyname}{protocolname}
137Translate an Internet protocol name (e.g. \code{'icmp'}) to a constant
138suitable for passing as the (optional) third argument to the
Fred Draked883ca11998-03-10 05:20:33 +0000139\function{socket()} function. This is usually only needed for sockets
140opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket
141modes, the correct protocol is chosen automatically if the protocol is
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000142omitted or zero.
143\end{funcdesc}
144
Fred Draked883ca11998-03-10 05:20:33 +0000145\begin{funcdesc}{getservbyname}{servicename, protocolname}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000146Translate an Internet service name and protocol name to a port number
147for that service. The protocol name should be \code{'tcp'} or
148\code{'udp'}.
149\end{funcdesc}
150
Fred Draked883ca11998-03-10 05:20:33 +0000151\begin{funcdesc}{socket}{family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000152Create a new socket using the given address family, socket type and
Fred Draked883ca11998-03-10 05:20:33 +0000153protocol number. The address family should be \constant{AF_INET} or
154\constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM},
155\constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000156The protocol number is usually zero and may be omitted in that case.
157\end{funcdesc}
158
Fred Draked883ca11998-03-10 05:20:33 +0000159\begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000160Build a socket object from an existing file descriptor (an integer as
Fred Draked883ca11998-03-10 05:20:33 +0000161returned by a file object's \method{fileno()} method). Address family,
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000162socket type and protocol number are as for the \code{socket} function
163above. The file descriptor should refer to a socket, but this is not
164checked --- subsequent operations on the object may fail if the file
165descriptor is invalid. This function is rarely needed, but can be
166used to get or set socket options on a socket passed to a program as
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000167standard input or output (e.g.\ a server started by the \UNIX{} inet
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000168daemon).
169\end{funcdesc}
170
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000171\begin{funcdesc}{ntohl}{x}
Fred Drakec5aec051997-12-08 21:25:41 +0000172Convert 32-bit integers from network to host byte order. On machines
173where the host byte order is the same as network byte order, this is a
174no-op; otherwise, it performs a 4-byte swap operation.
175\end{funcdesc}
176
177\begin{funcdesc}{ntohs}{x}
178Convert 16-bit integers from network to host byte order. On machines
179where the host byte order is the same as network byte order, this is a
180no-op; otherwise, it performs a 2-byte swap operation.
181\end{funcdesc}
182
183\begin{funcdesc}{htonl}{x}
184Convert 32-bit integers from host to network byte order. On machines
185where the host byte order is the same as network byte order, this is a
186no-op; otherwise, it performs a 4-byte swap operation.
187\end{funcdesc}
188
189\begin{funcdesc}{htons}{x}
190Convert 16-bit integers from host to network byte order. On machines
191where the host byte order is the same as network byte order, this is a
192no-op; otherwise, it performs a 2-byte swap operation.
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000193\end{funcdesc}
194
Fred Drake5451d671997-10-13 21:31:02 +0000195\begin{datadesc}{SocketType}
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000196This is a Python type object that represents the socket object type.
Fred Draked883ca11998-03-10 05:20:33 +0000197It is the same as \code{type(socket(...))}.
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000198\end{datadesc}
199
Guido van Rossum470be141995-03-17 16:07:09 +0000200\subsection{Socket Objects}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000201
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000202Socket objects have the following methods. Except for
Fred Draked883ca11998-03-10 05:20:33 +0000203\method{makefile()} these correspond to \UNIX{} system calls
204applicable to sockets.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000205
Fred Drake3f1c4721998-04-03 07:04:45 +0000206\begin{methoddesc}[socket]{accept}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000207Accept a connection.
208The socket must be bound to an address and listening for connections.
209The return value is a pair \code{(\var{conn}, \var{address})}
210where \var{conn} is a \emph{new} socket object usable to send and
211receive data on the connection, and \var{address} is the address bound
212to the socket on the other end of the connection.
Fred Drake3f1c4721998-04-03 07:04:45 +0000213\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000214
Fred Drake3f1c4721998-04-03 07:04:45 +0000215\begin{methoddesc}[socket]{bind}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000216Bind the socket to \var{address}. The socket must not already be bound.
Guido van Rossum86751151995-02-28 17:14:32 +0000217(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000218\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000219
Fred Drake3f1c4721998-04-03 07:04:45 +0000220\begin{methoddesc}[socket]{close}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000221Close the socket. All future operations on the socket object will fail.
222The remote end will receive no more data (after queued data is flushed).
223Sockets are automatically closed when they are garbage-collected.
Fred Drake3f1c4721998-04-03 07:04:45 +0000224\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000225
Fred Drake3f1c4721998-04-03 07:04:45 +0000226\begin{methoddesc}[socket]{connect}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000227Connect to a remote socket at \var{address}.
Fred Draked883ca11998-03-10 05:20:33 +0000228(The format of \var{address} depends on the address family --- see
229above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000230\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000231
Fred Drake3f1c4721998-04-03 07:04:45 +0000232\begin{methoddesc}[socket]{connect_ex}{address}
Guido van Rossumeefcba61997-12-09 19:47:24 +0000233Like \code{connect(\var{address})}, but return an error indicator
Guido van Rossumf7790c61997-11-18 15:29:20 +0000234instead of raising an exception. The error indicator is 0 if the
Fred Draked883ca11998-03-10 05:20:33 +0000235operation succeeded, otherwise the value of the \cdata{errno}
Fred Drake3f1c4721998-04-03 07:04:45 +0000236variable. This is useful, e.g., for asynchronous connects.
237\end{methoddesc}
Guido van Rossumf7790c61997-11-18 15:29:20 +0000238
Fred Drake3f1c4721998-04-03 07:04:45 +0000239\begin{methoddesc}[socket]{fileno}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000240Return the socket's file descriptor (a small integer). This is useful
Fred Draked883ca11998-03-10 05:20:33 +0000241with \function{select.select()}.
Fred Drake3f1c4721998-04-03 07:04:45 +0000242\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000243
Fred Drake3f1c4721998-04-03 07:04:45 +0000244\begin{methoddesc}[socket]{getpeername}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000245Return the remote address to which the socket is connected. This is
246useful to find out the port number of a remote IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000247(The format of the address returned depends on the address family ---
Guido van Rossum781db5d1994-08-05 13:37:36 +0000248see above.) On some systems this function is not supported.
Fred Drake3f1c4721998-04-03 07:04:45 +0000249\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000250
Fred Drake3f1c4721998-04-03 07:04:45 +0000251\begin{methoddesc}[socket]{getsockname}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000252Return the socket's own address. This is useful to find out the port
253number of an IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000254(The format of the address returned depends on the address family ---
Guido van Rossuma84ec511994-06-23 12:13:52 +0000255see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000256\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000257
Fred Drake3f1c4721998-04-03 07:04:45 +0000258\begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000259Return the value of the given socket option (see the \UNIX{} man page
Fred Draked883ca11998-03-10 05:20:33 +0000260\manpage{getsockopt}{2}). The needed symbolic constants
261(\constant{SO_*} etc.) are defined in this module. If \var{buflen}
Guido van Rossum470be141995-03-17 16:07:09 +0000262is absent, an integer option is assumed and its integer value
Guido van Rossum8df36371995-02-27 17:52:15 +0000263is returned by the function. If \var{buflen} is present, it specifies
264the maximum length of the buffer used to receive the option in, and
Guido van Rossum470be141995-03-17 16:07:09 +0000265this buffer is returned as a string. It is up to the caller to decode
Guido van Rossum8df36371995-02-27 17:52:15 +0000266the contents of the buffer (see the optional built-in module
Fred Draked883ca11998-03-10 05:20:33 +0000267\module{struct} for a way to decode C structures encoded as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000268\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000269
Fred Drake3f1c4721998-04-03 07:04:45 +0000270\begin{methoddesc}[socket]{listen}{backlog}
Guido van Rossum470be141995-03-17 16:07:09 +0000271Listen for connections made to the socket. The \var{backlog} argument
272specifies the maximum number of queued connections and should be at
273least 1; the maximum value is system-dependent (usually 5).
Fred Drake3f1c4721998-04-03 07:04:45 +0000274\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000275
Fred Drake3f1c4721998-04-03 07:04:45 +0000276\begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}}
Guido van Rossum470be141995-03-17 16:07:09 +0000277Return a \dfn{file object} associated with the socket. (File objects
Fred Draked883ca11998-03-10 05:20:33 +0000278were described earlier in \ref{bltin-file-objects}, ``File Objects.'')
279The file object references a \cfunction{dup()}ped version of the
280socket file descriptor, so the file object and socket object may be
281closed or garbage-collected independently. The optional \var{mode}
282and \var{bufsize} arguments are interpreted the same way as by the
283built-in \function{open()} function.
Fred Drake3f1c4721998-04-03 07:04:45 +0000284\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000285
Fred Drake3f1c4721998-04-03 07:04:45 +0000286\begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000287Receive data from the socket. The return value is a string representing
288the data received. The maximum amount of data to be received
289at once is specified by \var{bufsize}. See the \UNIX{} manual page
Fred Draked883ca11998-03-10 05:20:33 +0000290\manpage{recv}{2} for the meaning of the optional argument
291\var{flags}; it defaults to zero.
Fred Drake3f1c4721998-04-03 07:04:45 +0000292\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000293
Fred Drake3f1c4721998-04-03 07:04:45 +0000294\begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000295Receive data from the socket. The return value is a pair
296\code{(\var{string}, \var{address})} where \var{string} is a string
297representing the data received and \var{address} is the address of the
Guido van Rossum470be141995-03-17 16:07:09 +0000298socket sending the data. The optional \var{flags} argument has the
Fred Draked883ca11998-03-10 05:20:33 +0000299same meaning as for \method{recv()} above.
Guido van Rossum86751151995-02-28 17:14:32 +0000300(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000301\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000302
Fred Drake3f1c4721998-04-03 07:04:45 +0000303\begin{methoddesc}[socket]{send}{string\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000304Send data to the socket. The socket must be connected to a remote
Guido van Rossum470be141995-03-17 16:07:09 +0000305socket. The optional \var{flags} argument has the same meaning as for
Fred Draked883ca11998-03-10 05:20:33 +0000306\method{recv()} above. Returns the number of bytes sent.
Fred Drake3f1c4721998-04-03 07:04:45 +0000307\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000308
Fred Drake3f1c4721998-04-03 07:04:45 +0000309\begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000310Send data to the socket. The socket should not be connected to a
311remote socket, since the destination socket is specified by
Fred Draked883ca11998-03-10 05:20:33 +0000312\var{address}. The optional \var{flags} argument has the same
313meaning as for \method{recv()} above. Return the number of bytes sent.
Guido van Rossum86751151995-02-28 17:14:32 +0000314(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000315\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000316
Fred Drake3f1c4721998-04-03 07:04:45 +0000317\begin{methoddesc}[socket]{setblocking}{flag}
Guido van Rossum91951481994-09-07 14:39:14 +0000318Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
319the socket is set to non-blocking, else to blocking mode. Initially
320all sockets are in blocking mode. In non-blocking mode, if a
Fred Draked883ca11998-03-10 05:20:33 +0000321\method{recv()} call doesn't find any data, or if a \code{send} call can't
322immediately dispose of the data, a \exception{error} exception is
Guido van Rossum91951481994-09-07 14:39:14 +0000323raised; in blocking mode, the calls block until they can proceed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000324\end{methoddesc}
Guido van Rossum91951481994-09-07 14:39:14 +0000325
Fred Drake3f1c4721998-04-03 07:04:45 +0000326\begin{methoddesc}[socket]{setsockopt}{level, optname, value}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000327Set the value of the given socket option (see the \UNIX{} man page
Fred Draked883ca11998-03-10 05:20:33 +0000328\manpage{setsockopt}{2}). The needed symbolic constants are defined in
329the \module{socket} module (\code{SO_*} etc.). The value can be an
Guido van Rossum8df36371995-02-27 17:52:15 +0000330integer or a string representing a buffer. In the latter case it is
331up to the caller to ensure that the string contains the proper bits
332(see the optional built-in module
Fred Draked883ca11998-03-10 05:20:33 +0000333\module{struct}\refbimodindex{struct} for a way to encode C structures
334as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000335\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000336
Fred Drake3f1c4721998-04-03 07:04:45 +0000337\begin{methoddesc}[socket]{shutdown}{how}
Fred Draked883ca11998-03-10 05:20:33 +0000338Shut down one or both halves of the connection. If \var{how} is
339\code{0}, further receives are disallowed. If \var{how} is \code{1},
340further sends are disallowed. If \var{how} is \code{2}, further sends
341and receives are disallowed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000342\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000343
Fred Draked883ca11998-03-10 05:20:33 +0000344Note that there are no methods \method{read()} or \method{write()};
345use \method{recv()} and \method{send()} without \var{flags} argument
346instead.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000347
348\subsection{Example}
349\nodename{Socket Example}
350
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000351Here are two minimal example programs using the TCP/IP protocol:\ a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000352server that echoes all data that it receives back (servicing only one
353client), and a client using it. Note that a server must perform the
Fred Draked883ca11998-03-10 05:20:33 +0000354sequence \function{socket()}, \method{bind()}, \method{listen()},
355\method{accept()} (possibly repeating the \method{accept()} to service
356more than one client), while a client only needs the sequence
357\function{socket()}, \method{connect()}. Also note that the server
358does not \method{send()}/\method{recv()} on the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000359socket it is listening on but on the new socket returned by
Fred Draked883ca11998-03-10 05:20:33 +0000360\method{accept()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000361
Fred Drake19479911998-02-13 06:58:54 +0000362\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000363# Echo server program
364from socket import *
365HOST = '' # Symbolic name meaning the local host
366PORT = 50007 # Arbitrary non-privileged server
367s = socket(AF_INET, SOCK_STREAM)
368s.bind(HOST, PORT)
Guido van Rossum5da57551994-03-02 10:52:16 +0000369s.listen(1)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000370conn, addr = s.accept()
371print 'Connected by', addr
372while 1:
373 data = conn.recv(1024)
374 if not data: break
375 conn.send(data)
376conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000377\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000378
Fred Drake19479911998-02-13 06:58:54 +0000379\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000380# Echo client program
381from socket import *
382HOST = 'daring.cwi.nl' # The remote host
383PORT = 50007 # The same port as used by the server
384s = socket(AF_INET, SOCK_STREAM)
385s.connect(HOST, PORT)
386s.send('Hello, world')
387data = s.recv(1024)
388s.close()
389print 'Received', `data`
Fred Drake19479911998-02-13 06:58:54 +0000390\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000391
Guido van Rossume47da0a1997-07-17 16:34:52 +0000392\begin{seealso}
393\seemodule{SocketServer}{classes that simplify writing network servers}
394\end{seealso}