blob: 47a291484dcc38981a39a16ab80c09a6359cfe08 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{socket}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{socket}
3
4\modulesynopsis{Low-level networking interface.}
5
Fred Draked883ca11998-03-10 05:20:33 +00006
Fred Drakeaf8a0151998-01-14 14:51:31 +00007This module provides access to the BSD \emph{socket} interface.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008It is available on \UNIX{} systems that support this interface.
9
10For an introduction to socket programming (in C), see the following
11papers: \emph{An Introductory 4.3BSD Interprocess Communication
12Tutorial}, by Stuart Sechrest and \emph{An Advanced 4.3BSD Interprocess
13Communication Tutorial}, by Samuel J. Leffler et al, both in the
14\UNIX{} Programmer's Manual, Supplementary Documents 1 (sections PS1:7
15and PS1:8). The \UNIX{} manual pages for the various socket-related
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000016system calls are also a valuable source of information on the details of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017socket semantics.
18
19The Python interface is a straightforward transliteration of the
20\UNIX{} system call and library interface for sockets to Python's
Fred Draked883ca11998-03-10 05:20:33 +000021object-oriented style: the \function{socket()} function returns a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022\dfn{socket object} whose methods implement the various socket system
Barry Warsawd44be3f1997-01-03 20:19:05 +000023calls. Parameter types are somewhat higher-level than in the C
Fred Draked883ca11998-03-10 05:20:33 +000024interface: as with \method{read()} and \method{write()} operations on
25Python files, buffer allocation on receive operations is automatic,
26and buffer length is implicit on send operations.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000027
28Socket addresses are represented as a single string for the
Fred Draked883ca11998-03-10 05:20:33 +000029\constant{AF_UNIX} address family and as a pair
30\code{(\var{host}, \var{port})} for the \constant{AF_INET} address
31family, where \var{host} is a string representing
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032either a hostname in Internet domain notation like
33\code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'},
34and \var{port} is an integral port number. Other address families are
35currently not supported. The address format required by a particular
36socket object is automatically selected based on the address family
37specified when the socket object was created.
38
Guido van Rossume4f347e1997-05-09 02:21:51 +000039For IP addresses, two special forms are accepted instead of a host
Fred Draked883ca11998-03-10 05:20:33 +000040address: the empty string represents \constant{INADDR_ANY}, and the string
41\code{"<broadcast>"} represents \constant{INADDR_BROADCAST}.
Guido van Rossume4f347e1997-05-09 02:21:51 +000042
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000043All errors raise exceptions. The normal exceptions for invalid
44argument types and out-of-memory conditions can be raised; errors
45related to socket or address semantics raise the error \code{socket.error}.
46
Guido van Rossum470be141995-03-17 16:07:09 +000047Non-blocking mode is supported through the \code{setblocking()}
48method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049
Fred Draked883ca11998-03-10 05:20:33 +000050The module \module{socket} exports the following constants and functions:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051
Fred Draked883ca11998-03-10 05:20:33 +000052
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053\begin{excdesc}{error}
54This exception is raised for socket- or address-related errors.
55The accompanying value is either a string telling what went wrong or a
56pair \code{(\var{errno}, \var{string})}
57representing an error returned by a system
Guido van Rossum8e1e68d1998-02-06 15:18:25 +000058call, similar to the value accompanying \code{os.error}.
59See the module \module{errno}\refbimodindex{errno}, which contains
60names for the error codes defined by the underlying operating system.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000061\end{excdesc}
62
63\begin{datadesc}{AF_UNIX}
64\dataline{AF_INET}
65These constants represent the address (and protocol) families,
Fred Draked883ca11998-03-10 05:20:33 +000066used for the first argument to \function{socket()}. If the
67\constant{AF_UNIX} constant is not defined then this protocol is
68unsupported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000069\end{datadesc}
70
71\begin{datadesc}{SOCK_STREAM}
72\dataline{SOCK_DGRAM}
Guido van Rossum781db5d1994-08-05 13:37:36 +000073\dataline{SOCK_RAW}
74\dataline{SOCK_RDM}
75\dataline{SOCK_SEQPACKET}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076These constants represent the socket types,
Fred Draked883ca11998-03-10 05:20:33 +000077used for the second argument to \function{socket()}.
78(Only \constant{SOCK_STREAM} and
79\constant{SOCK_DGRAM} appear to be generally useful.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080\end{datadesc}
81
Guido van Rossumed2bad81995-02-16 16:29:18 +000082\begin{datadesc}{SO_*}
83\dataline{SOMAXCONN}
84\dataline{MSG_*}
85\dataline{SOL_*}
86\dataline{IPPROTO_*}
87\dataline{IPPORT_*}
88\dataline{INADDR_*}
89\dataline{IP_*}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000090Many constants of these forms, documented in the \UNIX{} documentation on
Guido van Rossumed2bad81995-02-16 16:29:18 +000091sockets and/or the IP protocol, are also defined in the socket module.
Fred Draked883ca11998-03-10 05:20:33 +000092They are generally used in arguments to the \method{setsockopt()} and
93\method{getsockopt()} methods of socket objects. In most cases, only
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000094those symbols that are defined in the \UNIX{} header files are defined;
Guido van Rossumed2bad81995-02-16 16:29:18 +000095for a few symbols, default values are provided.
96\end{datadesc}
97
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000098\begin{funcdesc}{gethostbyname}{hostname}
99Translate a host name to IP address format. The IP address is
100returned as a string, e.g., \code{'100.50.200.5'}. If the host name
Guido van Rossumcdf6af11998-08-07 18:07:36 +0000101is an IP address itself it is returned unchanged. See
102\code{gethostbyname_ex} for a more complete interface.
103\end{funcdesc}
104
105\begin{funcdesc}{gethostbyname_ex}{hostname}
106Translate a host name to IP address format, extended interface.
107Return a triple \code{(hostname, aliaslist, ipaddrlist)} where
108\code{hostname} is the primary host name responding to the given
109\var{ip_address}, \code{aliaslist} is a (possibly empty) list of
110alternative host names for the same address, and \code{ipaddrlist} is
111a list of IP addresses for the same interface on the same
112host (often but not always a single address).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000113\end{funcdesc}
114
Guido van Rossum781db5d1994-08-05 13:37:36 +0000115\begin{funcdesc}{gethostname}{}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000116Return a string containing the hostname of the machine where
117the Python interpreter is currently executing. If you want to know the
Fred Draked883ca11998-03-10 05:20:33 +0000118current machine's IP address, use \code{gethostbyname(gethostname())}.
119Note: \function{gethostname()} doesn't always return the fully qualified
120domain name; use \code{gethostbyaddr(gethostname())}
Guido van Rossumfe27a501997-01-11 17:04:56 +0000121(see below).
Guido van Rossum31cce971995-01-04 19:17:34 +0000122\end{funcdesc}
123
124\begin{funcdesc}{gethostbyaddr}{ip_address}
Fred Draked883ca11998-03-10 05:20:33 +0000125Return a triple \code{(\var{hostname}, \var{aliaslist},
126\var{ipaddrlist})} where \var{hostname} is the primary host name
127responding to the given \var{ip_address}, \var{aliaslist} is a
128(possibly empty) list of alternative host names for the same address,
129and \var{ipaddrlist} is a list of IP addresses for the same interface
130on the same host (most likely containing only a single address).
Guido van Rossumfe27a501997-01-11 17:04:56 +0000131To find the fully qualified domain name, check \var{hostname} and the
132items of \var{aliaslist} for an entry containing at least one period.
Guido van Rossum781db5d1994-08-05 13:37:36 +0000133\end{funcdesc}
134
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000135\begin{funcdesc}{getprotobyname}{protocolname}
136Translate an Internet protocol name (e.g. \code{'icmp'}) to a constant
137suitable for passing as the (optional) third argument to the
Fred Draked883ca11998-03-10 05:20:33 +0000138\function{socket()} function. This is usually only needed for sockets
139opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket
140modes, the correct protocol is chosen automatically if the protocol is
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000141omitted or zero.
142\end{funcdesc}
143
Fred Draked883ca11998-03-10 05:20:33 +0000144\begin{funcdesc}{getservbyname}{servicename, protocolname}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000145Translate an Internet service name and protocol name to a port number
146for that service. The protocol name should be \code{'tcp'} or
147\code{'udp'}.
148\end{funcdesc}
149
Fred Draked883ca11998-03-10 05:20:33 +0000150\begin{funcdesc}{socket}{family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000151Create a new socket using the given address family, socket type and
Fred Draked883ca11998-03-10 05:20:33 +0000152protocol number. The address family should be \constant{AF_INET} or
153\constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM},
154\constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000155The protocol number is usually zero and may be omitted in that case.
156\end{funcdesc}
157
Fred Draked883ca11998-03-10 05:20:33 +0000158\begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000159Build a socket object from an existing file descriptor (an integer as
Fred Draked883ca11998-03-10 05:20:33 +0000160returned by a file object's \method{fileno()} method). Address family,
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000161socket type and protocol number are as for the \code{socket} function
162above. The file descriptor should refer to a socket, but this is not
163checked --- subsequent operations on the object may fail if the file
164descriptor is invalid. This function is rarely needed, but can be
165used to get or set socket options on a socket passed to a program as
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000166standard input or output (e.g.\ a server started by the \UNIX{} inet
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000167daemon).
168\end{funcdesc}
169
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000170\begin{funcdesc}{ntohl}{x}
Fred Drakec5aec051997-12-08 21:25:41 +0000171Convert 32-bit integers from network to host byte order. On machines
172where the host byte order is the same as network byte order, this is a
173no-op; otherwise, it performs a 4-byte swap operation.
174\end{funcdesc}
175
176\begin{funcdesc}{ntohs}{x}
177Convert 16-bit integers from network to host byte order. On machines
178where the host byte order is the same as network byte order, this is a
179no-op; otherwise, it performs a 2-byte swap operation.
180\end{funcdesc}
181
182\begin{funcdesc}{htonl}{x}
183Convert 32-bit integers from host to network byte order. On machines
184where the host byte order is the same as network byte order, this is a
185no-op; otherwise, it performs a 4-byte swap operation.
186\end{funcdesc}
187
188\begin{funcdesc}{htons}{x}
189Convert 16-bit integers from host to network byte order. On machines
190where the host byte order is the same as network byte order, this is a
191no-op; otherwise, it performs a 2-byte swap operation.
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000192\end{funcdesc}
193
Fred Drake5451d671997-10-13 21:31:02 +0000194\begin{datadesc}{SocketType}
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000195This is a Python type object that represents the socket object type.
Fred Draked883ca11998-03-10 05:20:33 +0000196It is the same as \code{type(socket(...))}.
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000197\end{datadesc}
198
Guido van Rossum470be141995-03-17 16:07:09 +0000199\subsection{Socket Objects}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000200
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000201Socket objects have the following methods. Except for
Fred Draked883ca11998-03-10 05:20:33 +0000202\method{makefile()} these correspond to \UNIX{} system calls
203applicable to sockets.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000204
Fred Drake3f1c4721998-04-03 07:04:45 +0000205\begin{methoddesc}[socket]{accept}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000206Accept a connection.
207The socket must be bound to an address and listening for connections.
208The return value is a pair \code{(\var{conn}, \var{address})}
209where \var{conn} is a \emph{new} socket object usable to send and
210receive data on the connection, and \var{address} is the address bound
211to the socket on the other end of the connection.
Fred Drake3f1c4721998-04-03 07:04:45 +0000212\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000213
Fred Drake3f1c4721998-04-03 07:04:45 +0000214\begin{methoddesc}[socket]{bind}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000215Bind the socket to \var{address}. The socket must not already be bound.
Guido van Rossum86751151995-02-28 17:14:32 +0000216(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000217\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000218
Fred Drake3f1c4721998-04-03 07:04:45 +0000219\begin{methoddesc}[socket]{close}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000220Close the socket. All future operations on the socket object will fail.
221The remote end will receive no more data (after queued data is flushed).
222Sockets are automatically closed when they are garbage-collected.
Fred Drake3f1c4721998-04-03 07:04:45 +0000223\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000224
Fred Drake3f1c4721998-04-03 07:04:45 +0000225\begin{methoddesc}[socket]{connect}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000226Connect to a remote socket at \var{address}.
Fred Draked883ca11998-03-10 05:20:33 +0000227(The format of \var{address} depends on the address family --- see
228above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000229\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000230
Fred Drake3f1c4721998-04-03 07:04:45 +0000231\begin{methoddesc}[socket]{connect_ex}{address}
Guido van Rossumeefcba61997-12-09 19:47:24 +0000232Like \code{connect(\var{address})}, but return an error indicator
Guido van Rossumf7790c61997-11-18 15:29:20 +0000233instead of raising an exception. The error indicator is 0 if the
Fred Draked883ca11998-03-10 05:20:33 +0000234operation succeeded, otherwise the value of the \cdata{errno}
Fred Drake3f1c4721998-04-03 07:04:45 +0000235variable. This is useful, e.g., for asynchronous connects.
236\end{methoddesc}
Guido van Rossumf7790c61997-11-18 15:29:20 +0000237
Fred Drake3f1c4721998-04-03 07:04:45 +0000238\begin{methoddesc}[socket]{fileno}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000239Return the socket's file descriptor (a small integer). This is useful
Fred Draked883ca11998-03-10 05:20:33 +0000240with \function{select.select()}.
Fred Drake3f1c4721998-04-03 07:04:45 +0000241\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000242
Fred Drake3f1c4721998-04-03 07:04:45 +0000243\begin{methoddesc}[socket]{getpeername}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000244Return the remote address to which the socket is connected. This is
245useful to find out the port number of a remote IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000246(The format of the address returned depends on the address family ---
Guido van Rossum781db5d1994-08-05 13:37:36 +0000247see above.) On some systems this function is not supported.
Fred Drake3f1c4721998-04-03 07:04:45 +0000248\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000249
Fred Drake3f1c4721998-04-03 07:04:45 +0000250\begin{methoddesc}[socket]{getsockname}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000251Return the socket's own address. This is useful to find out the port
252number of an IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000253(The format of the address returned depends on the address family ---
Guido van Rossuma84ec511994-06-23 12:13:52 +0000254see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000255\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000256
Fred Drake3f1c4721998-04-03 07:04:45 +0000257\begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000258Return the value of the given socket option (see the \UNIX{} man page
Fred Draked883ca11998-03-10 05:20:33 +0000259\manpage{getsockopt}{2}). The needed symbolic constants
260(\constant{SO_*} etc.) are defined in this module. If \var{buflen}
Guido van Rossum470be141995-03-17 16:07:09 +0000261is absent, an integer option is assumed and its integer value
Guido van Rossum8df36371995-02-27 17:52:15 +0000262is returned by the function. If \var{buflen} is present, it specifies
263the maximum length of the buffer used to receive the option in, and
Guido van Rossum470be141995-03-17 16:07:09 +0000264this buffer is returned as a string. It is up to the caller to decode
Guido van Rossum8df36371995-02-27 17:52:15 +0000265the contents of the buffer (see the optional built-in module
Fred Draked883ca11998-03-10 05:20:33 +0000266\module{struct} for a way to decode C structures encoded as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000267\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000268
Fred Drake3f1c4721998-04-03 07:04:45 +0000269\begin{methoddesc}[socket]{listen}{backlog}
Guido van Rossum470be141995-03-17 16:07:09 +0000270Listen for connections made to the socket. The \var{backlog} argument
271specifies the maximum number of queued connections and should be at
272least 1; the maximum value is system-dependent (usually 5).
Fred Drake3f1c4721998-04-03 07:04:45 +0000273\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000274
Fred Drake3f1c4721998-04-03 07:04:45 +0000275\begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}}
Guido van Rossum470be141995-03-17 16:07:09 +0000276Return a \dfn{file object} associated with the socket. (File objects
Fred Draked883ca11998-03-10 05:20:33 +0000277were described earlier in \ref{bltin-file-objects}, ``File Objects.'')
278The file object references a \cfunction{dup()}ped version of the
279socket file descriptor, so the file object and socket object may be
280closed or garbage-collected independently. The optional \var{mode}
281and \var{bufsize} arguments are interpreted the same way as by the
282built-in \function{open()} function.
Fred Drake3f1c4721998-04-03 07:04:45 +0000283\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000284
Fred Drake3f1c4721998-04-03 07:04:45 +0000285\begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000286Receive data from the socket. The return value is a string representing
287the data received. The maximum amount of data to be received
288at once is specified by \var{bufsize}. See the \UNIX{} manual page
Fred Draked883ca11998-03-10 05:20:33 +0000289\manpage{recv}{2} for the meaning of the optional argument
290\var{flags}; it defaults to zero.
Fred Drake3f1c4721998-04-03 07:04:45 +0000291\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000292
Fred Drake3f1c4721998-04-03 07:04:45 +0000293\begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000294Receive data from the socket. The return value is a pair
295\code{(\var{string}, \var{address})} where \var{string} is a string
296representing the data received and \var{address} is the address of the
Guido van Rossum470be141995-03-17 16:07:09 +0000297socket sending the data. The optional \var{flags} argument has the
Fred Draked883ca11998-03-10 05:20:33 +0000298same meaning as for \method{recv()} above.
Guido van Rossum86751151995-02-28 17:14:32 +0000299(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000300\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000301
Fred Drake3f1c4721998-04-03 07:04:45 +0000302\begin{methoddesc}[socket]{send}{string\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000303Send data to the socket. The socket must be connected to a remote
Guido van Rossum470be141995-03-17 16:07:09 +0000304socket. The optional \var{flags} argument has the same meaning as for
Fred Draked883ca11998-03-10 05:20:33 +0000305\method{recv()} above. Returns the number of bytes sent.
Fred Drake3f1c4721998-04-03 07:04:45 +0000306\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000307
Fred Drake3f1c4721998-04-03 07:04:45 +0000308\begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000309Send data to the socket. The socket should not be connected to a
310remote socket, since the destination socket is specified by
Fred Draked883ca11998-03-10 05:20:33 +0000311\var{address}. The optional \var{flags} argument has the same
312meaning as for \method{recv()} above. Return the number of bytes sent.
Guido van Rossum86751151995-02-28 17:14:32 +0000313(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000314\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000315
Fred Drake3f1c4721998-04-03 07:04:45 +0000316\begin{methoddesc}[socket]{setblocking}{flag}
Guido van Rossum91951481994-09-07 14:39:14 +0000317Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
318the socket is set to non-blocking, else to blocking mode. Initially
319all sockets are in blocking mode. In non-blocking mode, if a
Fred Draked883ca11998-03-10 05:20:33 +0000320\method{recv()} call doesn't find any data, or if a \code{send} call can't
321immediately dispose of the data, a \exception{error} exception is
Guido van Rossum91951481994-09-07 14:39:14 +0000322raised; in blocking mode, the calls block until they can proceed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000323\end{methoddesc}
Guido van Rossum91951481994-09-07 14:39:14 +0000324
Fred Drake3f1c4721998-04-03 07:04:45 +0000325\begin{methoddesc}[socket]{setsockopt}{level, optname, value}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000326Set the value of the given socket option (see the \UNIX{} man page
Fred Draked883ca11998-03-10 05:20:33 +0000327\manpage{setsockopt}{2}). The needed symbolic constants are defined in
328the \module{socket} module (\code{SO_*} etc.). The value can be an
Guido van Rossum8df36371995-02-27 17:52:15 +0000329integer or a string representing a buffer. In the latter case it is
330up to the caller to ensure that the string contains the proper bits
331(see the optional built-in module
Fred Draked883ca11998-03-10 05:20:33 +0000332\module{struct}\refbimodindex{struct} for a way to encode C structures
333as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000334\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000335
Fred Drake3f1c4721998-04-03 07:04:45 +0000336\begin{methoddesc}[socket]{shutdown}{how}
Fred Draked883ca11998-03-10 05:20:33 +0000337Shut down one or both halves of the connection. If \var{how} is
338\code{0}, further receives are disallowed. If \var{how} is \code{1},
339further sends are disallowed. If \var{how} is \code{2}, further sends
340and receives are disallowed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000341\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000342
Fred Draked883ca11998-03-10 05:20:33 +0000343Note that there are no methods \method{read()} or \method{write()};
344use \method{recv()} and \method{send()} without \var{flags} argument
345instead.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000346
347\subsection{Example}
348\nodename{Socket Example}
349
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000350Here are two minimal example programs using the TCP/IP protocol:\ a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000351server that echoes all data that it receives back (servicing only one
352client), and a client using it. Note that a server must perform the
Fred Draked883ca11998-03-10 05:20:33 +0000353sequence \function{socket()}, \method{bind()}, \method{listen()},
354\method{accept()} (possibly repeating the \method{accept()} to service
355more than one client), while a client only needs the sequence
356\function{socket()}, \method{connect()}. Also note that the server
357does not \method{send()}/\method{recv()} on the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000358socket it is listening on but on the new socket returned by
Fred Draked883ca11998-03-10 05:20:33 +0000359\method{accept()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000360
Fred Drake19479911998-02-13 06:58:54 +0000361\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000362# Echo server program
363from socket import *
364HOST = '' # Symbolic name meaning the local host
365PORT = 50007 # Arbitrary non-privileged server
366s = socket(AF_INET, SOCK_STREAM)
367s.bind(HOST, PORT)
Guido van Rossum5da57551994-03-02 10:52:16 +0000368s.listen(1)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000369conn, addr = s.accept()
370print 'Connected by', addr
371while 1:
372 data = conn.recv(1024)
373 if not data: break
374 conn.send(data)
375conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000376\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000377
Fred Drake19479911998-02-13 06:58:54 +0000378\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000379# Echo client program
380from socket import *
381HOST = 'daring.cwi.nl' # The remote host
382PORT = 50007 # The same port as used by the server
383s = socket(AF_INET, SOCK_STREAM)
384s.connect(HOST, PORT)
385s.send('Hello, world')
386data = s.recv(1024)
387s.close()
388print 'Received', `data`
Fred Drake19479911998-02-13 06:58:54 +0000389\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000390
Guido van Rossume47da0a1997-07-17 16:34:52 +0000391\begin{seealso}
392\seemodule{SocketServer}{classes that simplify writing network servers}
393\end{seealso}