blob: 913bb8fe03b56afa5d3e7eb09cb7f857a0e1e5a5 [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\bimodindex{socket}
Fred Draked883ca11998-03-10 05:20:33 +00004
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
Fred Draked883ca11998-03-10 05:20:33 +000019object-oriented style: the \function{socket()} function returns a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020\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
Fred Draked883ca11998-03-10 05:20:33 +000022interface: as with \method{read()} and \method{write()} operations on
23Python files, buffer allocation on receive operations is automatic,
24and buffer length is implicit on send operations.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000025
26Socket addresses are represented as a single string for the
Fred Draked883ca11998-03-10 05:20:33 +000027\constant{AF_UNIX} address family and as a pair
28\code{(\var{host}, \var{port})} for the \constant{AF_INET} address
29family, where \var{host} is a string representing
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030either 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
Fred Draked883ca11998-03-10 05:20:33 +000038address: the empty string represents \constant{INADDR_ANY}, and the string
39\code{"<broadcast>"} represents \constant{INADDR_BROADCAST}.
Guido van Rossume4f347e1997-05-09 02:21:51 +000040
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
Fred Draked883ca11998-03-10 05:20:33 +000048The module \module{socket} exports the following constants and functions:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049
Fred Draked883ca11998-03-10 05:20:33 +000050
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,
Fred Draked883ca11998-03-10 05:20:33 +000064used for the first argument to \function{socket()}. If the
65\constant{AF_UNIX} constant is not defined then this protocol is
66unsupported.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067\end{datadesc}
68
69\begin{datadesc}{SOCK_STREAM}
70\dataline{SOCK_DGRAM}
Guido van Rossum781db5d1994-08-05 13:37:36 +000071\dataline{SOCK_RAW}
72\dataline{SOCK_RDM}
73\dataline{SOCK_SEQPACKET}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000074These constants represent the socket types,
Fred Draked883ca11998-03-10 05:20:33 +000075used for the second argument to \function{socket()}.
76(Only \constant{SOCK_STREAM} and
77\constant{SOCK_DGRAM} appear to be generally useful.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000078\end{datadesc}
79
Guido van Rossumed2bad81995-02-16 16:29:18 +000080\begin{datadesc}{SO_*}
81\dataline{SOMAXCONN}
82\dataline{MSG_*}
83\dataline{SOL_*}
84\dataline{IPPROTO_*}
85\dataline{IPPORT_*}
86\dataline{INADDR_*}
87\dataline{IP_*}
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000088Many constants of these forms, documented in the \UNIX{} documentation on
Guido van Rossumed2bad81995-02-16 16:29:18 +000089sockets and/or the IP protocol, are also defined in the socket module.
Fred Draked883ca11998-03-10 05:20:33 +000090They are generally used in arguments to the \method{setsockopt()} and
91\method{getsockopt()} methods of socket objects. In most cases, only
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000092those symbols that are defined in the \UNIX{} header files are defined;
Guido van Rossumed2bad81995-02-16 16:29:18 +000093for a few symbols, default values are provided.
94\end{datadesc}
95
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000096\begin{funcdesc}{gethostbyname}{hostname}
97Translate a host name to IP address format. The IP address is
98returned as a string, e.g., \code{'100.50.200.5'}. If the host name
99is an IP address itself it is returned unchanged.
100\end{funcdesc}
101
Guido van Rossum781db5d1994-08-05 13:37:36 +0000102\begin{funcdesc}{gethostname}{}
Guido van Rossum16d6e711994-08-08 12:30:22 +0000103Return a string containing the hostname of the machine where
104the Python interpreter is currently executing. If you want to know the
Fred Draked883ca11998-03-10 05:20:33 +0000105current machine's IP address, use \code{gethostbyname(gethostname())}.
106Note: \function{gethostname()} doesn't always return the fully qualified
107domain name; use \code{gethostbyaddr(gethostname())}
Guido van Rossumfe27a501997-01-11 17:04:56 +0000108(see below).
Guido van Rossum31cce971995-01-04 19:17:34 +0000109\end{funcdesc}
110
111\begin{funcdesc}{gethostbyaddr}{ip_address}
Fred Draked883ca11998-03-10 05:20:33 +0000112Return a triple \code{(\var{hostname}, \var{aliaslist},
113\var{ipaddrlist})} where \var{hostname} is the primary host name
114responding to the given \var{ip_address}, \var{aliaslist} is a
115(possibly empty) list of alternative host names for the same address,
116and \var{ipaddrlist} is a list of IP addresses for the same interface
117on the same host (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
Fred Draked883ca11998-03-10 05:20:33 +0000125\function{socket()} function. This is usually only needed for sockets
126opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket
127modes, the correct protocol is chosen automatically if the protocol is
Guido van Rossum62ac99e1996-12-19 16:43:25 +0000128omitted or zero.
129\end{funcdesc}
130
Fred Draked883ca11998-03-10 05:20:33 +0000131\begin{funcdesc}{getservbyname}{servicename, protocolname}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132Translate 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
Fred Draked883ca11998-03-10 05:20:33 +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
Fred Draked883ca11998-03-10 05:20:33 +0000139protocol number. The address family should be \constant{AF_INET} or
140\constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM},
141\constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000142The protocol number is usually zero and may be omitted in that case.
143\end{funcdesc}
144
Fred Draked883ca11998-03-10 05:20:33 +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
Fred Draked883ca11998-03-10 05:20:33 +0000147returned by a file object's \method{fileno()} method). Address family,
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000148socket 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.
Fred Draked883ca11998-03-10 05:20:33 +0000183It is the same as \code{type(socket(...))}.
Guido van Rossum2335c5e1997-05-21 14:41:42 +0000184\end{datadesc}
185
Guido van Rossum470be141995-03-17 16:07:09 +0000186\subsection{Socket Objects}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000187
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000188Socket objects have the following methods. Except for
Fred Draked883ca11998-03-10 05:20:33 +0000189\method{makefile()} these correspond to \UNIX{} system calls
190applicable to sockets.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000191
Fred Drake19479911998-02-13 06:58:54 +0000192\setindexsubitem{(socket method)}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000193\begin{funcdesc}{accept}{}
194Accept a connection.
195The socket must be bound to an address and listening for connections.
196The return value is a pair \code{(\var{conn}, \var{address})}
197where \var{conn} is a \emph{new} socket object usable to send and
198receive data on the connection, and \var{address} is the address bound
199to the socket on the other end of the connection.
200\end{funcdesc}
201
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000202\begin{funcdesc}{bind}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000203Bind the socket to \var{address}. The socket must not already be bound.
Guido van Rossum86751151995-02-28 17:14:32 +0000204(The format of \var{address} depends on the address family --- see above.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000205\end{funcdesc}
206
207\begin{funcdesc}{close}{}
208Close the socket. All future operations on the socket object will fail.
209The remote end will receive no more data (after queued data is flushed).
210Sockets are automatically closed when they are garbage-collected.
211\end{funcdesc}
212
213\begin{funcdesc}{connect}{address}
Guido van Rossuma84ec511994-06-23 12:13:52 +0000214Connect to a remote socket at \var{address}.
Fred Draked883ca11998-03-10 05:20:33 +0000215(The format of \var{address} depends on the address family --- see
216above.)
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
Fred Draked883ca11998-03-10 05:20:33 +0000222operation succeeded, otherwise the value of the \cdata{errno}
Guido van Rossumf7790c61997-11-18 15:29:20 +0000223variable. 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
Fred Draked883ca11998-03-10 05:20:33 +0000228with \function{select.select()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000229\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
Fred Draked883ca11998-03-10 05:20:33 +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 Draked883ca11998-03-10 05:20:33 +0000247\manpage{getsockopt}{2}). The needed symbolic constants
248(\constant{SO_*} etc.) are defined in this module. If \var{buflen}
Guido van Rossum470be141995-03-17 16:07:09 +0000249is 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
Fred Draked883ca11998-03-10 05:20:33 +0000254\module{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
Fred Draked883ca11998-03-10 05:20:33 +0000265were described earlier in \ref{bltin-file-objects}, ``File Objects.'')
266The file object references a \cfunction{dup()}ped version of the
267socket file descriptor, so the file object and socket object may be
268closed or garbage-collected independently. The optional \var{mode}
269and \var{bufsize} arguments are interpreted the same way as by the
270built-in \function{open()} function.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000271\end{funcdesc}
272
Fred Draked883ca11998-03-10 05:20:33 +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
Fred Draked883ca11998-03-10 05:20:33 +0000277\manpage{recv}{2} for the meaning of the optional argument
278\var{flags}; it defaults to zero.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000279\end{funcdesc}
280
Fred Draked883ca11998-03-10 05:20:33 +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
Fred Draked883ca11998-03-10 05:20:33 +0000286same meaning as for \method{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
Fred Draked883ca11998-03-10 05:20:33 +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
Fred Draked883ca11998-03-10 05:20:33 +0000293\method{recv()} above. Returns 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
Fred Draked883ca11998-03-10 05:20:33 +0000299\var{address}. The optional \var{flags} argument has the same
300meaning as for \method{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
Fred Draked883ca11998-03-10 05:20:33 +0000308\method{recv()} call doesn't find any data, or if a \code{send} call can't
309immediately dispose of the data, a \exception{error} exception is
Guido van Rossum91951481994-09-07 14:39:14 +0000310raised; 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 Draked883ca11998-03-10 05:20:33 +0000315\manpage{setsockopt}{2}). The needed symbolic constants are defined in
316the \module{socket} module (\code{SO_*} etc.). The value can be an
Guido van Rossum8df36371995-02-27 17:52:15 +0000317integer 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
Fred Draked883ca11998-03-10 05:20:33 +0000320\module{struct}\refbimodindex{struct} for a way to encode C structures
321as strings).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000322\end{funcdesc}
323
324\begin{funcdesc}{shutdown}{how}
Fred Draked883ca11998-03-10 05:20:33 +0000325Shut down one or both halves of the connection. If \var{how} is
326\code{0}, further receives are disallowed. If \var{how} is \code{1},
327further sends are disallowed. If \var{how} is \code{2}, further sends
328and receives are disallowed.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000329\end{funcdesc}
330
Fred Draked883ca11998-03-10 05:20:33 +0000331Note that there are no methods \method{read()} or \method{write()};
332use \method{recv()} and \method{send()} without \var{flags} argument
333instead.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000334
335\subsection{Example}
336\nodename{Socket Example}
337
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000338Here are two minimal example programs using the TCP/IP protocol:\ a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000339server that echoes all data that it receives back (servicing only one
340client), and a client using it. Note that a server must perform the
Fred Draked883ca11998-03-10 05:20:33 +0000341sequence \function{socket()}, \method{bind()}, \method{listen()},
342\method{accept()} (possibly repeating the \method{accept()} to service
343more than one client), while a client only needs the sequence
344\function{socket()}, \method{connect()}. Also note that the server
345does not \method{send()}/\method{recv()} on the
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000346socket it is listening on but on the new socket returned by
Fred Draked883ca11998-03-10 05:20:33 +0000347\method{accept()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000348
Fred Drake19479911998-02-13 06:58:54 +0000349\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000350# Echo server program
351from socket import *
352HOST = '' # Symbolic name meaning the local host
353PORT = 50007 # Arbitrary non-privileged server
354s = socket(AF_INET, SOCK_STREAM)
355s.bind(HOST, PORT)
Guido van Rossum5da57551994-03-02 10:52:16 +0000356s.listen(1)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000357conn, addr = s.accept()
358print 'Connected by', addr
359while 1:
360 data = conn.recv(1024)
361 if not data: break
362 conn.send(data)
363conn.close()
Fred Drake19479911998-02-13 06:58:54 +0000364\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000365
Fred Drake19479911998-02-13 06:58:54 +0000366\begin{verbatim}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000367# Echo client program
368from socket import *
369HOST = 'daring.cwi.nl' # The remote host
370PORT = 50007 # The same port as used by the server
371s = socket(AF_INET, SOCK_STREAM)
372s.connect(HOST, PORT)
373s.send('Hello, world')
374data = s.recv(1024)
375s.close()
376print 'Received', `data`
Fred Drake19479911998-02-13 06:58:54 +0000377\end{verbatim}
Fred Draked883ca11998-03-10 05:20:33 +0000378
Guido van Rossume47da0a1997-07-17 16:34:52 +0000379\begin{seealso}
380\seemodule{SocketServer}{classes that simplify writing network servers}
381\end{seealso}