blob: 57b82625eaa3f14309331d150a5cc30c6edc4958 [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.
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
Fred Drake318c0b11999-04-21 17:29:14 +000023\dfn{socket object}\obindex{socket} whose methods implement the
24various socket system calls. Parameter types are somewhat
25higher-level than in the C interface: as with \method{read()} and
26\method{write()} operations on Python files, buffer allocation on
27receive operations is automatic, and buffer length is implicit on send
28operations.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000029
30Socket addresses are represented as a single string for the
Fred Draked883ca11998-03-10 05:20:33 +000031\constant{AF_UNIX} address family and as a pair
32\code{(\var{host}, \var{port})} for the \constant{AF_INET} address
33family, where \var{host} is a string representing
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034either a hostname in Internet domain notation like
35\code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'},
36and \var{port} is an integral port number. Other address families are
37currently not supported. The address format required by a particular
38socket object is automatically selected based on the address family
39specified when the socket object was created.
40
Guido van Rossume4f347e1997-05-09 02:21:51 +000041For IP addresses, two special forms are accepted instead of a host
Fred Draked883ca11998-03-10 05:20:33 +000042address: the empty string represents \constant{INADDR_ANY}, and the string
Fred Drake318c0b11999-04-21 17:29:14 +000043\code{'<broadcast>'} represents \constant{INADDR_BROADCAST}.
Guido van Rossume4f347e1997-05-09 02:21:51 +000044
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000045All errors raise exceptions. The normal exceptions for invalid
46argument types and out-of-memory conditions can be raised; errors
Fred Drake318c0b11999-04-21 17:29:14 +000047related to socket or address semantics raise the error
48\exception{socket.error}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049
Fred Drake318c0b11999-04-21 17:29:14 +000050Non-blocking mode is supported through the
51\method{setblocking()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052
Fred Draked883ca11998-03-10 05:20:33 +000053The module \module{socket} exports the following constants and functions:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054
Fred Draked883ca11998-03-10 05:20:33 +000055
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056\begin{excdesc}{error}
57This exception is raised for socket- or address-related errors.
58The accompanying value is either a string telling what went wrong or a
59pair \code{(\var{errno}, \var{string})}
60representing an error returned by a system
Fred Drake318c0b11999-04-21 17:29:14 +000061call, similar to the value accompanying \exception{os.error}.
62See the module \refmodule{errno}\refbimodindex{errno}, which contains
Guido van Rossum8e1e68d1998-02-06 15:18:25 +000063names for the error codes defined by the underlying operating system.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000064\end{excdesc}
65
66\begin{datadesc}{AF_UNIX}
67\dataline{AF_INET}
68These constants represent the address (and protocol) families,
Fred Draked883ca11998-03-10 05:20:33 +000069used for the first argument to \function{socket()}. If the
70\constant{AF_UNIX} constant is not defined then this protocol is
71unsupported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000072\end{datadesc}
73
74\begin{datadesc}{SOCK_STREAM}
75\dataline{SOCK_DGRAM}
Guido van Rossum781db5d1994-08-05 13:37:36 +000076\dataline{SOCK_RAW}
77\dataline{SOCK_RDM}
78\dataline{SOCK_SEQPACKET}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079These constants represent the socket types,
Fred Draked883ca11998-03-10 05:20:33 +000080used for the second argument to \function{socket()}.
81(Only \constant{SOCK_STREAM} and
82\constant{SOCK_DGRAM} appear to be generally useful.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000083\end{datadesc}
84
Guido van Rossumed2bad81995-02-16 16:29:18 +000085\begin{datadesc}{SO_*}
86\dataline{SOMAXCONN}
87\dataline{MSG_*}
88\dataline{SOL_*}
89\dataline{IPPROTO_*}
90\dataline{IPPORT_*}
91\dataline{INADDR_*}
92\dataline{IP_*}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000093Many constants of these forms, documented in the \UNIX{} documentation on
Guido van Rossumed2bad81995-02-16 16:29:18 +000094sockets and/or the IP protocol, are also defined in the socket module.
Fred Draked883ca11998-03-10 05:20:33 +000095They are generally used in arguments to the \method{setsockopt()} and
96\method{getsockopt()} methods of socket objects. In most cases, only
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000097those symbols that are defined in the \UNIX{} header files are defined;
Guido van Rossumed2bad81995-02-16 16:29:18 +000098for a few symbols, default values are provided.
99\end{datadesc}
100
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000101\begin{funcdesc}{gethostbyname}{hostname}
102Translate a host name to IP address format. The IP address is
103returned as a string, e.g., \code{'100.50.200.5'}. If the host name
Guido van Rossumcdf6af11998-08-07 18:07:36 +0000104is an IP address itself it is returned unchanged. See
Fred Drake318c0b11999-04-21 17:29:14 +0000105\function{gethostbyname_ex()} for a more complete interface.
Guido van Rossumcdf6af11998-08-07 18:07:36 +0000106\end{funcdesc}
107
108\begin{funcdesc}{gethostbyname_ex}{hostname}
109Translate a host name to IP address format, extended interface.
110Return a triple \code{(hostname, aliaslist, ipaddrlist)} where
111\code{hostname} is the primary host name responding to the given
112\var{ip_address}, \code{aliaslist} is a (possibly empty) list of
113alternative host names for the same address, and \code{ipaddrlist} is
114a list of IP addresses for the same interface on the same
115host (often but not always a single address).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000116\end{funcdesc}
117
Guido van Rossum781db5d1994-08-05 13:37:36 +0000118\begin{funcdesc}{gethostname}{}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000119Return a string containing the hostname of the machine where
120the Python interpreter is currently executing. If you want to know the
Fred Draked883ca11998-03-10 05:20:33 +0000121current machine's IP address, use \code{gethostbyname(gethostname())}.
122Note: \function{gethostname()} doesn't always return the fully qualified
123domain name; use \code{gethostbyaddr(gethostname())}
Guido van Rossumfe27a501997-01-11 17:04:56 +0000124(see below).
Guido van Rossum31cce971995-01-04 19:17:34 +0000125\end{funcdesc}
126
127\begin{funcdesc}{gethostbyaddr}{ip_address}
Fred Draked883ca11998-03-10 05:20:33 +0000128Return a triple \code{(\var{hostname}, \var{aliaslist},
129\var{ipaddrlist})} where \var{hostname} is the primary host name
130responding to the given \var{ip_address}, \var{aliaslist} is a
131(possibly empty) list of alternative host names for the same address,
132and \var{ipaddrlist} is a list of IP addresses for the same interface
133on the same host (most likely containing only a single address).
Guido van Rossumfe27a501997-01-11 17:04:56 +0000134To find the fully qualified domain name, check \var{hostname} and the
135items of \var{aliaslist} for an entry containing at least one period.
Guido van Rossum781db5d1994-08-05 13:37:36 +0000136\end{funcdesc}
137
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000138\begin{funcdesc}{getprotobyname}{protocolname}
Fred Drake318c0b11999-04-21 17:29:14 +0000139Translate an Internet protocol name (e.g.\ \code{'icmp'}) to a constant
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000140suitable for passing as the (optional) third argument to the
Fred Draked883ca11998-03-10 05:20:33 +0000141\function{socket()} function. This is usually only needed for sockets
142opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket
143modes, the correct protocol is chosen automatically if the protocol is
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000144omitted or zero.
145\end{funcdesc}
146
Fred Draked883ca11998-03-10 05:20:33 +0000147\begin{funcdesc}{getservbyname}{servicename, protocolname}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000148Translate an Internet service name and protocol name to a port number
149for that service. The protocol name should be \code{'tcp'} or
150\code{'udp'}.
151\end{funcdesc}
152
Fred Draked883ca11998-03-10 05:20:33 +0000153\begin{funcdesc}{socket}{family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000154Create a new socket using the given address family, socket type and
Fred Draked883ca11998-03-10 05:20:33 +0000155protocol number. The address family should be \constant{AF_INET} or
156\constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM},
157\constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000158The protocol number is usually zero and may be omitted in that case.
159\end{funcdesc}
160
Fred Draked883ca11998-03-10 05:20:33 +0000161\begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000162Build a socket object from an existing file descriptor (an integer as
Fred Draked883ca11998-03-10 05:20:33 +0000163returned by a file object's \method{fileno()} method). Address family,
Fred Drake318c0b11999-04-21 17:29:14 +0000164socket type and protocol number are as for the \function{socket()} function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000165above. The file descriptor should refer to a socket, but this is not
166checked --- subsequent operations on the object may fail if the file
167descriptor is invalid. This function is rarely needed, but can be
168used to get or set socket options on a socket passed to a program as
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000169standard input or output (e.g.\ a server started by the \UNIX{} inet
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000170daemon).
171\end{funcdesc}
172
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000173\begin{funcdesc}{ntohl}{x}
Fred Drakec5aec051997-12-08 21:25:41 +0000174Convert 32-bit integers from network to host byte order. On machines
175where the host byte order is the same as network byte order, this is a
176no-op; otherwise, it performs a 4-byte swap operation.
177\end{funcdesc}
178
179\begin{funcdesc}{ntohs}{x}
180Convert 16-bit integers from network to host byte order. On machines
181where the host byte order is the same as network byte order, this is a
182no-op; otherwise, it performs a 2-byte swap operation.
183\end{funcdesc}
184
185\begin{funcdesc}{htonl}{x}
186Convert 32-bit integers from host to network byte order. On machines
187where the host byte order is the same as network byte order, this is a
188no-op; otherwise, it performs a 4-byte swap operation.
189\end{funcdesc}
190
191\begin{funcdesc}{htons}{x}
192Convert 16-bit integers from host to network byte order. On machines
193where the host byte order is the same as network byte order, this is a
194no-op; otherwise, it performs a 2-byte swap operation.
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000195\end{funcdesc}
196
Fred Drake5451d671997-10-13 21:31:02 +0000197\begin{datadesc}{SocketType}
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000198This is a Python type object that represents the socket object type.
Fred Draked883ca11998-03-10 05:20:33 +0000199It is the same as \code{type(socket(...))}.
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000200\end{datadesc}
201
Guido van Rossum470be141995-03-17 16:07:09 +0000202\subsection{Socket Objects}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000203
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000204Socket objects have the following methods. Except for
Fred Draked883ca11998-03-10 05:20:33 +0000205\method{makefile()} these correspond to \UNIX{} system calls
206applicable to sockets.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000207
Fred Drake3f1c4721998-04-03 07:04:45 +0000208\begin{methoddesc}[socket]{accept}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000209Accept a connection.
210The socket must be bound to an address and listening for connections.
211The return value is a pair \code{(\var{conn}, \var{address})}
212where \var{conn} is a \emph{new} socket object usable to send and
213receive data on the connection, and \var{address} is the address bound
214to the socket on the other end of the connection.
Fred Drake3f1c4721998-04-03 07:04:45 +0000215\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000216
Fred Drake3f1c4721998-04-03 07:04:45 +0000217\begin{methoddesc}[socket]{bind}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000218Bind the socket to \var{address}. The socket must not already be bound.
Guido van Rossum86751151995-02-28 17:14:32 +0000219(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000220\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000221
Fred Drake3f1c4721998-04-03 07:04:45 +0000222\begin{methoddesc}[socket]{close}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000223Close the socket. All future operations on the socket object will fail.
224The remote end will receive no more data (after queued data is flushed).
225Sockets are automatically closed when they are garbage-collected.
Fred Drake3f1c4721998-04-03 07:04:45 +0000226\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000227
Fred Drake3f1c4721998-04-03 07:04:45 +0000228\begin{methoddesc}[socket]{connect}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000229Connect to a remote socket at \var{address}.
Fred Draked883ca11998-03-10 05:20:33 +0000230(The format of \var{address} depends on the address family --- see
231above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000232\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000233
Fred Drake3f1c4721998-04-03 07:04:45 +0000234\begin{methoddesc}[socket]{connect_ex}{address}
Guido van Rossumeefcba61997-12-09 19:47:24 +0000235Like \code{connect(\var{address})}, but return an error indicator
Guido van Rossumf7790c61997-11-18 15:29:20 +0000236instead of raising an exception. The error indicator is 0 if the
Fred Draked883ca11998-03-10 05:20:33 +0000237operation succeeded, otherwise the value of the \cdata{errno}
Fred Drake3f1c4721998-04-03 07:04:45 +0000238variable. This is useful, e.g., for asynchronous connects.
239\end{methoddesc}
Guido van Rossumf7790c61997-11-18 15:29:20 +0000240
Fred Drake3f1c4721998-04-03 07:04:45 +0000241\begin{methoddesc}[socket]{fileno}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000242Return the socket's file descriptor (a small integer). This is useful
Fred Draked883ca11998-03-10 05:20:33 +0000243with \function{select.select()}.
Fred Drake3f1c4721998-04-03 07:04:45 +0000244\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000245
Fred Drake3f1c4721998-04-03 07:04:45 +0000246\begin{methoddesc}[socket]{getpeername}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000247Return the remote address to which the socket is connected. This is
248useful to find out the port number of a remote IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000249(The format of the address returned depends on the address family ---
Guido van Rossum781db5d1994-08-05 13:37:36 +0000250see above.) On some systems this function is not supported.
Fred Drake3f1c4721998-04-03 07:04:45 +0000251\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000252
Fred Drake3f1c4721998-04-03 07:04:45 +0000253\begin{methoddesc}[socket]{getsockname}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000254Return the socket's own address. This is useful to find out the port
255number of an IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000256(The format of the address returned depends on the address family ---
Guido van Rossuma84ec511994-06-23 12:13:52 +0000257see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000258\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000259
Fred Drake3f1c4721998-04-03 07:04:45 +0000260\begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000261Return the value of the given socket option (see the \UNIX{} man page
Fred Draked883ca11998-03-10 05:20:33 +0000262\manpage{getsockopt}{2}). The needed symbolic constants
263(\constant{SO_*} etc.) are defined in this module. If \var{buflen}
Guido van Rossum470be141995-03-17 16:07:09 +0000264is absent, an integer option is assumed and its integer value
Guido van Rossum8df36371995-02-27 17:52:15 +0000265is returned by the function. If \var{buflen} is present, it specifies
266the maximum length of the buffer used to receive the option in, and
Guido van Rossum470be141995-03-17 16:07:09 +0000267this buffer is returned as a string. It is up to the caller to decode
Guido van Rossum8df36371995-02-27 17:52:15 +0000268the contents of the buffer (see the optional built-in module
Fred Drake318c0b11999-04-21 17:29:14 +0000269\refmodule{struct} for a way to decode C structures encoded as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000270\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000271
Fred Drake3f1c4721998-04-03 07:04:45 +0000272\begin{methoddesc}[socket]{listen}{backlog}
Guido van Rossum470be141995-03-17 16:07:09 +0000273Listen for connections made to the socket. The \var{backlog} argument
274specifies the maximum number of queued connections and should be at
275least 1; the maximum value is system-dependent (usually 5).
Fred Drake3f1c4721998-04-03 07:04:45 +0000276\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000277
Fred Drake3f1c4721998-04-03 07:04:45 +0000278\begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}}
Guido van Rossum470be141995-03-17 16:07:09 +0000279Return a \dfn{file object} associated with the socket. (File objects
Fred Draked883ca11998-03-10 05:20:33 +0000280were described earlier in \ref{bltin-file-objects}, ``File Objects.'')
281The file object references a \cfunction{dup()}ped version of the
282socket file descriptor, so the file object and socket object may be
283closed or garbage-collected independently. The optional \var{mode}
284and \var{bufsize} arguments are interpreted the same way as by the
285built-in \function{open()} function.
Fred Drake3f1c4721998-04-03 07:04:45 +0000286\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000287
Fred Drake3f1c4721998-04-03 07:04:45 +0000288\begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000289Receive data from the socket. The return value is a string representing
290the data received. The maximum amount of data to be received
291at once is specified by \var{bufsize}. See the \UNIX{} manual page
Fred Draked883ca11998-03-10 05:20:33 +0000292\manpage{recv}{2} for the meaning of the optional argument
293\var{flags}; it defaults to zero.
Fred Drake3f1c4721998-04-03 07:04:45 +0000294\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000295
Fred Drake3f1c4721998-04-03 07:04:45 +0000296\begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000297Receive data from the socket. The return value is a pair
298\code{(\var{string}, \var{address})} where \var{string} is a string
299representing the data received and \var{address} is the address of the
Guido van Rossum470be141995-03-17 16:07:09 +0000300socket sending the data. The optional \var{flags} argument has the
Fred Draked883ca11998-03-10 05:20:33 +0000301same meaning as for \method{recv()} above.
Guido van Rossum86751151995-02-28 17:14:32 +0000302(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000303\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000304
Fred Drake3f1c4721998-04-03 07:04:45 +0000305\begin{methoddesc}[socket]{send}{string\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000306Send data to the socket. The socket must be connected to a remote
Guido van Rossum470be141995-03-17 16:07:09 +0000307socket. The optional \var{flags} argument has the same meaning as for
Fred Draked883ca11998-03-10 05:20:33 +0000308\method{recv()} above. Returns the number of bytes sent.
Fred Drake3f1c4721998-04-03 07:04:45 +0000309\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000310
Fred Drake3f1c4721998-04-03 07:04:45 +0000311\begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000312Send data to the socket. The socket should not be connected to a
313remote socket, since the destination socket is specified by
Fred Draked883ca11998-03-10 05:20:33 +0000314\var{address}. The optional \var{flags} argument has the same
315meaning as for \method{recv()} above. Return the number of bytes sent.
Guido van Rossum86751151995-02-28 17:14:32 +0000316(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000317\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000318
Fred Drake3f1c4721998-04-03 07:04:45 +0000319\begin{methoddesc}[socket]{setblocking}{flag}
Guido van Rossum91951481994-09-07 14:39:14 +0000320Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
321the socket is set to non-blocking, else to blocking mode. Initially
322all sockets are in blocking mode. In non-blocking mode, if a
Fred Drake318c0b11999-04-21 17:29:14 +0000323\method{recv()} call doesn't find any data, or if a
324\method{send()} call can't immediately dispose of the data, a
325\exception{error} exception is raised; in blocking mode, the calls
326block until they can proceed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000327\end{methoddesc}
Guido van Rossum91951481994-09-07 14:39:14 +0000328
Fred Drake3f1c4721998-04-03 07:04:45 +0000329\begin{methoddesc}[socket]{setsockopt}{level, optname, value}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000330Set the value of the given socket option (see the \UNIX{} man page
Fred Draked883ca11998-03-10 05:20:33 +0000331\manpage{setsockopt}{2}). The needed symbolic constants are defined in
332the \module{socket} module (\code{SO_*} etc.). The value can be an
Guido van Rossum8df36371995-02-27 17:52:15 +0000333integer or a string representing a buffer. In the latter case it is
334up to the caller to ensure that the string contains the proper bits
335(see the optional built-in module
Fred Drake318c0b11999-04-21 17:29:14 +0000336\refmodule{struct}\refbimodindex{struct} for a way to encode C
337structures as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000338\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000339
Fred Drake3f1c4721998-04-03 07:04:45 +0000340\begin{methoddesc}[socket]{shutdown}{how}
Fred Draked883ca11998-03-10 05:20:33 +0000341Shut down one or both halves of the connection. If \var{how} is
342\code{0}, further receives are disallowed. If \var{how} is \code{1},
343further sends are disallowed. If \var{how} is \code{2}, further sends
344and receives are disallowed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000345\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000346
Fred Draked883ca11998-03-10 05:20:33 +0000347Note that there are no methods \method{read()} or \method{write()};
348use \method{recv()} and \method{send()} without \var{flags} argument
349instead.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000350
351\subsection{Example}
352\nodename{Socket Example}
353
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000354Here are two minimal example programs using the TCP/IP protocol:\ a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000355server that echoes all data that it receives back (servicing only one
356client), and a client using it. Note that a server must perform the
Fred Draked883ca11998-03-10 05:20:33 +0000357sequence \function{socket()}, \method{bind()}, \method{listen()},
358\method{accept()} (possibly repeating the \method{accept()} to service
359more than one client), while a client only needs the sequence
360\function{socket()}, \method{connect()}. Also note that the server
361does not \method{send()}/\method{recv()} on the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000362socket it is listening on but on the new socket returned by
Fred Draked883ca11998-03-10 05:20:33 +0000363\method{accept()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000364
Fred Drake19479911998-02-13 06:58:54 +0000365\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000366# Echo server program
367from socket import *
368HOST = '' # Symbolic name meaning the local host
369PORT = 50007 # Arbitrary non-privileged server
370s = socket(AF_INET, SOCK_STREAM)
371s.bind(HOST, PORT)
Guido van Rossum5da57551994-03-02 10:52:16 +0000372s.listen(1)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000373conn, addr = s.accept()
374print 'Connected by', addr
375while 1:
376 data = conn.recv(1024)
377 if not data: break
378 conn.send(data)
379conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000380\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000381
Fred Drake19479911998-02-13 06:58:54 +0000382\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000383# Echo client program
384from socket import *
385HOST = 'daring.cwi.nl' # The remote host
386PORT = 50007 # The same port as used by the server
387s = socket(AF_INET, SOCK_STREAM)
388s.connect(HOST, PORT)
389s.send('Hello, world')
390data = s.recv(1024)
391s.close()
392print 'Received', `data`
Fred Drake19479911998-02-13 06:58:54 +0000393\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000394
Guido van Rossume47da0a1997-07-17 16:34:52 +0000395\begin{seealso}
396\seemodule{SocketServer}{classes that simplify writing network servers}
397\end{seealso}