blob: 6b44e15def96c6e2c59e248da076bb5bce65de97 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{socket} ---
Fred Drake318c0b11999-04-21 17:29:14 +00002 Low-level networking interface}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake318c0b11999-04-21 17:29:14 +00004\declaremodule{builtin}{socket}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Low-level networking interface.}
6
Fred Draked883ca11998-03-10 05:20:33 +00007
Fred Drakeaf8a0151998-01-14 14:51:31 +00008This module provides access to the BSD \emph{socket} interface.
Fred Drake38e5d272000-04-03 20:13:55 +00009It is available on all modern \UNIX{} systems, Windows, MacOS, BeOS,
10OS/2, and probably additional platforms.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011
12For an introduction to socket programming (in C), see the following
Fred Drake37f15741999-11-10 16:21:37 +000013papers: \citetitle{An Introductory 4.3BSD Interprocess Communication
14Tutorial}, by Stuart Sechrest and \citetitle{An Advanced 4.3BSD
15Interprocess Communication Tutorial}, by Samuel J. Leffler et al,
16both in the \citetitle{\UNIX{} Programmer's Manual, Supplementary Documents 1}
Fred Drake38e5d272000-04-03 20:13:55 +000017(sections PS1:7 and PS1:8). The platform-specific reference material
18for the various socket-related system calls are also a valuable source
19of information on the details of socket semantics. For \UNIX, refer
20to the manual pages; for Windows, see the WinSock (or Winsock 2)
21specification.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022
23The Python interface is a straightforward transliteration of the
24\UNIX{} system call and library interface for sockets to Python's
Fred Draked883ca11998-03-10 05:20:33 +000025object-oriented style: the \function{socket()} function returns a
Fred Drake318c0b11999-04-21 17:29:14 +000026\dfn{socket object}\obindex{socket} whose methods implement the
27various socket system calls. Parameter types are somewhat
28higher-level than in the C interface: as with \method{read()} and
29\method{write()} operations on Python files, buffer allocation on
30receive operations is automatic, and buffer length is implicit on send
31operations.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032
33Socket addresses are represented as a single string for the
Fred Draked883ca11998-03-10 05:20:33 +000034\constant{AF_UNIX} address family and as a pair
35\code{(\var{host}, \var{port})} for the \constant{AF_INET} address
36family, where \var{host} is a string representing
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000037either a hostname in Internet domain notation like
38\code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'},
39and \var{port} is an integral port number. Other address families are
40currently not supported. The address format required by a particular
41socket object is automatically selected based on the address family
42specified when the socket object was created.
43
Guido van Rossume4f347e1997-05-09 02:21:51 +000044For IP addresses, two special forms are accepted instead of a host
Fred Draked883ca11998-03-10 05:20:33 +000045address: the empty string represents \constant{INADDR_ANY}, and the string
Fred Drake318c0b11999-04-21 17:29:14 +000046\code{'<broadcast>'} represents \constant{INADDR_BROADCAST}.
Guido van Rossume4f347e1997-05-09 02:21:51 +000047
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048All errors raise exceptions. The normal exceptions for invalid
49argument types and out-of-memory conditions can be raised; errors
Fred Drake318c0b11999-04-21 17:29:14 +000050related to socket or address semantics raise the error
51\exception{socket.error}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052
Fred Drake318c0b11999-04-21 17:29:14 +000053Non-blocking mode is supported through the
54\method{setblocking()} method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055
Fred Draked883ca11998-03-10 05:20:33 +000056The module \module{socket} exports the following constants and functions:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000057
Fred Draked883ca11998-03-10 05:20:33 +000058
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059\begin{excdesc}{error}
60This exception is raised for socket- or address-related errors.
61The accompanying value is either a string telling what went wrong or a
62pair \code{(\var{errno}, \var{string})}
63representing an error returned by a system
Fred Drake318c0b11999-04-21 17:29:14 +000064call, similar to the value accompanying \exception{os.error}.
65See the module \refmodule{errno}\refbimodindex{errno}, which contains
Guido van Rossum8e1e68d1998-02-06 15:18:25 +000066names for the error codes defined by the underlying operating system.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067\end{excdesc}
68
69\begin{datadesc}{AF_UNIX}
70\dataline{AF_INET}
71These constants represent the address (and protocol) families,
Fred Draked883ca11998-03-10 05:20:33 +000072used for the first argument to \function{socket()}. If the
73\constant{AF_UNIX} constant is not defined then this protocol is
74unsupported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075\end{datadesc}
76
77\begin{datadesc}{SOCK_STREAM}
78\dataline{SOCK_DGRAM}
Guido van Rossum781db5d1994-08-05 13:37:36 +000079\dataline{SOCK_RAW}
80\dataline{SOCK_RDM}
81\dataline{SOCK_SEQPACKET}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082These constants represent the socket types,
Fred Draked883ca11998-03-10 05:20:33 +000083used for the second argument to \function{socket()}.
84(Only \constant{SOCK_STREAM} and
85\constant{SOCK_DGRAM} appear to be generally useful.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000086\end{datadesc}
87
Guido van Rossumed2bad81995-02-16 16:29:18 +000088\begin{datadesc}{SO_*}
89\dataline{SOMAXCONN}
90\dataline{MSG_*}
91\dataline{SOL_*}
92\dataline{IPPROTO_*}
93\dataline{IPPORT_*}
94\dataline{INADDR_*}
95\dataline{IP_*}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000096Many constants of these forms, documented in the \UNIX{} documentation on
Guido van Rossumed2bad81995-02-16 16:29:18 +000097sockets and/or the IP protocol, are also defined in the socket module.
Fred Draked883ca11998-03-10 05:20:33 +000098They are generally used in arguments to the \method{setsockopt()} and
99\method{getsockopt()} methods of socket objects. In most cases, only
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000100those symbols that are defined in the \UNIX{} header files are defined;
Guido van Rossumed2bad81995-02-16 16:29:18 +0000101for a few symbols, default values are provided.
102\end{datadesc}
103
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000104\begin{funcdesc}{gethostbyname}{hostname}
105Translate a host name to IP address format. The IP address is
106returned as a string, e.g., \code{'100.50.200.5'}. If the host name
Guido van Rossumcdf6af11998-08-07 18:07:36 +0000107is an IP address itself it is returned unchanged. See
Fred Drake318c0b11999-04-21 17:29:14 +0000108\function{gethostbyname_ex()} for a more complete interface.
Guido van Rossumcdf6af11998-08-07 18:07:36 +0000109\end{funcdesc}
110
111\begin{funcdesc}{gethostbyname_ex}{hostname}
112Translate a host name to IP address format, extended interface.
113Return a triple \code{(hostname, aliaslist, ipaddrlist)} where
114\code{hostname} is the primary host name responding to the given
115\var{ip_address}, \code{aliaslist} is a (possibly empty) list of
116alternative host names for the same address, and \code{ipaddrlist} is
117a list of IP addresses for the same interface on the same
118host (often but not always a single address).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000119\end{funcdesc}
120
Guido van Rossum781db5d1994-08-05 13:37:36 +0000121\begin{funcdesc}{gethostname}{}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000122Return a string containing the hostname of the machine where
123the Python interpreter is currently executing. If you want to know the
Fred Draked883ca11998-03-10 05:20:33 +0000124current machine's IP address, use \code{gethostbyname(gethostname())}.
125Note: \function{gethostname()} doesn't always return the fully qualified
126domain name; use \code{gethostbyaddr(gethostname())}
Guido van Rossumfe27a501997-01-11 17:04:56 +0000127(see below).
Guido van Rossum31cce971995-01-04 19:17:34 +0000128\end{funcdesc}
129
130\begin{funcdesc}{gethostbyaddr}{ip_address}
Fred Draked883ca11998-03-10 05:20:33 +0000131Return a triple \code{(\var{hostname}, \var{aliaslist},
132\var{ipaddrlist})} where \var{hostname} is the primary host name
133responding to the given \var{ip_address}, \var{aliaslist} is a
134(possibly empty) list of alternative host names for the same address,
135and \var{ipaddrlist} is a list of IP addresses for the same interface
136on the same host (most likely containing only a single address).
Guido van Rossumfe27a501997-01-11 17:04:56 +0000137To find the fully qualified domain name, check \var{hostname} and the
138items of \var{aliaslist} for an entry containing at least one period.
Guido van Rossum781db5d1994-08-05 13:37:36 +0000139\end{funcdesc}
140
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000141\begin{funcdesc}{getprotobyname}{protocolname}
Fred Drake318c0b11999-04-21 17:29:14 +0000142Translate an Internet protocol name (e.g.\ \code{'icmp'}) to a constant
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000143suitable for passing as the (optional) third argument to the
Fred Draked883ca11998-03-10 05:20:33 +0000144\function{socket()} function. This is usually only needed for sockets
145opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket
146modes, the correct protocol is chosen automatically if the protocol is
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000147omitted or zero.
148\end{funcdesc}
149
Fred Draked883ca11998-03-10 05:20:33 +0000150\begin{funcdesc}{getservbyname}{servicename, protocolname}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000151Translate an Internet service name and protocol name to a port number
152for that service. The protocol name should be \code{'tcp'} or
153\code{'udp'}.
154\end{funcdesc}
155
Fred Draked883ca11998-03-10 05:20:33 +0000156\begin{funcdesc}{socket}{family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000157Create a new socket using the given address family, socket type and
Fred Draked883ca11998-03-10 05:20:33 +0000158protocol number. The address family should be \constant{AF_INET} or
159\constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM},
160\constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000161The protocol number is usually zero and may be omitted in that case.
162\end{funcdesc}
163
Fred Draked883ca11998-03-10 05:20:33 +0000164\begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000165Build a socket object from an existing file descriptor (an integer as
Fred Draked883ca11998-03-10 05:20:33 +0000166returned by a file object's \method{fileno()} method). Address family,
Fred Drake318c0b11999-04-21 17:29:14 +0000167socket type and protocol number are as for the \function{socket()} function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000168above. The file descriptor should refer to a socket, but this is not
169checked --- subsequent operations on the object may fail if the file
170descriptor is invalid. This function is rarely needed, but can be
171used to get or set socket options on a socket passed to a program as
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000172standard input or output (e.g.\ a server started by the \UNIX{} inet
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000173daemon).
174\end{funcdesc}
175
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000176\begin{funcdesc}{ntohl}{x}
Fred Drakec5aec051997-12-08 21:25:41 +0000177Convert 32-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 4-byte swap operation.
180\end{funcdesc}
181
182\begin{funcdesc}{ntohs}{x}
183Convert 16-bit integers from network to host 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 2-byte swap operation.
186\end{funcdesc}
187
188\begin{funcdesc}{htonl}{x}
189Convert 32-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 4-byte swap operation.
192\end{funcdesc}
193
194\begin{funcdesc}{htons}{x}
195Convert 16-bit integers from host to network byte order. On machines
196where the host byte order is the same as network byte order, this is a
197no-op; otherwise, it performs a 2-byte swap operation.
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000198\end{funcdesc}
199
Fred Drakee6fb1c41999-09-16 15:50:00 +0000200\begin{funcdesc}{inet_aton}{ip_string}
201Convert an IP address from dotted-quad string format
202(e.g.\ '123.45.67.89') to 32-bit packed binary format, as a string four
203characters in length.
204
205Useful when conversing with a program that uses the standard C library
206and needs objects of type \ctype{struct in_addr}, which is the C type
207for the 32-bit packed binary this function returns.
208
209If the IP address string passed to this function is invalid,
210\exception{socket.error} will be raised. Note that exactly what is
211valid depends on the underlying C implementation of
212\cfunction{inet_aton()}.
213\end{funcdesc}
214
215\begin{funcdesc}{inet_ntoa}{packed_ip}
216Convert a 32-bit packed IP address (a string four characters in
217length) to its standard dotted-quad string representation
218(e.g. '123.45.67.89').
219
220Useful when conversing with a program that uses the standard C library
221and needs objects of type \ctype{struct in_addr}, which is the C type
222for the 32-bit packed binary this function takes as an argument.
223
224If the string passed to this function is not exactly 4 bytes in
225length, \exception{socket.error} will be raised.
226\end{funcdesc}
227
Fred Drake5451d671997-10-13 21:31:02 +0000228\begin{datadesc}{SocketType}
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000229This is a Python type object that represents the socket object type.
Fred Draked883ca11998-03-10 05:20:33 +0000230It is the same as \code{type(socket(...))}.
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000231\end{datadesc}
232
Fred Drakeaa7524c2000-07-06 18:37:08 +0000233
234\begin{seealso}
235 \seemodule{SocketServer}{Classes that simplify writing network servers.}
236\end{seealso}
237
238
Fred Drakea94f6761999-08-05 13:41:04 +0000239\subsection{Socket Objects \label{socket-objects}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000240
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000241Socket objects have the following methods. Except for
Fred Draked883ca11998-03-10 05:20:33 +0000242\method{makefile()} these correspond to \UNIX{} system calls
243applicable to sockets.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000244
Fred Drake3f1c4721998-04-03 07:04:45 +0000245\begin{methoddesc}[socket]{accept}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000246Accept a connection.
247The socket must be bound to an address and listening for connections.
248The return value is a pair \code{(\var{conn}, \var{address})}
249where \var{conn} is a \emph{new} socket object usable to send and
250receive data on the connection, and \var{address} is the address bound
251to the socket on the other end of the connection.
Fred Drake3f1c4721998-04-03 07:04:45 +0000252\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000253
Fred Drake3f1c4721998-04-03 07:04:45 +0000254\begin{methoddesc}[socket]{bind}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000255Bind the socket to \var{address}. The socket must not already be bound.
Fred Drake7d686902000-04-04 17:48:30 +0000256(The format of \var{address} depends on the address family --- see
257above.) \strong{Note:} This method has historically accepted a pair
258of parameters for \constant{AF_INET} addresses instead of only a
259tuple. This was never intentional and will no longer be available in
260Python 1.7.
Fred Drake3f1c4721998-04-03 07:04:45 +0000261\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000262
Fred Drake3f1c4721998-04-03 07:04:45 +0000263\begin{methoddesc}[socket]{close}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000264Close the socket. All future operations on the socket object will fail.
265The remote end will receive no more data (after queued data is flushed).
266Sockets are automatically closed when they are garbage-collected.
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]{connect}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000270Connect to a remote socket at \var{address}.
Fred Draked883ca11998-03-10 05:20:33 +0000271(The format of \var{address} depends on the address family --- see
Fred Drake7d686902000-04-04 17:48:30 +0000272above.) \strong{Note:} This method has historically accepted a pair
273of parameters for \constant{AF_INET} addresses instead of only a
274tuple. This was never intentional and will no longer be available in
275Python 1.7.
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]{connect_ex}{address}
Guido van Rossumeefcba61997-12-09 19:47:24 +0000279Like \code{connect(\var{address})}, but return an error indicator
Fred Drakeb0bc7f21999-05-06 22:03:50 +0000280instead of raising an exception for errors returned by the C-level
281\cfunction{connect()} call (other problems, such as ``host not found,''
282can still raise exceptions). The error indicator is \code{0} if the
Fred Draked883ca11998-03-10 05:20:33 +0000283operation succeeded, otherwise the value of the \cdata{errno}
Fred Drake3f1c4721998-04-03 07:04:45 +0000284variable. This is useful, e.g., for asynchronous connects.
Fred Drake7d686902000-04-04 17:48:30 +0000285\strong{Note:} This method has historically accepted a pair of
286parameters for \constant{AF_INET} addresses instead of only a tuple.
287This was never intentional and will no longer be available in Python
2881.7.
Fred Drake3f1c4721998-04-03 07:04:45 +0000289\end{methoddesc}
Guido van Rossumf7790c61997-11-18 15:29:20 +0000290
Fred Drake3f1c4721998-04-03 07:04:45 +0000291\begin{methoddesc}[socket]{fileno}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000292Return the socket's file descriptor (a small integer). This is useful
Fred Draked883ca11998-03-10 05:20:33 +0000293with \function{select.select()}.
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]{getpeername}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000297Return the remote address to which the socket is connected. This is
298useful to find out the port number of a remote IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000299(The format of the address returned depends on the address family ---
Guido van Rossum781db5d1994-08-05 13:37:36 +0000300see above.) On some systems this function is not supported.
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]{getsockname}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000304Return the socket's own address. This is useful to find out the port
305number of an IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000306(The format of the address returned depends on the address family ---
Guido van Rossuma84ec511994-06-23 12:13:52 +0000307see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000308\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000309
Fred Drake3f1c4721998-04-03 07:04:45 +0000310\begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000311Return the value of the given socket option (see the \UNIX{} man page
Fred Draked883ca11998-03-10 05:20:33 +0000312\manpage{getsockopt}{2}). The needed symbolic constants
313(\constant{SO_*} etc.) are defined in this module. If \var{buflen}
Guido van Rossum470be141995-03-17 16:07:09 +0000314is absent, an integer option is assumed and its integer value
Guido van Rossum8df36371995-02-27 17:52:15 +0000315is returned by the function. If \var{buflen} is present, it specifies
316the maximum length of the buffer used to receive the option in, and
Guido van Rossum470be141995-03-17 16:07:09 +0000317this buffer is returned as a string. It is up to the caller to decode
Guido van Rossum8df36371995-02-27 17:52:15 +0000318the contents of the buffer (see the optional built-in module
Fred Drake318c0b11999-04-21 17:29:14 +0000319\refmodule{struct} for a way to decode C structures encoded as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000320\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000321
Fred Drake3f1c4721998-04-03 07:04:45 +0000322\begin{methoddesc}[socket]{listen}{backlog}
Guido van Rossum470be141995-03-17 16:07:09 +0000323Listen for connections made to the socket. The \var{backlog} argument
324specifies the maximum number of queued connections and should be at
325least 1; the maximum value is system-dependent (usually 5).
Fred Drake3f1c4721998-04-03 07:04:45 +0000326\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000327
Fred Drake3f1c4721998-04-03 07:04:45 +0000328\begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}}
Guido van Rossum470be141995-03-17 16:07:09 +0000329Return a \dfn{file object} associated with the socket. (File objects
Fred Drakea94f6761999-08-05 13:41:04 +0000330are described in \ref{bltin-file-objects}, ``File Objects.'')
Fred Draked883ca11998-03-10 05:20:33 +0000331The file object references a \cfunction{dup()}ped version of the
332socket file descriptor, so the file object and socket object may be
Fred Drakea94f6761999-08-05 13:41:04 +0000333closed or garbage-collected independently.
334\index{I/O control!buffering}The optional \var{mode}
Fred Draked883ca11998-03-10 05:20:33 +0000335and \var{bufsize} arguments are interpreted the same way as by the
336built-in \function{open()} function.
Fred Drake3f1c4721998-04-03 07:04:45 +0000337\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000338
Fred Drake3f1c4721998-04-03 07:04:45 +0000339\begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000340Receive data from the socket. The return value is a string representing
341the data received. The maximum amount of data to be received
342at once is specified by \var{bufsize}. See the \UNIX{} manual page
Fred Draked883ca11998-03-10 05:20:33 +0000343\manpage{recv}{2} for the meaning of the optional argument
344\var{flags}; it defaults to zero.
Fred Drake3f1c4721998-04-03 07:04:45 +0000345\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000346
Fred Drake3f1c4721998-04-03 07:04:45 +0000347\begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000348Receive data from the socket. The return value is a pair
349\code{(\var{string}, \var{address})} where \var{string} is a string
350representing the data received and \var{address} is the address of the
Guido van Rossum470be141995-03-17 16:07:09 +0000351socket sending the data. The optional \var{flags} argument has the
Fred Draked883ca11998-03-10 05:20:33 +0000352same meaning as for \method{recv()} above.
Guido van Rossum86751151995-02-28 17:14:32 +0000353(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000354\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000355
Fred Drake3f1c4721998-04-03 07:04:45 +0000356\begin{methoddesc}[socket]{send}{string\optional{, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000357Send data to the socket. The socket must be connected to a remote
Guido van Rossum470be141995-03-17 16:07:09 +0000358socket. The optional \var{flags} argument has the same meaning as for
Fred Draked883ca11998-03-10 05:20:33 +0000359\method{recv()} above. Returns the number of bytes sent.
Fred Drake3f1c4721998-04-03 07:04:45 +0000360\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000361
Fred Drake3f1c4721998-04-03 07:04:45 +0000362\begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000363Send data to the socket. The socket should not be connected to a
364remote socket, since the destination socket is specified by
Fred Draked883ca11998-03-10 05:20:33 +0000365\var{address}. The optional \var{flags} argument has the same
366meaning as for \method{recv()} above. Return the number of bytes sent.
Guido van Rossum86751151995-02-28 17:14:32 +0000367(The format of \var{address} depends on the address family --- see above.)
Fred Drake3f1c4721998-04-03 07:04:45 +0000368\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000369
Fred Drake3f1c4721998-04-03 07:04:45 +0000370\begin{methoddesc}[socket]{setblocking}{flag}
Guido van Rossum91951481994-09-07 14:39:14 +0000371Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
372the socket is set to non-blocking, else to blocking mode. Initially
373all sockets are in blocking mode. In non-blocking mode, if a
Fred Drake318c0b11999-04-21 17:29:14 +0000374\method{recv()} call doesn't find any data, or if a
375\method{send()} call can't immediately dispose of the data, a
376\exception{error} exception is raised; in blocking mode, the calls
377block until they can proceed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000378\end{methoddesc}
Guido van Rossum91951481994-09-07 14:39:14 +0000379
Fred Drake3f1c4721998-04-03 07:04:45 +0000380\begin{methoddesc}[socket]{setsockopt}{level, optname, value}
Fred Drake9a748aa2000-06-30 04:21:41 +0000381Set the value of the given socket option (see the \UNIX{} manual page
Fred Draked883ca11998-03-10 05:20:33 +0000382\manpage{setsockopt}{2}). The needed symbolic constants are defined in
383the \module{socket} module (\code{SO_*} etc.). The value can be an
Guido van Rossum8df36371995-02-27 17:52:15 +0000384integer or a string representing a buffer. In the latter case it is
385up to the caller to ensure that the string contains the proper bits
386(see the optional built-in module
Fred Drake318c0b11999-04-21 17:29:14 +0000387\refmodule{struct}\refbimodindex{struct} for a way to encode C
388structures as strings).
Fred Drake3f1c4721998-04-03 07:04:45 +0000389\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000390
Fred Drake3f1c4721998-04-03 07:04:45 +0000391\begin{methoddesc}[socket]{shutdown}{how}
Fred Draked883ca11998-03-10 05:20:33 +0000392Shut down one or both halves of the connection. If \var{how} is
393\code{0}, further receives are disallowed. If \var{how} is \code{1},
394further sends are disallowed. If \var{how} is \code{2}, further sends
395and receives are disallowed.
Fred Drake3f1c4721998-04-03 07:04:45 +0000396\end{methoddesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000397
Fred Draked883ca11998-03-10 05:20:33 +0000398Note that there are no methods \method{read()} or \method{write()};
399use \method{recv()} and \method{send()} without \var{flags} argument
400instead.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000401
Fred Drakeaa7524c2000-07-06 18:37:08 +0000402
403\subsection{Example \label{socket-example}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000404
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000405Here are two minimal example programs using the TCP/IP protocol:\ a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000406server that echoes all data that it receives back (servicing only one
407client), and a client using it. Note that a server must perform the
Fred Draked883ca11998-03-10 05:20:33 +0000408sequence \function{socket()}, \method{bind()}, \method{listen()},
409\method{accept()} (possibly repeating the \method{accept()} to service
410more than one client), while a client only needs the sequence
411\function{socket()}, \method{connect()}. Also note that the server
412does not \method{send()}/\method{recv()} on the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000413socket it is listening on but on the new socket returned by
Fred Draked883ca11998-03-10 05:20:33 +0000414\method{accept()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000415
Fred Drake19479911998-02-13 06:58:54 +0000416\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000417# Echo server program
418from socket import *
419HOST = '' # Symbolic name meaning the local host
420PORT = 50007 # Arbitrary non-privileged server
421s = socket(AF_INET, SOCK_STREAM)
Fred Drake3d69c0e2000-05-03 19:40:32 +0000422s.bind((HOST, PORT))
Guido van Rossum5da57551994-03-02 10:52:16 +0000423s.listen(1)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000424conn, addr = s.accept()
425print 'Connected by', addr
426while 1:
427 data = conn.recv(1024)
428 if not data: break
429 conn.send(data)
430conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000431\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000432
Fred Drake19479911998-02-13 06:58:54 +0000433\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000434# Echo client program
435from socket import *
436HOST = 'daring.cwi.nl' # The remote host
437PORT = 50007 # The same port as used by the server
438s = socket(AF_INET, SOCK_STREAM)
Fred Drake3d69c0e2000-05-03 19:40:32 +0000439s.connect((HOST, PORT))
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000440s.send('Hello, world')
441data = s.recv(1024)
442s.close()
443print 'Received', `data`
Fred Drake19479911998-02-13 06:58:54 +0000444\end{verbatim}