blob: a5d5a17ec468ac63ade3e1f5e2f885f66e3e8fcb [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
13system calls also a valuable source of information on the details of
14socket 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
20calls. Parameter types are somewhat higer-level than in the C
21interface: 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
36All errors raise exceptions. The normal exceptions for invalid
37argument types and out-of-memory conditions can be raised; errors
38related to socket or address semantics raise the error \code{socket.error}.
39
40Non-blocking and asynchronous mode are not supported; see module
41\code{select} for a way to do non-blocking socket I/O.
42
43The module \code{socket} exports the following constants and functions:
44
45\renewcommand{\indexsubitem}{(in module socket)}
46\begin{excdesc}{error}
47This exception is raised for socket- or address-related errors.
48The accompanying value is either a string telling what went wrong or a
49pair \code{(\var{errno}, \var{string})}
50representing an error returned by a system
51call, similar to the value accompanying \code{posix.error}.
52\end{excdesc}
53
54\begin{datadesc}{AF_UNIX}
55\dataline{AF_INET}
56These constants represent the address (and protocol) families,
57used for the first argument to \code{socket()}.
58\end{datadesc}
59
60\begin{datadesc}{SOCK_STREAM}
61\dataline{SOCK_DGRAM}
62These constants represent the socket types,
63used for the second argument to \code{socket()}.
64(There are other types, but only \code{SOCK_STREAM} and
65\code{SOCK_DGRAM} appear to be generally useful.)
66\end{datadesc}
67
68\begin{funcdesc}{gethostbyname}{hostname}
69Translate a host name to IP address format. The IP address is
70returned as a string, e.g., \code{'100.50.200.5'}. If the host name
71is an IP address itself it is returned unchanged.
72\end{funcdesc}
73
74\begin{funcdesc}{getservbyname}{servicename\, protocolname}
75Translate an Internet service name and protocol name to a port number
76for that service. The protocol name should be \code{'tcp'} or
77\code{'udp'}.
78\end{funcdesc}
79
80\begin{funcdesc}{socket}{family\, type\, proto}
81Create a new socket using the given address family, socket type and
82protocol number. The address family should be \code{AF_INET} or
83\code{AF_UNIX}. The socket type should be \code{SOCK_STREAM},
84\code{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
85The protocol number is usually zero and may be omitted in that case.
86\end{funcdesc}
87
88\begin{funcdesc}{fromfd}{fd\, family\, type\, proto}
89Build a socket object from an existing file descriptor (an integer as
90returned by a file object's \code{fileno} method). Address family,
91socket type and protocol number are as for the \code{socket} function
92above. The file descriptor should refer to a socket, but this is not
93checked --- subsequent operations on the object may fail if the file
94descriptor is invalid. This function is rarely needed, but can be
95used to get or set socket options on a socket passed to a program as
96standard input or output (e.g. a server started by the \UNIX{} inet
97daemon).
98\end{funcdesc}
99
100\subsection{Socket Object Methods}
101
102\noindent
103Socket objects have the following methods. Except for
104\code{makefile()} these correspond to \UNIX{} system calls applicable to
105sockets.
106
107\renewcommand{\indexsubitem}{(socket method)}
108\begin{funcdesc}{accept}{}
109Accept a connection.
110The socket must be bound to an address and listening for connections.
111The return value is a pair \code{(\var{conn}, \var{address})}
112where \var{conn} is a \emph{new} socket object usable to send and
113receive data on the connection, and \var{address} is the address bound
114to the socket on the other end of the connection.
115\end{funcdesc}
116
117\begin{funcdesc}{avail}{}
118Return true (nonzero) if at least one byte of data can be received
119from the socket without blocking, false (zero) if not. There is no
120indication of how many bytes are available. (\strong{This function is
121obsolete --- see module \code{select} for a more general solution.})
122\end{funcdesc}
123
124\begin{funcdesc}{bind}{address}
125Bind the socket to an address. The socket must not already be bound.
126\end{funcdesc}
127
128\begin{funcdesc}{close}{}
129Close the socket. All future operations on the socket object will fail.
130The remote end will receive no more data (after queued data is flushed).
131Sockets are automatically closed when they are garbage-collected.
132\end{funcdesc}
133
134\begin{funcdesc}{connect}{address}
135Connect to a remote socket.
136\end{funcdesc}
137
138\begin{funcdesc}{fileno}{}
139Return the socket's file descriptor (a small integer). This is useful
140with \code{select}.
141\end{funcdesc}
142
143\begin{funcdesc}{getpeername}{}
144Return the remote address to which the socket is connected. This is
145useful to find out the port number of a remote IP socket, for instance.
146\end{funcdesc}
147
148\begin{funcdesc}{getsockname}{}
149Return the socket's own address. This is useful to find out the port
150number of an IP socket, for instance.
151\end{funcdesc}
152
153\begin{funcdesc}{getsockopt}{level\, optname\, buflen}
154Return the value of the given socket option (see the \UNIX{} man page
155{\it getsockopt}(2)). The needed symbolic constants are defined in module
156SOCKET. If the optional third argument is absent, an integer option
157is assumed and its integer value is returned by the function. If
158\var{buflen} is present, it specifies the maximum length of the buffer used
159to receive the option in, and this buffer is returned as a string.
160It's up to the caller to decode the contents of the buffer (see the
161optional built-in module \code{struct} for a way to decode C structures
162encoded as strings).
163\end{funcdesc}
164
165\begin{funcdesc}{listen}{backlog}
166Listen for connections made to the socket.
167The argument (in the range 0-5) specifies the maximum number of
168queued connections.
169\end{funcdesc}
170
171\begin{funcdesc}{makefile}{mode}
172Return a \dfn{file object} associated with the socket.
173(File objects were described earlier under Built-in Types.)
174The file object references a \code{dup}ped version of the socket file
175descriptor, so the file object and socket object may be closed or
176garbage-collected independently.
177\end{funcdesc}
178
179\begin{funcdesc}{recv}{bufsize\, flags}
180Receive data from the socket. The return value is a string representing
181the data received. The maximum amount of data to be received
182at once is specified by \var{bufsize}. See the \UNIX{} manual page
183for the meaning of the optional argument \var{flags}; it defaults to
184zero.
185\end{funcdesc}
186
187\begin{funcdesc}{recvfrom}{bufsize}
188Receive data from the socket. The return value is a pair
189\code{(\var{string}, \var{address})} where \var{string} is a string
190representing the data received and \var{address} is the address of the
191socket sending the data.
192\end{funcdesc}
193
194\begin{funcdesc}{send}{string}
195Send data to the socket. The socket must be connected to a remote
196socket.
197\end{funcdesc}
198
199\begin{funcdesc}{sendto}{string\, address}
200Send data to the socket. The socket should not be connected to a
201remote socket, since the destination socket is specified by
202\code{address}.
203\end{funcdesc}
204
205\begin{funcdesc}{setsockopt}{level\, optname\, value}
206Set the value of the given socket option (see the \UNIX{} man page
207{\it setsockopt}(2)). The needed symbolic constants are defined in module
208\code{SOCKET}. The value can be an integer or a string representing a
209buffer. In the latter case it is up to the caller to ensure that the
210string contains the proper bits (see the optional built-in module
211\code{struct} for a way to encode C structures as strings).
212\end{funcdesc}
213
214\begin{funcdesc}{shutdown}{how}
215Shut down one or both halves of the connection. If \var{how} is \code{0},
216further receives are disallowed. If \var{how} is \code{1}, further sends are
217disallowed. If \var{how} is \code{2}, further sends and receives are
218disallowed.
219\end{funcdesc}
220
221Note that there are no methods \code{read()} or \code{write()}; use
222\code{recv()} and \code{send()} without \var{flags} argument instead.
223
224\subsection{Example}
225\nodename{Socket Example}
226
227Here are two minimal example programs using the TCP/IP protocol: a
228server that echoes all data that it receives back (servicing only one
229client), and a client using it. Note that a server must perform the
230sequence \code{socket}, \code{bind}, \code{listen}, \code{accept}
231(possibly repeating the \code{accept} to service more than one client),
232while a client only needs the sequence \code{socket}, \code{connect}.
233Also note that the server does not \code{send}/\code{receive} on the
234socket it is listening on but on the new socket returned by
235\code{accept}.
236
237\bcode\begin{verbatim}
238# Echo server program
239from socket import *
240HOST = '' # Symbolic name meaning the local host
241PORT = 50007 # Arbitrary non-privileged server
242s = socket(AF_INET, SOCK_STREAM)
243s.bind(HOST, PORT)
244s.listen(0)
245conn, addr = s.accept()
246print 'Connected by', addr
247while 1:
248 data = conn.recv(1024)
249 if not data: break
250 conn.send(data)
251conn.close()
252\end{verbatim}\ecode
253
254\bcode\begin{verbatim}
255# Echo client program
256from socket import *
257HOST = 'daring.cwi.nl' # The remote host
258PORT = 50007 # The same port as used by the server
259s = socket(AF_INET, SOCK_STREAM)
260s.connect(HOST, PORT)
261s.send('Hello, world')
262data = s.recv(1024)
263s.close()
264print 'Received', `data`
265\end{verbatim}\ecode