Clarify portability and main program.
[python/dscho.git] / Doc / lib / libsocket.tex
blobc6476b9b962ff6f69f4057a6e23a046d9bc6f13f
1 \section{\module{socket} ---
2 Low-level networking interface.}
3 \declaremodule{builtin}{socket}
5 \modulesynopsis{Low-level networking interface.}
8 This module provides access to the BSD \emph{socket} interface.
9 It is available on \UNIX{} systems that support this interface.
11 For an introduction to socket programming (in C), see the following
12 papers: \emph{An Introductory 4.3BSD Interprocess Communication
13 Tutorial}, by Stuart Sechrest and \emph{An Advanced 4.3BSD Interprocess
14 Communication Tutorial}, by Samuel J. Leffler et al, both in the
15 \UNIX{} Programmer's Manual, Supplementary Documents 1 (sections PS1:7
16 and PS1:8). The \UNIX{} manual pages for the various socket-related
17 system calls are also a valuable source of information on the details of
18 socket semantics.
20 The Python interface is a straightforward transliteration of the
21 \UNIX{} system call and library interface for sockets to Python's
22 object-oriented style: the \function{socket()} function returns a
23 \dfn{socket object} whose methods implement the various socket system
24 calls. Parameter types are somewhat higher-level than in the C
25 interface: as with \method{read()} and \method{write()} operations on
26 Python files, buffer allocation on receive operations is automatic,
27 and buffer length is implicit on send operations.
29 Socket addresses are represented as a single string for the
30 \constant{AF_UNIX} address family and as a pair
31 \code{(\var{host}, \var{port})} for the \constant{AF_INET} address
32 family, where \var{host} is a string representing
33 either a hostname in Internet domain notation like
34 \code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'},
35 and \var{port} is an integral port number. Other address families are
36 currently not supported. The address format required by a particular
37 socket object is automatically selected based on the address family
38 specified when the socket object was created.
40 For IP addresses, two special forms are accepted instead of a host
41 address: the empty string represents \constant{INADDR_ANY}, and the string
42 \code{"<broadcast>"} represents \constant{INADDR_BROADCAST}.
44 All errors raise exceptions. The normal exceptions for invalid
45 argument types and out-of-memory conditions can be raised; errors
46 related to socket or address semantics raise the error \code{socket.error}.
48 Non-blocking mode is supported through the \code{setblocking()}
49 method.
51 The module \module{socket} exports the following constants and functions:
54 \begin{excdesc}{error}
55 This exception is raised for socket- or address-related errors.
56 The accompanying value is either a string telling what went wrong or a
57 pair \code{(\var{errno}, \var{string})}
58 representing an error returned by a system
59 call, similar to the value accompanying \code{os.error}.
60 See the module \module{errno}\refbimodindex{errno}, which contains
61 names for the error codes defined by the underlying operating system.
62 \end{excdesc}
64 \begin{datadesc}{AF_UNIX}
65 \dataline{AF_INET}
66 These constants represent the address (and protocol) families,
67 used for the first argument to \function{socket()}. If the
68 \constant{AF_UNIX} constant is not defined then this protocol is
69 unsupported.
70 \end{datadesc}
72 \begin{datadesc}{SOCK_STREAM}
73 \dataline{SOCK_DGRAM}
74 \dataline{SOCK_RAW}
75 \dataline{SOCK_RDM}
76 \dataline{SOCK_SEQPACKET}
77 These constants represent the socket types,
78 used for the second argument to \function{socket()}.
79 (Only \constant{SOCK_STREAM} and
80 \constant{SOCK_DGRAM} appear to be generally useful.)
81 \end{datadesc}
83 \begin{datadesc}{SO_*}
84 \dataline{SOMAXCONN}
85 \dataline{MSG_*}
86 \dataline{SOL_*}
87 \dataline{IPPROTO_*}
88 \dataline{IPPORT_*}
89 \dataline{INADDR_*}
90 \dataline{IP_*}
91 Many constants of these forms, documented in the \UNIX{} documentation on
92 sockets and/or the IP protocol, are also defined in the socket module.
93 They are generally used in arguments to the \method{setsockopt()} and
94 \method{getsockopt()} methods of socket objects. In most cases, only
95 those symbols that are defined in the \UNIX{} header files are defined;
96 for a few symbols, default values are provided.
97 \end{datadesc}
99 \begin{funcdesc}{gethostbyname}{hostname}
100 Translate a host name to IP address format. The IP address is
101 returned as a string, e.g., \code{'100.50.200.5'}. If the host name
102 is an IP address itself it is returned unchanged. See
103 \code{gethostbyname_ex} for a more complete interface.
104 \end{funcdesc}
106 \begin{funcdesc}{gethostbyname_ex}{hostname}
107 Translate a host name to IP address format, extended interface.
108 Return a triple \code{(hostname, aliaslist, ipaddrlist)} where
109 \code{hostname} is the primary host name responding to the given
110 \var{ip_address}, \code{aliaslist} is a (possibly empty) list of
111 alternative host names for the same address, and \code{ipaddrlist} is
112 a list of IP addresses for the same interface on the same
113 host (often but not always a single address).
114 \end{funcdesc}
116 \begin{funcdesc}{gethostname}{}
117 Return a string containing the hostname of the machine where
118 the Python interpreter is currently executing. If you want to know the
119 current machine's IP address, use \code{gethostbyname(gethostname())}.
120 Note: \function{gethostname()} doesn't always return the fully qualified
121 domain name; use \code{gethostbyaddr(gethostname())}
122 (see below).
123 \end{funcdesc}
125 \begin{funcdesc}{gethostbyaddr}{ip_address}
126 Return a triple \code{(\var{hostname}, \var{aliaslist},
127 \var{ipaddrlist})} where \var{hostname} is the primary host name
128 responding to the given \var{ip_address}, \var{aliaslist} is a
129 (possibly empty) list of alternative host names for the same address,
130 and \var{ipaddrlist} is a list of IP addresses for the same interface
131 on the same host (most likely containing only a single address).
132 To find the fully qualified domain name, check \var{hostname} and the
133 items of \var{aliaslist} for an entry containing at least one period.
134 \end{funcdesc}
136 \begin{funcdesc}{getprotobyname}{protocolname}
137 Translate an Internet protocol name (e.g. \code{'icmp'}) to a constant
138 suitable for passing as the (optional) third argument to the
139 \function{socket()} function. This is usually only needed for sockets
140 opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket
141 modes, the correct protocol is chosen automatically if the protocol is
142 omitted or zero.
143 \end{funcdesc}
145 \begin{funcdesc}{getservbyname}{servicename, protocolname}
146 Translate an Internet service name and protocol name to a port number
147 for that service. The protocol name should be \code{'tcp'} or
148 \code{'udp'}.
149 \end{funcdesc}
151 \begin{funcdesc}{socket}{family, type\optional{, proto}}
152 Create a new socket using the given address family, socket type and
153 protocol number. The address family should be \constant{AF_INET} or
154 \constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM},
155 \constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
156 The protocol number is usually zero and may be omitted in that case.
157 \end{funcdesc}
159 \begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}}
160 Build a socket object from an existing file descriptor (an integer as
161 returned by a file object's \method{fileno()} method). Address family,
162 socket type and protocol number are as for the \code{socket} function
163 above. The file descriptor should refer to a socket, but this is not
164 checked --- subsequent operations on the object may fail if the file
165 descriptor is invalid. This function is rarely needed, but can be
166 used to get or set socket options on a socket passed to a program as
167 standard input or output (e.g.\ a server started by the \UNIX{} inet
168 daemon).
169 \end{funcdesc}
171 \begin{funcdesc}{ntohl}{x}
172 Convert 32-bit integers from network to host byte order. On machines
173 where the host byte order is the same as network byte order, this is a
174 no-op; otherwise, it performs a 4-byte swap operation.
175 \end{funcdesc}
177 \begin{funcdesc}{ntohs}{x}
178 Convert 16-bit integers from network to host byte order. On machines
179 where the host byte order is the same as network byte order, this is a
180 no-op; otherwise, it performs a 2-byte swap operation.
181 \end{funcdesc}
183 \begin{funcdesc}{htonl}{x}
184 Convert 32-bit integers from host to network byte order. On machines
185 where the host byte order is the same as network byte order, this is a
186 no-op; otherwise, it performs a 4-byte swap operation.
187 \end{funcdesc}
189 \begin{funcdesc}{htons}{x}
190 Convert 16-bit integers from host to network byte order. On machines
191 where the host byte order is the same as network byte order, this is a
192 no-op; otherwise, it performs a 2-byte swap operation.
193 \end{funcdesc}
195 \begin{datadesc}{SocketType}
196 This is a Python type object that represents the socket object type.
197 It is the same as \code{type(socket(...))}.
198 \end{datadesc}
200 \subsection{Socket Objects}
202 Socket objects have the following methods. Except for
203 \method{makefile()} these correspond to \UNIX{} system calls
204 applicable to sockets.
206 \begin{methoddesc}[socket]{accept}{}
207 Accept a connection.
208 The socket must be bound to an address and listening for connections.
209 The return value is a pair \code{(\var{conn}, \var{address})}
210 where \var{conn} is a \emph{new} socket object usable to send and
211 receive data on the connection, and \var{address} is the address bound
212 to the socket on the other end of the connection.
213 \end{methoddesc}
215 \begin{methoddesc}[socket]{bind}{address}
216 Bind the socket to \var{address}. The socket must not already be bound.
217 (The format of \var{address} depends on the address family --- see above.)
218 \end{methoddesc}
220 \begin{methoddesc}[socket]{close}{}
221 Close the socket. All future operations on the socket object will fail.
222 The remote end will receive no more data (after queued data is flushed).
223 Sockets are automatically closed when they are garbage-collected.
224 \end{methoddesc}
226 \begin{methoddesc}[socket]{connect}{address}
227 Connect to a remote socket at \var{address}.
228 (The format of \var{address} depends on the address family --- see
229 above.)
230 \end{methoddesc}
232 \begin{methoddesc}[socket]{connect_ex}{address}
233 Like \code{connect(\var{address})}, but return an error indicator
234 instead of raising an exception. The error indicator is 0 if the
235 operation succeeded, otherwise the value of the \cdata{errno}
236 variable. This is useful, e.g., for asynchronous connects.
237 \end{methoddesc}
239 \begin{methoddesc}[socket]{fileno}{}
240 Return the socket's file descriptor (a small integer). This is useful
241 with \function{select.select()}.
242 \end{methoddesc}
244 \begin{methoddesc}[socket]{getpeername}{}
245 Return the remote address to which the socket is connected. This is
246 useful to find out the port number of a remote IP socket, for instance.
247 (The format of the address returned depends on the address family ---
248 see above.) On some systems this function is not supported.
249 \end{methoddesc}
251 \begin{methoddesc}[socket]{getsockname}{}
252 Return the socket's own address. This is useful to find out the port
253 number of an IP socket, for instance.
254 (The format of the address returned depends on the address family ---
255 see above.)
256 \end{methoddesc}
258 \begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}}
259 Return the value of the given socket option (see the \UNIX{} man page
260 \manpage{getsockopt}{2}). The needed symbolic constants
261 (\constant{SO_*} etc.) are defined in this module. If \var{buflen}
262 is absent, an integer option is assumed and its integer value
263 is returned by the function. If \var{buflen} is present, it specifies
264 the maximum length of the buffer used to receive the option in, and
265 this buffer is returned as a string. It is up to the caller to decode
266 the contents of the buffer (see the optional built-in module
267 \module{struct} for a way to decode C structures encoded as strings).
268 \end{methoddesc}
270 \begin{methoddesc}[socket]{listen}{backlog}
271 Listen for connections made to the socket. The \var{backlog} argument
272 specifies the maximum number of queued connections and should be at
273 least 1; the maximum value is system-dependent (usually 5).
274 \end{methoddesc}
276 \begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}}
277 Return a \dfn{file object} associated with the socket. (File objects
278 were described earlier in \ref{bltin-file-objects}, ``File Objects.'')
279 The file object references a \cfunction{dup()}ped version of the
280 socket file descriptor, so the file object and socket object may be
281 closed or garbage-collected independently. The optional \var{mode}
282 and \var{bufsize} arguments are interpreted the same way as by the
283 built-in \function{open()} function.
284 \end{methoddesc}
286 \begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}}
287 Receive data from the socket. The return value is a string representing
288 the data received. The maximum amount of data to be received
289 at once is specified by \var{bufsize}. See the \UNIX{} manual page
290 \manpage{recv}{2} for the meaning of the optional argument
291 \var{flags}; it defaults to zero.
292 \end{methoddesc}
294 \begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}}
295 Receive data from the socket. The return value is a pair
296 \code{(\var{string}, \var{address})} where \var{string} is a string
297 representing the data received and \var{address} is the address of the
298 socket sending the data. The optional \var{flags} argument has the
299 same meaning as for \method{recv()} above.
300 (The format of \var{address} depends on the address family --- see above.)
301 \end{methoddesc}
303 \begin{methoddesc}[socket]{send}{string\optional{, flags}}
304 Send data to the socket. The socket must be connected to a remote
305 socket. The optional \var{flags} argument has the same meaning as for
306 \method{recv()} above. Returns the number of bytes sent.
307 \end{methoddesc}
309 \begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address}
310 Send data to the socket. The socket should not be connected to a
311 remote socket, since the destination socket is specified by
312 \var{address}. The optional \var{flags} argument has the same
313 meaning as for \method{recv()} above. Return the number of bytes sent.
314 (The format of \var{address} depends on the address family --- see above.)
315 \end{methoddesc}
317 \begin{methoddesc}[socket]{setblocking}{flag}
318 Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
319 the socket is set to non-blocking, else to blocking mode. Initially
320 all sockets are in blocking mode. In non-blocking mode, if a
321 \method{recv()} call doesn't find any data, or if a \code{send} call can't
322 immediately dispose of the data, a \exception{error} exception is
323 raised; in blocking mode, the calls block until they can proceed.
324 \end{methoddesc}
326 \begin{methoddesc}[socket]{setsockopt}{level, optname, value}
327 Set the value of the given socket option (see the \UNIX{} man page
328 \manpage{setsockopt}{2}). The needed symbolic constants are defined in
329 the \module{socket} module (\code{SO_*} etc.). The value can be an
330 integer or a string representing a buffer. In the latter case it is
331 up to the caller to ensure that the string contains the proper bits
332 (see the optional built-in module
333 \module{struct}\refbimodindex{struct} for a way to encode C structures
334 as strings).
335 \end{methoddesc}
337 \begin{methoddesc}[socket]{shutdown}{how}
338 Shut down one or both halves of the connection. If \var{how} is
339 \code{0}, further receives are disallowed. If \var{how} is \code{1},
340 further sends are disallowed. If \var{how} is \code{2}, further sends
341 and receives are disallowed.
342 \end{methoddesc}
344 Note that there are no methods \method{read()} or \method{write()};
345 use \method{recv()} and \method{send()} without \var{flags} argument
346 instead.
348 \subsection{Example}
349 \nodename{Socket Example}
351 Here are two minimal example programs using the TCP/IP protocol:\ a
352 server that echoes all data that it receives back (servicing only one
353 client), and a client using it. Note that a server must perform the
354 sequence \function{socket()}, \method{bind()}, \method{listen()},
355 \method{accept()} (possibly repeating the \method{accept()} to service
356 more than one client), while a client only needs the sequence
357 \function{socket()}, \method{connect()}. Also note that the server
358 does not \method{send()}/\method{recv()} on the
359 socket it is listening on but on the new socket returned by
360 \method{accept()}.
362 \begin{verbatim}
363 # Echo server program
364 from socket import *
365 HOST = '' # Symbolic name meaning the local host
366 PORT = 50007 # Arbitrary non-privileged server
367 s = socket(AF_INET, SOCK_STREAM)
368 s.bind(HOST, PORT)
369 s.listen(1)
370 conn, addr = s.accept()
371 print 'Connected by', addr
372 while 1:
373 data = conn.recv(1024)
374 if not data: break
375 conn.send(data)
376 conn.close()
377 \end{verbatim}
379 \begin{verbatim}
380 # Echo client program
381 from socket import *
382 HOST = 'daring.cwi.nl' # The remote host
383 PORT = 50007 # The same port as used by the server
384 s = socket(AF_INET, SOCK_STREAM)
385 s.connect(HOST, PORT)
386 s.send('Hello, world')
387 data = s.recv(1024)
388 s.close()
389 print 'Received', `data`
390 \end{verbatim}
392 \begin{seealso}
393 \seemodule{SocketServer}{classes that simplify writing network servers}
394 \end{seealso}