blob: 3b6cee09c579ff0ecc15e1a70d95ffa368c419dd [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{socket}}
2
3\bimodindex{socket}
4This module provides access to the BSD {\em socket} interface.
5It is available on \UNIX{} systems that support this interface.
6
7For an introduction to socket programming (in C), see the following
8papers: \emph{An Introductory 4.3BSD Interprocess Communication
9Tutorial}, by Stuart Sechrest and \emph{An Advanced 4.3BSD Interprocess
10Communication Tutorial}, by Samuel J. Leffler et al, both in the
11\UNIX{} Programmer's Manual, Supplementary Documents 1 (sections PS1:7
12and PS1:8). The \UNIX{} manual pages for the various socket-related
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000013system calls are also a valuable source of information on the details of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014socket semantics.
15
16The Python interface is a straightforward transliteration of the
17\UNIX{} system call and library interface for sockets to Python's
18object-oriented style: the \code{socket()} function returns a
19\dfn{socket object} whose methods implement the various socket system
Barry Warsawd44be3f1997-01-03 20:19:05 +000020calls. Parameter types are somewhat higher-level than in the C
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000021interface: as with \code{read()} and \code{write()} operations on Python
22files, buffer allocation on receive operations is automatic, and
23buffer length is implicit on send operations.
24
25Socket addresses are represented as a single string for the
26\code{AF_UNIX} address family and as a pair
27\code{(\var{host}, \var{port})} for the \code{AF_INET} address family,
28where \var{host} is a string representing
29either a hostname in Internet domain notation like
30\code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'},
31and \var{port} is an integral port number. Other address families are
32currently not supported. The address format required by a particular
33socket object is automatically selected based on the address family
34specified when the socket object was created.
35
Guido van Rossume4f347e1997-05-09 02:21:51 +000036For IP addresses, two special forms are accepted instead of a host
37address: the empty string represents \code{INADDR_ANY}, and the string
38\code{"<broadcast>"} represents \code{INADDR_BROADCAST}.
39
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000040All errors raise exceptions. The normal exceptions for invalid
41argument types and out-of-memory conditions can be raised; errors
42related to socket or address semantics raise the error \code{socket.error}.
43
Guido van Rossum470be141995-03-17 16:07:09 +000044Non-blocking mode is supported through the \code{setblocking()}
45method.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046
47The module \code{socket} exports the following constants and functions:
48
49\renewcommand{\indexsubitem}{(in module socket)}
50\begin{excdesc}{error}
51This exception is raised for socket- or address-related errors.
52The accompanying value is either a string telling what went wrong or a
53pair \code{(\var{errno}, \var{string})}
54representing an error returned by a system
55call, similar to the value accompanying \code{posix.error}.
56\end{excdesc}
57
58\begin{datadesc}{AF_UNIX}
59\dataline{AF_INET}
60These constants represent the address (and protocol) families,
Guido van Rossum781db5d1994-08-05 13:37:36 +000061used for the first argument to \code{socket()}. If the \code{AF_UNIX}
62constant is not defined then this protocol is unsupported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063\end{datadesc}
64
65\begin{datadesc}{SOCK_STREAM}
66\dataline{SOCK_DGRAM}
Guido van Rossum781db5d1994-08-05 13:37:36 +000067\dataline{SOCK_RAW}
68\dataline{SOCK_RDM}
69\dataline{SOCK_SEQPACKET}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000070These constants represent the socket types,
71used for the second argument to \code{socket()}.
Guido van Rossum781db5d1994-08-05 13:37:36 +000072(Only \code{SOCK_STREAM} and
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000073\code{SOCK_DGRAM} appear to be generally useful.)
74\end{datadesc}
75
Guido van Rossumed2bad81995-02-16 16:29:18 +000076\begin{datadesc}{SO_*}
77\dataline{SOMAXCONN}
78\dataline{MSG_*}
79\dataline{SOL_*}
80\dataline{IPPROTO_*}
81\dataline{IPPORT_*}
82\dataline{INADDR_*}
83\dataline{IP_*}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000084Many constants of these forms, documented in the \UNIX{} documentation on
Guido van Rossumed2bad81995-02-16 16:29:18 +000085sockets and/or the IP protocol, are also defined in the socket module.
86They are generally used in arguments to the \code{setsockopt} and
87\code{getsockopt} methods of socket objects. In most cases, only
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000088those symbols that are defined in the \UNIX{} header files are defined;
Guido van Rossumed2bad81995-02-16 16:29:18 +000089for a few symbols, default values are provided.
90\end{datadesc}
91
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000092\begin{funcdesc}{gethostbyname}{hostname}
93Translate a host name to IP address format. The IP address is
94returned as a string, e.g., \code{'100.50.200.5'}. If the host name
95is an IP address itself it is returned unchanged.
96\end{funcdesc}
97
Guido van Rossum781db5d1994-08-05 13:37:36 +000098\begin{funcdesc}{gethostname}{}
Guido van Rossum16d6e711994-08-08 12:30:22 +000099Return a string containing the hostname of the machine where
100the Python interpreter is currently executing. If you want to know the
101current machine's IP address, use
Guido van Rossum470be141995-03-17 16:07:09 +0000102\code{socket.gethostbyname(socket.gethostname())}.
Guido van Rossumfe27a501997-01-11 17:04:56 +0000103Note: \code{gethostname()} doesn't always return the fully qualified
104domain name; use \code{socket.gethostbyaddr(socket.gethostname())}
105(see below).
Guido van Rossum31cce971995-01-04 19:17:34 +0000106\end{funcdesc}
107
108\begin{funcdesc}{gethostbyaddr}{ip_address}
109Return a triple \code{(hostname, aliaslist, ipaddrlist)} where
110\code{hostname} is the primary host name responding to the given
111\var{ip_address}, \code{aliaslist} is a (possibly empty) list of
112alternative host names for the same address, and \code{ipaddrlist} is
113a list of IP addresses for the same interface on the same
114host (most likely containing only a single address).
Guido van Rossumfe27a501997-01-11 17:04:56 +0000115To find the fully qualified domain name, check \var{hostname} and the
116items of \var{aliaslist} for an entry containing at least one period.
Guido van Rossum781db5d1994-08-05 13:37:36 +0000117\end{funcdesc}
118
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000119\begin{funcdesc}{getprotobyname}{protocolname}
120Translate an Internet protocol name (e.g. \code{'icmp'}) to a constant
121suitable for passing as the (optional) third argument to the
122\code{socket()} function. This is usually only needed for sockets
123opened in ``raw'' mode (\code{SOCK_RAW}); for the normal socket modes,
124the correct protocol is chosen automatically if the protocol is
125omitted or zero.
126\end{funcdesc}
127
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000128\begin{funcdesc}{getservbyname}{servicename\, protocolname}
129Translate an Internet service name and protocol name to a port number
130for that service. The protocol name should be \code{'tcp'} or
131\code{'udp'}.
132\end{funcdesc}
133
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000134\begin{funcdesc}{socket}{family\, type\optional{\, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000135Create a new socket using the given address family, socket type and
136protocol number. The address family should be \code{AF_INET} or
137\code{AF_UNIX}. The socket type should be \code{SOCK_STREAM},
138\code{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
139The protocol number is usually zero and may be omitted in that case.
140\end{funcdesc}
141
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000142\begin{funcdesc}{fromfd}{fd\, family\, type\optional{\, proto}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000143Build a socket object from an existing file descriptor (an integer as
144returned by a file object's \code{fileno} method). Address family,
145socket type and protocol number are as for the \code{socket} function
146above. The file descriptor should refer to a socket, but this is not
147checked --- subsequent operations on the object may fail if the file
148descriptor is invalid. This function is rarely needed, but can be
149used to get or set socket options on a socket passed to a program as
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000150standard input or output (e.g.\ a server started by the \UNIX{} inet
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000151daemon).
152\end{funcdesc}
153
Guido van Rossumbda7ca71996-12-02 17:24:10 +0000154\begin{funcdesc}{ntohl}{x}
155\funcline{ntohs}{x}
156\funcline{htonl}{x}
157\funcline{htons}{x}
158These functions convert 32-bit (`l' suffix) and 16-bit (`s' suffix)
159integers between network and host byte order. On machines where the
160host byte order is the same as the network byte order, they are no-ops
161(assuming the values fit in the indicated size); otherwise, they
162perform 2-byte or 4-byte swap operations.
163\end{funcdesc}
164
Guido van Rossum470be141995-03-17 16:07:09 +0000165\subsection{Socket Objects}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000166
167\noindent
168Socket objects have the following methods. Except for
169\code{makefile()} these correspond to \UNIX{} system calls applicable to
170sockets.
171
172\renewcommand{\indexsubitem}{(socket method)}
173\begin{funcdesc}{accept}{}
174Accept a connection.
175The socket must be bound to an address and listening for connections.
176The return value is a pair \code{(\var{conn}, \var{address})}
177where \var{conn} is a \emph{new} socket object usable to send and
178receive data on the connection, and \var{address} is the address bound
179to the socket on the other end of the connection.
180\end{funcdesc}
181
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000182\begin{funcdesc}{bind}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000183Bind the socket to \var{address}. The socket must not already be bound.
Guido van Rossum86751151995-02-28 17:14:32 +0000184(The format of \var{address} depends on the address family --- see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000185\end{funcdesc}
186
187\begin{funcdesc}{close}{}
188Close the socket. All future operations on the socket object will fail.
189The remote end will receive no more data (after queued data is flushed).
190Sockets are automatically closed when they are garbage-collected.
191\end{funcdesc}
192
193\begin{funcdesc}{connect}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000194Connect to a remote socket at \var{address}.
Guido van Rossum86751151995-02-28 17:14:32 +0000195(The format of \var{address} depends on the address family --- see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000196\end{funcdesc}
197
198\begin{funcdesc}{fileno}{}
199Return the socket's file descriptor (a small integer). This is useful
200with \code{select}.
201\end{funcdesc}
202
203\begin{funcdesc}{getpeername}{}
204Return the remote address to which the socket is connected. This is
205useful to find out the port number of a remote IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000206(The format of the address returned depends on the address family ---
Guido van Rossum781db5d1994-08-05 13:37:36 +0000207see above.) On some systems this function is not supported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000208\end{funcdesc}
209
210\begin{funcdesc}{getsockname}{}
211Return the socket's own address. This is useful to find out the port
212number of an IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000213(The format of the address returned depends on the address family ---
Guido van Rossuma84ec511994-06-23 12:13:52 +0000214see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000215\end{funcdesc}
216
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000217\begin{funcdesc}{getsockopt}{level\, optname\optional{\, buflen}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000218Return the value of the given socket option (see the \UNIX{} man page
Guido van Rossum470be141995-03-17 16:07:09 +0000219{\it getsockopt}(2)). The needed symbolic constants (\code{SO_*} etc.)
220are defined in this module. If \var{buflen}
221is absent, an integer option is assumed and its integer value
Guido van Rossum8df36371995-02-27 17:52:15 +0000222is returned by the function. If \var{buflen} is present, it specifies
223the maximum length of the buffer used to receive the option in, and
Guido van Rossum470be141995-03-17 16:07:09 +0000224this buffer is returned as a string. It is up to the caller to decode
Guido van Rossum8df36371995-02-27 17:52:15 +0000225the contents of the buffer (see the optional built-in module
226\code{struct} for a way to decode C structures encoded as strings).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000227\end{funcdesc}
228
229\begin{funcdesc}{listen}{backlog}
Guido van Rossum470be141995-03-17 16:07:09 +0000230Listen for connections made to the socket. The \var{backlog} argument
231specifies the maximum number of queued connections and should be at
232least 1; the maximum value is system-dependent (usually 5).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000233\end{funcdesc}
234
Guido van Rossum470be141995-03-17 16:07:09 +0000235\begin{funcdesc}{makefile}{\optional{mode\optional{\, bufsize}}}
236Return a \dfn{file object} associated with the socket. (File objects
237were described earlier under Built-in Types.) The file object
238references a \code{dup()}ped version of the socket file descriptor, so
239the file object and socket object may be closed or garbage-collected
240independently. The optional \var{mode} and \var{bufsize} arguments
241are interpreted the same way as by the built-in
242\code{open()} function.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000243\end{funcdesc}
244
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000245\begin{funcdesc}{recv}{bufsize\optional{\, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000246Receive data from the socket. The return value is a string representing
247the data received. The maximum amount of data to be received
248at once is specified by \var{bufsize}. See the \UNIX{} manual page
249for the meaning of the optional argument \var{flags}; it defaults to
250zero.
251\end{funcdesc}
252
Guido van Rossum470be141995-03-17 16:07:09 +0000253\begin{funcdesc}{recvfrom}{bufsize\optional{\, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000254Receive data from the socket. The return value is a pair
255\code{(\var{string}, \var{address})} where \var{string} is a string
256representing the data received and \var{address} is the address of the
Guido van Rossum470be141995-03-17 16:07:09 +0000257socket sending the data. The optional \var{flags} argument has the
258same meaning as for \code{recv()} above.
Guido van Rossum86751151995-02-28 17:14:32 +0000259(The format of \var{address} depends on the address family --- see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000260\end{funcdesc}
261
Guido van Rossum470be141995-03-17 16:07:09 +0000262\begin{funcdesc}{send}{string\optional{\, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000263Send data to the socket. The socket must be connected to a remote
Guido van Rossum470be141995-03-17 16:07:09 +0000264socket. The optional \var{flags} argument has the same meaning as for
265\code{recv()} above. Return the number of bytes sent.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000266\end{funcdesc}
267
Guido van Rossum470be141995-03-17 16:07:09 +0000268\begin{funcdesc}{sendto}{string\optional{\, flags}\, address}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000269Send data to the socket. The socket should not be connected to a
270remote socket, since the destination socket is specified by
Guido van Rossum470be141995-03-17 16:07:09 +0000271\code{address}. The optional \var{flags} argument has the same
272meaning as for \code{recv()} above. Return the number of bytes sent.
Guido van Rossum86751151995-02-28 17:14:32 +0000273(The format of \var{address} depends on the address family --- see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000274\end{funcdesc}
275
Guido van Rossum91951481994-09-07 14:39:14 +0000276\begin{funcdesc}{setblocking}{flag}
277Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
278the socket is set to non-blocking, else to blocking mode. Initially
279all sockets are in blocking mode. In non-blocking mode, if a
280\code{recv} call doesn't find any data, or if a \code{send} call can't
281immediately dispose of the data, a \code{socket.error} exception is
282raised; in blocking mode, the calls block until they can proceed.
283\end{funcdesc}
284
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000285\begin{funcdesc}{setsockopt}{level\, optname\, value}
286Set the value of the given socket option (see the \UNIX{} man page
Guido van Rossum8df36371995-02-27 17:52:15 +0000287{\it setsockopt}(2)). The needed symbolic constants are defined in
288the \code{socket} module (\code{SO_*} etc.). The value can be an
289integer or a string representing a buffer. In the latter case it is
290up to the caller to ensure that the string contains the proper bits
291(see the optional built-in module
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000292\code{struct} for a way to encode C structures as strings).
293\end{funcdesc}
294
295\begin{funcdesc}{shutdown}{how}
296Shut down one or both halves of the connection. If \var{how} is \code{0},
297further receives are disallowed. If \var{how} is \code{1}, further sends are
298disallowed. If \var{how} is \code{2}, further sends and receives are
299disallowed.
300\end{funcdesc}
301
302Note that there are no methods \code{read()} or \code{write()}; use
303\code{recv()} and \code{send()} without \var{flags} argument instead.
304
305\subsection{Example}
306\nodename{Socket Example}
307
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000308Here are two minimal example programs using the TCP/IP protocol:\ a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000309server that echoes all data that it receives back (servicing only one
310client), and a client using it. Note that a server must perform the
311sequence \code{socket}, \code{bind}, \code{listen}, \code{accept}
312(possibly repeating the \code{accept} to service more than one client),
313while a client only needs the sequence \code{socket}, \code{connect}.
314Also note that the server does not \code{send}/\code{receive} on the
315socket it is listening on but on the new socket returned by
316\code{accept}.
317
318\bcode\begin{verbatim}
319# Echo server program
320from socket import *
321HOST = '' # Symbolic name meaning the local host
322PORT = 50007 # Arbitrary non-privileged server
323s = socket(AF_INET, SOCK_STREAM)
324s.bind(HOST, PORT)
Guido van Rossum5da57551994-03-02 10:52:16 +0000325s.listen(1)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000326conn, addr = s.accept()
327print 'Connected by', addr
328while 1:
329 data = conn.recv(1024)
330 if not data: break
331 conn.send(data)
332conn.close()
333\end{verbatim}\ecode
334
335\bcode\begin{verbatim}
336# Echo client program
337from socket import *
338HOST = 'daring.cwi.nl' # The remote host
339PORT = 50007 # The same port as used by the server
340s = socket(AF_INET, SOCK_STREAM)
341s.connect(HOST, PORT)
342s.send('Hello, world')
343data = s.recv(1024)
344s.close()
345print 'Received', `data`
346\end{verbatim}\ecode