blob: 030eb72e6c9ca7ba98f785cbabf9fd7982d46eab [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{socket}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-socket}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\bimodindex{socket}
Fred Draked883ca11998-03-10 05:20:33 +00004
Fred Drakeaf8a0151998-01-14 14:51:31 +00005This module provides access to the BSD \emph{socket} interface.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00006It is available on \UNIX{} systems that support this interface.
7
8For an introduction to socket programming (in C), see the following
9papers: \emph{An Introductory 4.3BSD Interprocess Communication
10Tutorial}, by Stuart Sechrest and \emph{An Advanced 4.3BSD Interprocess
11Communication Tutorial}, by Samuel J. Leffler et al, both in the
12\UNIX{} Programmer's Manual, Supplementary Documents 1 (sections PS1:7
13and PS1:8). The \UNIX{} manual pages for the various socket-related
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000014system calls are also a valuable source of information on the details of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000015socket semantics.
16
17The Python interface is a straightforward transliteration of the
18\UNIX{} system call and library interface for sockets to Python's
Fred Draked883ca11998-03-10 05:20:33 +000019object-oriented style: the \function{socket()} function returns a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020\dfn{socket object} whose methods implement the various socket system
Barry Warsawd44be3f1997-01-03 20:19:05 +000021calls. Parameter types are somewhat higher-level than in the C
Fred Draked883ca11998-03-10 05:20:33 +000022interface: as with \method{read()} and \method{write()} operations on
23Python files, buffer allocation on receive operations is automatic,
24and buffer length is implicit on send operations.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000025
26Socket addresses are represented as a single string for the
Fred Draked883ca11998-03-10 05:20:33 +000027\constant{AF_UNIX} address family and as a pair
28\code{(\var{host}, \var{port})} for the \constant{AF_INET} address
29family, where \var{host} is a string representing
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030either a hostname in Internet domain notation like
31\code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'},
32and \var{port} is an integral port number. Other address families are
33currently not supported. The address format required by a particular
34socket object is automatically selected based on the address family
35specified when the socket object was created.
36
Guido van Rossume4f347e1997-05-09 02:21:51 +000037For IP addresses, two special forms are accepted instead of a host
Fred Draked883ca11998-03-10 05:20:33 +000038address: the empty string represents \constant{INADDR_ANY}, and the string
39\code{"<broadcast>"} represents \constant{INADDR_BROADCAST}.
Guido van Rossume4f347e1997-05-09 02:21:51 +000040
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041All errors raise exceptions. The normal exceptions for invalid
42argument types and out-of-memory conditions can be raised; errors
43related to socket or address semantics raise the error \code{socket.error}.
44
Guido van Rossum470be141995-03-17 16:07:09 +000045Non-blocking mode is supported through the \code{setblocking()}
46method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047
Fred Draked883ca11998-03-10 05:20:33 +000048The module \module{socket} exports the following constants and functions:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049
Fred Draked883ca11998-03-10 05:20:33 +000050
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051\begin{excdesc}{error}
52This exception is raised for socket- or address-related errors.
53The accompanying value is either a string telling what went wrong or a
54pair \code{(\var{errno}, \var{string})}
55representing an error returned by a system
Guido van Rossum8e1e68d1998-02-06 15:18:25 +000056call, similar to the value accompanying \code{os.error}.
57See the module \module{errno}\refbimodindex{errno}, which contains
58names for the error codes defined by the underlying operating system.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059\end{excdesc}
60
61\begin{datadesc}{AF_UNIX}
62\dataline{AF_INET}
63These constants represent the address (and protocol) families,
Fred Draked883ca11998-03-10 05:20:33 +000064used for the first argument to \function{socket()}. If the
65\constant{AF_UNIX} constant is not defined then this protocol is
66unsupported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067\end{datadesc}
68
69\begin{datadesc}{SOCK_STREAM}
70\dataline{SOCK_DGRAM}
Guido van Rossum781db5d1994-08-05 13:37:36 +000071\dataline{SOCK_RAW}
72\dataline{SOCK_RDM}
73\dataline{SOCK_SEQPACKET}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000074These constants represent the socket types,
Fred Draked883ca11998-03-10 05:20:33 +000075used for the second argument to \function{socket()}.
76(Only \constant{SOCK_STREAM} and
77\constant{SOCK_DGRAM} appear to be generally useful.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000078\end{datadesc}
79
Guido van Rossumed2bad81995-02-16 16:29:18 +000080\begin{datadesc}{SO_*}
81\dataline{SOMAXCONN}
82\dataline{MSG_*}
83\dataline{SOL_*}
84\dataline{IPPROTO_*}
85\dataline{IPPORT_*}
86\dataline{INADDR_*}
87\dataline{IP_*}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000088Many constants of these forms, documented in the \UNIX{} documentation on
Guido van Rossumed2bad81995-02-16 16:29:18 +000089sockets and/or the IP protocol, are also defined in the socket module.
Fred Draked883ca11998-03-10 05:20:33 +000090They are generally used in arguments to the \method{setsockopt()} and
91\method{getsockopt()} methods of socket objects. In most cases, only
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000092those symbols that are defined in the \UNIX{} header files are defined;
Guido van Rossumed2bad81995-02-16 16:29:18 +000093for a few symbols, default values are provided.
94\end{datadesc}
95
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000096\begin{funcdesc}{gethostbyname}{hostname}
97Translate a host name to IP address format. The IP address is
98returned as a string, e.g., \code{'100.50.200.5'}. If the host name
99is an IP address itself it is returned unchanged.
100\end{funcdesc}
101
Guido van Rossum781db5d1994-08-05 13:37:36 +0000102\begin{funcdesc}{gethostname}{}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000103Return a string containing the hostname of the machine where
104the Python interpreter is currently executing. If you want to know the
Fred Draked883ca11998-03-10 05:20:33 +0000105current machine's IP address, use \code{gethostbyname(gethostname())}.
106Note: \function{gethostname()} doesn't always return the fully qualified
107domain name; use \code{gethostbyaddr(gethostname())}
Guido van Rossumfe27a501997-01-11 17:04:56 +0000108(see below).
Guido van Rossum31cce971995-01-04 19:17:34 +0000109\end{funcdesc}
110
111\begin{funcdesc}{gethostbyaddr}{ip_address}
Fred Draked883ca11998-03-10 05:20:33 +0000112Return a triple \code{(\var{hostname}, \var{aliaslist},
113\var{ipaddrlist})} where \var{hostname} is the primary host name
114responding to the given \var{ip_address}, \var{aliaslist} is a
115(possibly empty) list of alternative host names for the same address,
116and \var{ipaddrlist} is a list of IP addresses for the same interface
117on the same host (most likely containing only a single address).
Guido van Rossumfe27a501997-01-11 17:04:56 +0000118To find the fully qualified domain name, check \var{hostname} and the
119items of \var{aliaslist} for an entry containing at least one period.
Guido van Rossum781db5d1994-08-05 13:37:36 +0000120\end{funcdesc}
121
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000122\begin{funcdesc}{getprotobyname}{protocolname}
123Translate an Internet protocol name (e.g. \code{'icmp'}) to a constant
124suitable for passing as the (optional) third argument to the
Fred Draked883ca11998-03-10 05:20:33 +0000125\function{socket()} function. This is usually only needed for sockets
126opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket
127modes, the correct protocol is chosen automatically if the protocol is
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000128omitted or zero.
129\end{funcdesc}
130
Fred Draked883ca11998-03-10 05:20:33 +0000131\begin{funcdesc}{getservbyname}{servicename, protocolname}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132Translate an Internet service name and protocol name to a port number
133for that service. The protocol name should be \code{'tcp'} or
134\code{'udp'}.
135\end{funcdesc}
136
Fred Draked883ca11998-03-10 05:20:33 +0000137\begin{funcdesc}{socket}{family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000138Create a new socket using the given address family, socket type and
Fred Draked883ca11998-03-10 05:20:33 +0000139protocol number. The address family should be \constant{AF_INET} or
140\constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM},
141\constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000142The protocol number is usually zero and may be omitted in that case.
143\end{funcdesc}
144
Fred Draked883ca11998-03-10 05:20:33 +0000145\begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000146Build a socket object from an existing file descriptor (an integer as
Fred Draked883ca11998-03-10 05:20:33 +0000147returned by a file object's \method{fileno()} method). Address family,
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000148socket type and protocol number are as for the \code{socket} function
149above. The file descriptor should refer to a socket, but this is not
150checked --- subsequent operations on the object may fail if the file
151descriptor is invalid. This function is rarely needed, but can be
152used to get or set socket options on a socket passed to a program as
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000153standard input or output (e.g.\ a server started by the \UNIX{} inet
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000154daemon).
155\end{funcdesc}
156
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000157\begin{funcdesc}{ntohl}{x}
Fred Drakec5aec051997-12-08 21:25:41 +0000158Convert 32-bit integers from network to host byte order. On machines
159where the host byte order is the same as network byte order, this is a
160no-op; otherwise, it performs a 4-byte swap operation.
161\end{funcdesc}
162
163\begin{funcdesc}{ntohs}{x}
164Convert 16-bit integers from network to host byte order. On machines
165where the host byte order is the same as network byte order, this is a
166no-op; otherwise, it performs a 2-byte swap operation.
167\end{funcdesc}
168
169\begin{funcdesc}{htonl}{x}
170Convert 32-bit integers from host to network byte order. On machines
171where the host byte order is the same as network byte order, this is a
172no-op; otherwise, it performs a 4-byte swap operation.
173\end{funcdesc}
174
175\begin{funcdesc}{htons}{x}
176Convert 16-bit integers from host to network byte order. On machines
177where the host byte order is the same as network byte order, this is a
178no-op; otherwise, it performs a 2-byte swap operation.
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000179\end{funcdesc}
180
Fred Drake5451d671997-10-13 21:31:02 +0000181\begin{datadesc}{SocketType}
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000182This is a Python type object that represents the socket object type.
Fred Draked883ca11998-03-10 05:20:33 +0000183It is the same as \code{type(socket(...))}.
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000184\end{datadesc}
185
Guido van Rossum470be141995-03-17 16:07:09 +0000186\subsection{Socket Objects}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000187
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000188Socket objects have the following methods. Except for
Fred Draked883ca11998-03-10 05:20:33 +0000189\method{makefile()} these correspond to \UNIX{} system calls
190applicable to sockets.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000191
Fred Drake3f1c4721998-04-03 07:04:45 +0000192\begin{methoddesc}[socket]{accept}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000193Accept a connection.
194The socket must be bound to an address and listening for connections.
195The return value is a pair \code{(\var{conn}, \var{address})}
196where \var{conn} is a \emph{new} socket object usable to send and
197receive data on the connection, and \var{address} is the address bound
198to the socket on the other end of the connection.
Fred Drake3f1c4721998-04-03 07:04:45 +0000199\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000200
Fred Drake3f1c4721998-04-03 07:04:45 +0000201\begin{methoddesc}[socket]{bind}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000202Bind the socket to \var{address}. The socket must not already be bound.
Guido van Rossum86751151995-02-28 17:14:32 +0000203(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000204\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000205
Fred Drake3f1c4721998-04-03 07:04:45 +0000206\begin{methoddesc}[socket]{close}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000207Close the socket. All future operations on the socket object will fail.
208The remote end will receive no more data (after queued data is flushed).
209Sockets are automatically closed when they are garbage-collected.
Fred Drake3f1c4721998-04-03 07:04:45 +0000210\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000211
Fred Drake3f1c4721998-04-03 07:04:45 +0000212\begin{methoddesc}[socket]{connect}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000213Connect to a remote socket at \var{address}.
Fred Draked883ca11998-03-10 05:20:33 +0000214(The format of \var{address} depends on the address family --- see
215above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000216\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000217
Fred Drake3f1c4721998-04-03 07:04:45 +0000218\begin{methoddesc}[socket]{connect_ex}{address}
Guido van Rossumeefcba61997-12-09 19:47:24 +0000219Like \code{connect(\var{address})}, but return an error indicator
Guido van Rossumf7790c61997-11-18 15:29:20 +0000220instead of raising an exception. The error indicator is 0 if the
Fred Draked883ca11998-03-10 05:20:33 +0000221operation succeeded, otherwise the value of the \cdata{errno}
Fred Drake3f1c4721998-04-03 07:04:45 +0000222variable. This is useful, e.g., for asynchronous connects.
223\end{methoddesc}
Guido van Rossumf7790c61997-11-18 15:29:20 +0000224
Fred Drake3f1c4721998-04-03 07:04:45 +0000225\begin{methoddesc}[socket]{fileno}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000226Return the socket's file descriptor (a small integer). This is useful
Fred Draked883ca11998-03-10 05:20:33 +0000227with \function{select.select()}.
Fred Drake3f1c4721998-04-03 07:04:45 +0000228\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000229
Fred Drake3f1c4721998-04-03 07:04:45 +0000230\begin{methoddesc}[socket]{getpeername}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000231Return the remote address to which the socket is connected. This is
232useful to find out the port number of a remote IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000233(The format of the address returned depends on the address family ---
Guido van Rossum781db5d1994-08-05 13:37:36 +0000234see above.) On some systems this function is not supported.
Fred Drake3f1c4721998-04-03 07:04:45 +0000235\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000236
Fred Drake3f1c4721998-04-03 07:04:45 +0000237\begin{methoddesc}[socket]{getsockname}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000238Return the socket's own address. This is useful to find out the port
239number of an IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000240(The format of the address returned depends on the address family ---
Guido van Rossuma84ec511994-06-23 12:13:52 +0000241see above.)
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]{getsockopt}{level, optname\optional{, buflen}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000245Return the value of the given socket option (see the \UNIX{} man page
Fred Draked883ca11998-03-10 05:20:33 +0000246\manpage{getsockopt}{2}). The needed symbolic constants
247(\constant{SO_*} etc.) are defined in this module. If \var{buflen}
Guido van Rossum470be141995-03-17 16:07:09 +0000248is absent, an integer option is assumed and its integer value
Guido van Rossum8df36371995-02-27 17:52:15 +0000249is returned by the function. If \var{buflen} is present, it specifies
250the maximum length of the buffer used to receive the option in, and
Guido van Rossum470be141995-03-17 16:07:09 +0000251this buffer is returned as a string. It is up to the caller to decode
Guido van Rossum8df36371995-02-27 17:52:15 +0000252the contents of the buffer (see the optional built-in module
Fred Draked883ca11998-03-10 05:20:33 +0000253\module{struct} for a way to decode C structures encoded as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000254\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000255
Fred Drake3f1c4721998-04-03 07:04:45 +0000256\begin{methoddesc}[socket]{listen}{backlog}
Guido van Rossum470be141995-03-17 16:07:09 +0000257Listen for connections made to the socket. The \var{backlog} argument
258specifies the maximum number of queued connections and should be at
259least 1; the maximum value is system-dependent (usually 5).
Fred Drake3f1c4721998-04-03 07:04:45 +0000260\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000261
Fred Drake3f1c4721998-04-03 07:04:45 +0000262\begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}}
Guido van Rossum470be141995-03-17 16:07:09 +0000263Return a \dfn{file object} associated with the socket. (File objects
Fred Draked883ca11998-03-10 05:20:33 +0000264were described earlier in \ref{bltin-file-objects}, ``File Objects.'')
265The file object references a \cfunction{dup()}ped version of the
266socket file descriptor, so the file object and socket object may be
267closed or garbage-collected independently. The optional \var{mode}
268and \var{bufsize} arguments are interpreted the same way as by the
269built-in \function{open()} function.
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]{recv}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000273Receive data from the socket. The return value is a string representing
274the data received. The maximum amount of data to be received
275at once is specified by \var{bufsize}. See the \UNIX{} manual page
Fred Draked883ca11998-03-10 05:20:33 +0000276\manpage{recv}{2} for the meaning of the optional argument
277\var{flags}; it defaults to zero.
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]{recvfrom}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000281Receive data from the socket. The return value is a pair
282\code{(\var{string}, \var{address})} where \var{string} is a string
283representing the data received and \var{address} is the address of the
Guido van Rossum470be141995-03-17 16:07:09 +0000284socket sending the data. The optional \var{flags} argument has the
Fred Draked883ca11998-03-10 05:20:33 +0000285same meaning as for \method{recv()} above.
Guido van Rossum86751151995-02-28 17:14:32 +0000286(The format of \var{address} depends on the address family --- see above.)
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]{send}{string\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000290Send data to the socket. The socket must be connected to a remote
Guido van Rossum470be141995-03-17 16:07:09 +0000291socket. The optional \var{flags} argument has the same meaning as for
Fred Draked883ca11998-03-10 05:20:33 +0000292\method{recv()} above. Returns the number of bytes sent.
Fred Drake3f1c4721998-04-03 07:04:45 +0000293\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000294
Fred Drake3f1c4721998-04-03 07:04:45 +0000295\begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000296Send data to the socket. The socket should not be connected to a
297remote socket, since the destination socket is specified by
Fred Draked883ca11998-03-10 05:20:33 +0000298\var{address}. The optional \var{flags} argument has the same
299meaning as for \method{recv()} above. Return the number of bytes sent.
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]{setblocking}{flag}
Guido van Rossum91951481994-09-07 14:39:14 +0000304Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
305the socket is set to non-blocking, else to blocking mode. Initially
306all sockets are in blocking mode. In non-blocking mode, if a
Fred Draked883ca11998-03-10 05:20:33 +0000307\method{recv()} call doesn't find any data, or if a \code{send} call can't
308immediately dispose of the data, a \exception{error} exception is
Guido van Rossum91951481994-09-07 14:39:14 +0000309raised; in blocking mode, the calls block until they can proceed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000310\end{methoddesc}
Guido van Rossum91951481994-09-07 14:39:14 +0000311
Fred Drake3f1c4721998-04-03 07:04:45 +0000312\begin{methoddesc}[socket]{setsockopt}{level, optname, value}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000313Set the value of the given socket option (see the \UNIX{} man page
Fred Draked883ca11998-03-10 05:20:33 +0000314\manpage{setsockopt}{2}). The needed symbolic constants are defined in
315the \module{socket} module (\code{SO_*} etc.). The value can be an
Guido van Rossum8df36371995-02-27 17:52:15 +0000316integer or a string representing a buffer. In the latter case it is
317up to the caller to ensure that the string contains the proper bits
318(see the optional built-in module
Fred Draked883ca11998-03-10 05:20:33 +0000319\module{struct}\refbimodindex{struct} for a way to encode C structures
320as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000321\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000322
Fred Drake3f1c4721998-04-03 07:04:45 +0000323\begin{methoddesc}[socket]{shutdown}{how}
Fred Draked883ca11998-03-10 05:20:33 +0000324Shut down one or both halves of the connection. If \var{how} is
325\code{0}, further receives are disallowed. If \var{how} is \code{1},
326further sends are disallowed. If \var{how} is \code{2}, further sends
327and receives are disallowed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000328\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000329
Fred Draked883ca11998-03-10 05:20:33 +0000330Note that there are no methods \method{read()} or \method{write()};
331use \method{recv()} and \method{send()} without \var{flags} argument
332instead.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000333
334\subsection{Example}
335\nodename{Socket Example}
336
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000337Here are two minimal example programs using the TCP/IP protocol:\ a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000338server that echoes all data that it receives back (servicing only one
339client), and a client using it. Note that a server must perform the
Fred Draked883ca11998-03-10 05:20:33 +0000340sequence \function{socket()}, \method{bind()}, \method{listen()},
341\method{accept()} (possibly repeating the \method{accept()} to service
342more than one client), while a client only needs the sequence
343\function{socket()}, \method{connect()}. Also note that the server
344does not \method{send()}/\method{recv()} on the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000345socket it is listening on but on the new socket returned by
Fred Draked883ca11998-03-10 05:20:33 +0000346\method{accept()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000347
Fred Drake19479911998-02-13 06:58:54 +0000348\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000349# Echo server program
350from socket import *
351HOST = '' # Symbolic name meaning the local host
352PORT = 50007 # Arbitrary non-privileged server
353s = socket(AF_INET, SOCK_STREAM)
354s.bind(HOST, PORT)
Guido van Rossum5da57551994-03-02 10:52:16 +0000355s.listen(1)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000356conn, addr = s.accept()
357print 'Connected by', addr
358while 1:
359 data = conn.recv(1024)
360 if not data: break
361 conn.send(data)
362conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000363\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000364
Fred Drake19479911998-02-13 06:58:54 +0000365\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000366# Echo client program
367from socket import *
368HOST = 'daring.cwi.nl' # The remote host
369PORT = 50007 # The same port as used by the server
370s = socket(AF_INET, SOCK_STREAM)
371s.connect(HOST, PORT)
372s.send('Hello, world')
373data = s.recv(1024)
374s.close()
375print 'Received', `data`
Fred Drake19479911998-02-13 06:58:54 +0000376\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000377
Guido van Rossume47da0a1997-07-17 16:34:52 +0000378\begin{seealso}
379\seemodule{SocketServer}{classes that simplify writing network servers}
380\end{seealso}