blob: 9b71f1f65aac370829b8e17d7d6d5462ee6aa2a8 [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
4\bimodindex{socket}
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
19object-oriented style: the \code{socket()} function returns a
20\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
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022interface: as with \code{read()} and \code{write()} operations on Python
23files, buffer allocation on receive operations is automatic, and
24buffer length is implicit on send operations.
25
26Socket addresses are represented as a single string for the
27\code{AF_UNIX} address family and as a pair
28\code{(\var{host}, \var{port})} for the \code{AF_INET} address family,
29where \var{host} is a string representing
30either 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
38address: the empty string represents \code{INADDR_ANY}, and the string
39\code{"<broadcast>"} represents \code{INADDR_BROADCAST}.
40
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
48The module \code{socket} exports the following constants and functions:
49
Fred Drake19479911998-02-13 06:58:54 +000050\setindexsubitem{(in module socket)}
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,
Guido van Rossum781db5d1994-08-05 13:37:36 +000064used for the first argument to \code{socket()}. If the \code{AF_UNIX}
65constant is not defined then this protocol is unsupported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000066\end{datadesc}
67
68\begin{datadesc}{SOCK_STREAM}
69\dataline{SOCK_DGRAM}
Guido van Rossum781db5d1994-08-05 13:37:36 +000070\dataline{SOCK_RAW}
71\dataline{SOCK_RDM}
72\dataline{SOCK_SEQPACKET}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000073These constants represent the socket types,
74used for the second argument to \code{socket()}.
Guido van Rossum781db5d1994-08-05 13:37:36 +000075(Only \code{SOCK_STREAM} and
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076\code{SOCK_DGRAM} appear to be generally useful.)
77\end{datadesc}
78
Guido van Rossumed2bad81995-02-16 16:29:18 +000079\begin{datadesc}{SO_*}
80\dataline{SOMAXCONN}
81\dataline{MSG_*}
82\dataline{SOL_*}
83\dataline{IPPROTO_*}
84\dataline{IPPORT_*}
85\dataline{INADDR_*}
86\dataline{IP_*}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000087Many constants of these forms, documented in the \UNIX{} documentation on
Guido van Rossumed2bad81995-02-16 16:29:18 +000088sockets and/or the IP protocol, are also defined in the socket module.
89They are generally used in arguments to the \code{setsockopt} and
90\code{getsockopt} methods of socket objects. In most cases, only
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000091those symbols that are defined in the \UNIX{} header files are defined;
Guido van Rossumed2bad81995-02-16 16:29:18 +000092for a few symbols, default values are provided.
93\end{datadesc}
94
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095\begin{funcdesc}{gethostbyname}{hostname}
96Translate a host name to IP address format. The IP address is
97returned as a string, e.g., \code{'100.50.200.5'}. If the host name
98is an IP address itself it is returned unchanged.
99\end{funcdesc}
100
Guido van Rossum781db5d1994-08-05 13:37:36 +0000101\begin{funcdesc}{gethostname}{}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000102Return a string containing the hostname of the machine where
103the Python interpreter is currently executing. If you want to know the
104current machine's IP address, use
Guido van Rossum470be141995-03-17 16:07:09 +0000105\code{socket.gethostbyname(socket.gethostname())}.
Guido van Rossumfe27a501997-01-11 17:04:56 +0000106Note: \code{gethostname()} doesn't always return the fully qualified
107domain name; use \code{socket.gethostbyaddr(socket.gethostname())}
108(see below).
Guido van Rossum31cce971995-01-04 19:17:34 +0000109\end{funcdesc}
110
111\begin{funcdesc}{gethostbyaddr}{ip_address}
112Return a triple \code{(hostname, aliaslist, ipaddrlist)} where
113\code{hostname} is the primary host name responding to the given
114\var{ip_address}, \code{aliaslist} is a (possibly empty) list of
115alternative host names for the same address, and \code{ipaddrlist} is
116a list of IP addresses for the same interface on the same
117host (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
125\code{socket()} function. This is usually only needed for sockets
126opened in ``raw'' mode (\code{SOCK_RAW}); for the normal socket modes,
127the correct protocol is chosen automatically if the protocol is
128omitted or zero.
129\end{funcdesc}
130
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000131\begin{funcdesc}{getservbyname}{servicename\, protocolname}
132Translate 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
Guido van Rossum6bb1adc1995-03-13 10:03:32 +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
139protocol number. The address family should be \code{AF_INET} or
140\code{AF_UNIX}. The socket type should be \code{SOCK_STREAM},
141\code{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
142The protocol number is usually zero and may be omitted in that case.
143\end{funcdesc}
144
Guido van Rossum6bb1adc1995-03-13 10:03:32 +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
147returned by a file object's \code{fileno} method). Address family,
148socket 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.
183It is the same as \code{type(socket.socket(...))}.
184\end{datadesc}
185
Guido van Rossum470be141995-03-17 16:07:09 +0000186\subsection{Socket Objects}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000187
188\noindent
189Socket objects have the following methods. Except for
190\code{makefile()} these correspond to \UNIX{} system calls applicable to
191sockets.
192
Fred Drake19479911998-02-13 06:58:54 +0000193\setindexsubitem{(socket method)}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000194\begin{funcdesc}{accept}{}
195Accept a connection.
196The socket must be bound to an address and listening for connections.
197The return value is a pair \code{(\var{conn}, \var{address})}
198where \var{conn} is a \emph{new} socket object usable to send and
199receive data on the connection, and \var{address} is the address bound
200to the socket on the other end of the connection.
201\end{funcdesc}
202
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000203\begin{funcdesc}{bind}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000204Bind the socket to \var{address}. The socket must not already be bound.
Guido van Rossum86751151995-02-28 17:14:32 +0000205(The format of \var{address} depends on the address family --- see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000206\end{funcdesc}
207
208\begin{funcdesc}{close}{}
209Close the socket. All future operations on the socket object will fail.
210The remote end will receive no more data (after queued data is flushed).
211Sockets are automatically closed when they are garbage-collected.
212\end{funcdesc}
213
214\begin{funcdesc}{connect}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000215Connect to a remote socket at \var{address}.
Guido van Rossum86751151995-02-28 17:14:32 +0000216(The format of \var{address} depends on the address family --- see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000217\end{funcdesc}
218
Guido van Rossumf7790c61997-11-18 15:29:20 +0000219\begin{funcdesc}{connect_ex}{address}
Guido van Rossumeefcba61997-12-09 19:47:24 +0000220Like \code{connect(\var{address})}, but return an error indicator
Guido van Rossumf7790c61997-11-18 15:29:20 +0000221instead of raising an exception. The error indicator is 0 if the
222operation succeeded, otherwise the value of the \code{errno}
223variable. This is useful e.g. for asynchronous connects.
224\end{funcdesc}
225
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000226\begin{funcdesc}{fileno}{}
227Return the socket's file descriptor (a small integer). This is useful
228with \code{select}.
229\end{funcdesc}
230
231\begin{funcdesc}{getpeername}{}
232Return the remote address to which the socket is connected. This is
233useful to find out the port number of a remote IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000234(The format of the address returned depends on the address family ---
Guido van Rossum781db5d1994-08-05 13:37:36 +0000235see above.) On some systems this function is not supported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000236\end{funcdesc}
237
238\begin{funcdesc}{getsockname}{}
239Return the socket's own address. This is useful to find out the port
240number of an IP socket, for instance.
Guido van Rossum86751151995-02-28 17:14:32 +0000241(The format of the address returned depends on the address family ---
Guido van Rossuma84ec511994-06-23 12:13:52 +0000242see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000243\end{funcdesc}
244
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000245\begin{funcdesc}{getsockopt}{level\, optname\optional{\, buflen}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000246Return the value of the given socket option (see the \UNIX{} man page
Fred Drake9eb5e501998-01-20 04:54:23 +0000247\emph{getsockopt}(2)). The needed symbolic constants (\code{SO_*} etc.)
Guido van Rossum470be141995-03-17 16:07:09 +0000248are defined in this module. If \var{buflen}
249is absent, an integer option is assumed and its integer value
Guido van Rossum8df36371995-02-27 17:52:15 +0000250is returned by the function. If \var{buflen} is present, it specifies
251the maximum length of the buffer used to receive the option in, and
Guido van Rossum470be141995-03-17 16:07:09 +0000252this buffer is returned as a string. It is up to the caller to decode
Guido van Rossum8df36371995-02-27 17:52:15 +0000253the contents of the buffer (see the optional built-in module
254\code{struct} for a way to decode C structures encoded as strings).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000255\end{funcdesc}
256
257\begin{funcdesc}{listen}{backlog}
Guido van Rossum470be141995-03-17 16:07:09 +0000258Listen for connections made to the socket. The \var{backlog} argument
259specifies the maximum number of queued connections and should be at
260least 1; the maximum value is system-dependent (usually 5).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000261\end{funcdesc}
262
Guido van Rossum470be141995-03-17 16:07:09 +0000263\begin{funcdesc}{makefile}{\optional{mode\optional{\, bufsize}}}
264Return a \dfn{file object} associated with the socket. (File objects
265were described earlier under Built-in Types.) The file object
266references a \code{dup()}ped version of the socket file descriptor, so
267the file object and socket object may be closed or garbage-collected
268independently. The optional \var{mode} and \var{bufsize} arguments
269are interpreted the same way as by the built-in
270\code{open()} function.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000271\end{funcdesc}
272
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000273\begin{funcdesc}{recv}{bufsize\optional{\, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000274Receive data from the socket. The return value is a string representing
275the data received. The maximum amount of data to be received
276at once is specified by \var{bufsize}. See the \UNIX{} manual page
277for the meaning of the optional argument \var{flags}; it defaults to
278zero.
279\end{funcdesc}
280
Guido van Rossum470be141995-03-17 16:07:09 +0000281\begin{funcdesc}{recvfrom}{bufsize\optional{\, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000282Receive data from the socket. The return value is a pair
283\code{(\var{string}, \var{address})} where \var{string} is a string
284representing the data received and \var{address} is the address of the
Guido van Rossum470be141995-03-17 16:07:09 +0000285socket sending the data. The optional \var{flags} argument has the
286same meaning as for \code{recv()} above.
Guido van Rossum86751151995-02-28 17:14:32 +0000287(The format of \var{address} depends on the address family --- see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000288\end{funcdesc}
289
Guido van Rossum470be141995-03-17 16:07:09 +0000290\begin{funcdesc}{send}{string\optional{\, flags}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000291Send data to the socket. The socket must be connected to a remote
Guido van Rossum470be141995-03-17 16:07:09 +0000292socket. The optional \var{flags} argument has the same meaning as for
293\code{recv()} above. Return the number of bytes sent.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000294\end{funcdesc}
295
Guido van Rossum470be141995-03-17 16:07:09 +0000296\begin{funcdesc}{sendto}{string\optional{\, flags}\, address}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000297Send data to the socket. The socket should not be connected to a
298remote socket, since the destination socket is specified by
Guido van Rossum470be141995-03-17 16:07:09 +0000299\code{address}. The optional \var{flags} argument has the same
300meaning as for \code{recv()} above. Return the number of bytes sent.
Guido van Rossum86751151995-02-28 17:14:32 +0000301(The format of \var{address} depends on the address family --- see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000302\end{funcdesc}
303
Guido van Rossum91951481994-09-07 14:39:14 +0000304\begin{funcdesc}{setblocking}{flag}
305Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
306the socket is set to non-blocking, else to blocking mode. Initially
307all sockets are in blocking mode. In non-blocking mode, if a
308\code{recv} call doesn't find any data, or if a \code{send} call can't
309immediately dispose of the data, a \code{socket.error} exception is
310raised; in blocking mode, the calls block until they can proceed.
311\end{funcdesc}
312
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000313\begin{funcdesc}{setsockopt}{level\, optname\, value}
314Set the value of the given socket option (see the \UNIX{} man page
Fred Drake9eb5e501998-01-20 04:54:23 +0000315\emph{setsockopt}(2)). The needed symbolic constants are defined in
Guido van Rossum8df36371995-02-27 17:52:15 +0000316the \code{socket} module (\code{SO_*} etc.). The value can be an
317integer or a string representing a buffer. In the latter case it is
318up to the caller to ensure that the string contains the proper bits
319(see the optional built-in module
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000320\code{struct} for a way to encode C structures as strings).
321\end{funcdesc}
322
323\begin{funcdesc}{shutdown}{how}
324Shut down one or both halves of the connection. If \var{how} is \code{0},
325further receives are disallowed. If \var{how} is \code{1}, further sends are
326disallowed. If \var{how} is \code{2}, further sends and receives are
327disallowed.
328\end{funcdesc}
329
330Note that there are no methods \code{read()} or \code{write()}; use
331\code{recv()} and \code{send()} without \var{flags} argument instead.
332
333\subsection{Example}
334\nodename{Socket Example}
335
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000336Here are two minimal example programs using the TCP/IP protocol:\ a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000337server that echoes all data that it receives back (servicing only one
338client), and a client using it. Note that a server must perform the
339sequence \code{socket}, \code{bind}, \code{listen}, \code{accept}
340(possibly repeating the \code{accept} to service more than one client),
341while a client only needs the sequence \code{socket}, \code{connect}.
342Also note that the server does not \code{send}/\code{receive} on the
343socket it is listening on but on the new socket returned by
344\code{accept}.
345
Fred Drake19479911998-02-13 06:58:54 +0000346\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000347# Echo server program
348from socket import *
349HOST = '' # Symbolic name meaning the local host
350PORT = 50007 # Arbitrary non-privileged server
351s = socket(AF_INET, SOCK_STREAM)
352s.bind(HOST, PORT)
Guido van Rossum5da57551994-03-02 10:52:16 +0000353s.listen(1)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000354conn, addr = s.accept()
355print 'Connected by', addr
356while 1:
357 data = conn.recv(1024)
358 if not data: break
359 conn.send(data)
360conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000361\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000362%
Fred Drake19479911998-02-13 06:58:54 +0000363\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000364# Echo client program
365from socket import *
366HOST = 'daring.cwi.nl' # The remote host
367PORT = 50007 # The same port as used by the server
368s = socket(AF_INET, SOCK_STREAM)
369s.connect(HOST, PORT)
370s.send('Hello, world')
371data = s.recv(1024)
372s.close()
373print 'Received', `data`
Fred Drake19479911998-02-13 06:58:54 +0000374\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000375%
376\begin{seealso}
377\seemodule{SocketServer}{classes that simplify writing network servers}
378\end{seealso}