Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{socket}} |
| 2 | |
| 3 | \bimodindex{socket} |
| 4 | This module provides access to the BSD {\em socket} interface. |
| 5 | It is available on \UNIX{} systems that support this interface. |
| 6 | |
| 7 | For an introduction to socket programming (in C), see the following |
| 8 | papers: \emph{An Introductory 4.3BSD Interprocess Communication |
| 9 | Tutorial}, by Stuart Sechrest and \emph{An Advanced 4.3BSD Interprocess |
| 10 | Communication Tutorial}, by Samuel J. Leffler et al, both in the |
| 11 | \UNIX{} Programmer's Manual, Supplementary Documents 1 (sections PS1:7 |
| 12 | and PS1:8). The \UNIX{} manual pages for the various socket-related |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 13 | system calls are also a valuable source of information on the details of |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 14 | socket semantics. |
| 15 | |
| 16 | The Python interface is a straightforward transliteration of the |
| 17 | \UNIX{} system call and library interface for sockets to Python's |
| 18 | object-oriented style: the \code{socket()} function returns a |
| 19 | \dfn{socket object} whose methods implement the various socket system |
Barry Warsaw | d44be3f | 1997-01-03 20:19:05 +0000 | [diff] [blame] | 20 | calls. Parameter types are somewhat higher-level than in the C |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 21 | interface: as with \code{read()} and \code{write()} operations on Python |
| 22 | files, buffer allocation on receive operations is automatic, and |
| 23 | buffer length is implicit on send operations. |
| 24 | |
| 25 | Socket 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, |
| 28 | where \var{host} is a string representing |
| 29 | either a hostname in Internet domain notation like |
| 30 | \code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'}, |
| 31 | and \var{port} is an integral port number. Other address families are |
| 32 | currently not supported. The address format required by a particular |
| 33 | socket object is automatically selected based on the address family |
| 34 | specified when the socket object was created. |
| 35 | |
| 36 | All errors raise exceptions. The normal exceptions for invalid |
| 37 | argument types and out-of-memory conditions can be raised; errors |
| 38 | related to socket or address semantics raise the error \code{socket.error}. |
| 39 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 40 | Non-blocking mode is supported through the \code{setblocking()} |
| 41 | method. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 42 | |
| 43 | The module \code{socket} exports the following constants and functions: |
| 44 | |
| 45 | \renewcommand{\indexsubitem}{(in module socket)} |
| 46 | \begin{excdesc}{error} |
| 47 | This exception is raised for socket- or address-related errors. |
| 48 | The accompanying value is either a string telling what went wrong or a |
| 49 | pair \code{(\var{errno}, \var{string})} |
| 50 | representing an error returned by a system |
| 51 | call, similar to the value accompanying \code{posix.error}. |
| 52 | \end{excdesc} |
| 53 | |
| 54 | \begin{datadesc}{AF_UNIX} |
| 55 | \dataline{AF_INET} |
| 56 | These constants represent the address (and protocol) families, |
Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 57 | used for the first argument to \code{socket()}. If the \code{AF_UNIX} |
| 58 | constant is not defined then this protocol is unsupported. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 59 | \end{datadesc} |
| 60 | |
| 61 | \begin{datadesc}{SOCK_STREAM} |
| 62 | \dataline{SOCK_DGRAM} |
Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 63 | \dataline{SOCK_RAW} |
| 64 | \dataline{SOCK_RDM} |
| 65 | \dataline{SOCK_SEQPACKET} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 66 | These constants represent the socket types, |
| 67 | used for the second argument to \code{socket()}. |
Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 68 | (Only \code{SOCK_STREAM} and |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 69 | \code{SOCK_DGRAM} appear to be generally useful.) |
| 70 | \end{datadesc} |
| 71 | |
Guido van Rossum | ed2bad8 | 1995-02-16 16:29:18 +0000 | [diff] [blame] | 72 | \begin{datadesc}{SO_*} |
| 73 | \dataline{SOMAXCONN} |
| 74 | \dataline{MSG_*} |
| 75 | \dataline{SOL_*} |
| 76 | \dataline{IPPROTO_*} |
| 77 | \dataline{IPPORT_*} |
| 78 | \dataline{INADDR_*} |
| 79 | \dataline{IP_*} |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 80 | Many constants of these forms, documented in the \UNIX{} documentation on |
Guido van Rossum | ed2bad8 | 1995-02-16 16:29:18 +0000 | [diff] [blame] | 81 | sockets and/or the IP protocol, are also defined in the socket module. |
| 82 | They are generally used in arguments to the \code{setsockopt} and |
| 83 | \code{getsockopt} methods of socket objects. In most cases, only |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 84 | those symbols that are defined in the \UNIX{} header files are defined; |
Guido van Rossum | ed2bad8 | 1995-02-16 16:29:18 +0000 | [diff] [blame] | 85 | for a few symbols, default values are provided. |
| 86 | \end{datadesc} |
| 87 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 88 | \begin{funcdesc}{gethostbyname}{hostname} |
| 89 | Translate a host name to IP address format. The IP address is |
| 90 | returned as a string, e.g., \code{'100.50.200.5'}. If the host name |
| 91 | is an IP address itself it is returned unchanged. |
| 92 | \end{funcdesc} |
| 93 | |
Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 94 | \begin{funcdesc}{gethostname}{} |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 95 | Return a string containing the hostname of the machine where |
| 96 | the Python interpreter is currently executing. If you want to know the |
| 97 | current machine's IP address, use |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 98 | \code{socket.gethostbyname(socket.gethostname())}. |
Guido van Rossum | fe27a50 | 1997-01-11 17:04:56 +0000 | [diff] [blame] | 99 | Note: \code{gethostname()} doesn't always return the fully qualified |
| 100 | domain name; use \code{socket.gethostbyaddr(socket.gethostname())} |
| 101 | (see below). |
Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 102 | \end{funcdesc} |
| 103 | |
| 104 | \begin{funcdesc}{gethostbyaddr}{ip_address} |
| 105 | Return a triple \code{(hostname, aliaslist, ipaddrlist)} where |
| 106 | \code{hostname} is the primary host name responding to the given |
| 107 | \var{ip_address}, \code{aliaslist} is a (possibly empty) list of |
| 108 | alternative host names for the same address, and \code{ipaddrlist} is |
| 109 | a list of IP addresses for the same interface on the same |
| 110 | host (most likely containing only a single address). |
Guido van Rossum | fe27a50 | 1997-01-11 17:04:56 +0000 | [diff] [blame] | 111 | To find the fully qualified domain name, check \var{hostname} and the |
| 112 | items of \var{aliaslist} for an entry containing at least one period. |
Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 113 | \end{funcdesc} |
| 114 | |
Guido van Rossum | 62ac99e | 1996-12-19 16:43:25 +0000 | [diff] [blame] | 115 | \begin{funcdesc}{getprotobyname}{protocolname} |
| 116 | Translate an Internet protocol name (e.g. \code{'icmp'}) to a constant |
| 117 | suitable for passing as the (optional) third argument to the |
| 118 | \code{socket()} function. This is usually only needed for sockets |
| 119 | opened in ``raw'' mode (\code{SOCK_RAW}); for the normal socket modes, |
| 120 | the correct protocol is chosen automatically if the protocol is |
| 121 | omitted or zero. |
| 122 | \end{funcdesc} |
| 123 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 124 | \begin{funcdesc}{getservbyname}{servicename\, protocolname} |
| 125 | Translate an Internet service name and protocol name to a port number |
| 126 | for that service. The protocol name should be \code{'tcp'} or |
| 127 | \code{'udp'}. |
| 128 | \end{funcdesc} |
| 129 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 130 | \begin{funcdesc}{socket}{family\, type\optional{\, proto}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 131 | Create a new socket using the given address family, socket type and |
| 132 | protocol number. The address family should be \code{AF_INET} or |
| 133 | \code{AF_UNIX}. The socket type should be \code{SOCK_STREAM}, |
| 134 | \code{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants. |
| 135 | The protocol number is usually zero and may be omitted in that case. |
| 136 | \end{funcdesc} |
| 137 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 138 | \begin{funcdesc}{fromfd}{fd\, family\, type\optional{\, proto}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 139 | Build a socket object from an existing file descriptor (an integer as |
| 140 | returned by a file object's \code{fileno} method). Address family, |
| 141 | socket type and protocol number are as for the \code{socket} function |
| 142 | above. The file descriptor should refer to a socket, but this is not |
| 143 | checked --- subsequent operations on the object may fail if the file |
| 144 | descriptor is invalid. This function is rarely needed, but can be |
| 145 | used to get or set socket options on a socket passed to a program as |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 146 | standard input or output (e.g.\ a server started by the \UNIX{} inet |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 147 | daemon). |
| 148 | \end{funcdesc} |
| 149 | |
Guido van Rossum | bda7ca7 | 1996-12-02 17:24:10 +0000 | [diff] [blame] | 150 | \begin{funcdesc}{ntohl}{x} |
| 151 | \funcline{ntohs}{x} |
| 152 | \funcline{htonl}{x} |
| 153 | \funcline{htons}{x} |
| 154 | These functions convert 32-bit (`l' suffix) and 16-bit (`s' suffix) |
| 155 | integers between network and host byte order. On machines where the |
| 156 | host byte order is the same as the network byte order, they are no-ops |
| 157 | (assuming the values fit in the indicated size); otherwise, they |
| 158 | perform 2-byte or 4-byte swap operations. |
| 159 | \end{funcdesc} |
| 160 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 161 | \subsection{Socket Objects} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 162 | |
| 163 | \noindent |
| 164 | Socket objects have the following methods. Except for |
| 165 | \code{makefile()} these correspond to \UNIX{} system calls applicable to |
| 166 | sockets. |
| 167 | |
| 168 | \renewcommand{\indexsubitem}{(socket method)} |
| 169 | \begin{funcdesc}{accept}{} |
| 170 | Accept a connection. |
| 171 | The socket must be bound to an address and listening for connections. |
| 172 | The return value is a pair \code{(\var{conn}, \var{address})} |
| 173 | where \var{conn} is a \emph{new} socket object usable to send and |
| 174 | receive data on the connection, and \var{address} is the address bound |
| 175 | to the socket on the other end of the connection. |
| 176 | \end{funcdesc} |
| 177 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 178 | \begin{funcdesc}{bind}{address} |
Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 179 | Bind the socket to \var{address}. The socket must not already be bound. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 180 | (The format of \var{address} depends on the address family --- see above.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 181 | \end{funcdesc} |
| 182 | |
| 183 | \begin{funcdesc}{close}{} |
| 184 | Close the socket. All future operations on the socket object will fail. |
| 185 | The remote end will receive no more data (after queued data is flushed). |
| 186 | Sockets are automatically closed when they are garbage-collected. |
| 187 | \end{funcdesc} |
| 188 | |
| 189 | \begin{funcdesc}{connect}{address} |
Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 190 | Connect to a remote socket at \var{address}. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 191 | (The format of \var{address} depends on the address family --- see above.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 192 | \end{funcdesc} |
| 193 | |
| 194 | \begin{funcdesc}{fileno}{} |
| 195 | Return the socket's file descriptor (a small integer). This is useful |
| 196 | with \code{select}. |
| 197 | \end{funcdesc} |
| 198 | |
| 199 | \begin{funcdesc}{getpeername}{} |
| 200 | Return the remote address to which the socket is connected. This is |
| 201 | useful to find out the port number of a remote IP socket, for instance. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 202 | (The format of the address returned depends on the address family --- |
Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 203 | see above.) On some systems this function is not supported. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 204 | \end{funcdesc} |
| 205 | |
| 206 | \begin{funcdesc}{getsockname}{} |
| 207 | Return the socket's own address. This is useful to find out the port |
| 208 | number of an IP socket, for instance. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 209 | (The format of the address returned depends on the address family --- |
Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 210 | see above.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 211 | \end{funcdesc} |
| 212 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 213 | \begin{funcdesc}{getsockopt}{level\, optname\optional{\, buflen}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 214 | Return the value of the given socket option (see the \UNIX{} man page |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 215 | {\it getsockopt}(2)). The needed symbolic constants (\code{SO_*} etc.) |
| 216 | are defined in this module. If \var{buflen} |
| 217 | is absent, an integer option is assumed and its integer value |
Guido van Rossum | 8df3637 | 1995-02-27 17:52:15 +0000 | [diff] [blame] | 218 | is returned by the function. If \var{buflen} is present, it specifies |
| 219 | the maximum length of the buffer used to receive the option in, and |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 220 | this buffer is returned as a string. It is up to the caller to decode |
Guido van Rossum | 8df3637 | 1995-02-27 17:52:15 +0000 | [diff] [blame] | 221 | the contents of the buffer (see the optional built-in module |
| 222 | \code{struct} for a way to decode C structures encoded as strings). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 223 | \end{funcdesc} |
| 224 | |
| 225 | \begin{funcdesc}{listen}{backlog} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 226 | Listen for connections made to the socket. The \var{backlog} argument |
| 227 | specifies the maximum number of queued connections and should be at |
| 228 | least 1; the maximum value is system-dependent (usually 5). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 229 | \end{funcdesc} |
| 230 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 231 | \begin{funcdesc}{makefile}{\optional{mode\optional{\, bufsize}}} |
| 232 | Return a \dfn{file object} associated with the socket. (File objects |
| 233 | were described earlier under Built-in Types.) The file object |
| 234 | references a \code{dup()}ped version of the socket file descriptor, so |
| 235 | the file object and socket object may be closed or garbage-collected |
| 236 | independently. The optional \var{mode} and \var{bufsize} arguments |
| 237 | are interpreted the same way as by the built-in |
| 238 | \code{open()} function. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 239 | \end{funcdesc} |
| 240 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 241 | \begin{funcdesc}{recv}{bufsize\optional{\, flags}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 242 | Receive data from the socket. The return value is a string representing |
| 243 | the data received. The maximum amount of data to be received |
| 244 | at once is specified by \var{bufsize}. See the \UNIX{} manual page |
| 245 | for the meaning of the optional argument \var{flags}; it defaults to |
| 246 | zero. |
| 247 | \end{funcdesc} |
| 248 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 249 | \begin{funcdesc}{recvfrom}{bufsize\optional{\, flags}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 250 | Receive data from the socket. The return value is a pair |
| 251 | \code{(\var{string}, \var{address})} where \var{string} is a string |
| 252 | representing the data received and \var{address} is the address of the |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 253 | socket sending the data. The optional \var{flags} argument has the |
| 254 | same meaning as for \code{recv()} above. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 255 | (The format of \var{address} depends on the address family --- see above.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 256 | \end{funcdesc} |
| 257 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 258 | \begin{funcdesc}{send}{string\optional{\, flags}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 259 | Send data to the socket. The socket must be connected to a remote |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 260 | socket. The optional \var{flags} argument has the same meaning as for |
| 261 | \code{recv()} above. Return the number of bytes sent. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 262 | \end{funcdesc} |
| 263 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 264 | \begin{funcdesc}{sendto}{string\optional{\, flags}\, address} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 265 | Send data to the socket. The socket should not be connected to a |
| 266 | remote socket, since the destination socket is specified by |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 267 | \code{address}. The optional \var{flags} argument has the same |
| 268 | meaning as for \code{recv()} above. Return the number of bytes sent. |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 269 | (The format of \var{address} depends on the address family --- see above.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 270 | \end{funcdesc} |
| 271 | |
Guido van Rossum | 9195148 | 1994-09-07 14:39:14 +0000 | [diff] [blame] | 272 | \begin{funcdesc}{setblocking}{flag} |
| 273 | Set blocking or non-blocking mode of the socket: if \var{flag} is 0, |
| 274 | the socket is set to non-blocking, else to blocking mode. Initially |
| 275 | all sockets are in blocking mode. In non-blocking mode, if a |
| 276 | \code{recv} call doesn't find any data, or if a \code{send} call can't |
| 277 | immediately dispose of the data, a \code{socket.error} exception is |
| 278 | raised; in blocking mode, the calls block until they can proceed. |
| 279 | \end{funcdesc} |
| 280 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 281 | \begin{funcdesc}{setsockopt}{level\, optname\, value} |
| 282 | Set the value of the given socket option (see the \UNIX{} man page |
Guido van Rossum | 8df3637 | 1995-02-27 17:52:15 +0000 | [diff] [blame] | 283 | {\it setsockopt}(2)). The needed symbolic constants are defined in |
| 284 | the \code{socket} module (\code{SO_*} etc.). The value can be an |
| 285 | integer or a string representing a buffer. In the latter case it is |
| 286 | up to the caller to ensure that the string contains the proper bits |
| 287 | (see the optional built-in module |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 288 | \code{struct} for a way to encode C structures as strings). |
| 289 | \end{funcdesc} |
| 290 | |
| 291 | \begin{funcdesc}{shutdown}{how} |
| 292 | Shut down one or both halves of the connection. If \var{how} is \code{0}, |
| 293 | further receives are disallowed. If \var{how} is \code{1}, further sends are |
| 294 | disallowed. If \var{how} is \code{2}, further sends and receives are |
| 295 | disallowed. |
| 296 | \end{funcdesc} |
| 297 | |
| 298 | Note that there are no methods \code{read()} or \code{write()}; use |
| 299 | \code{recv()} and \code{send()} without \var{flags} argument instead. |
| 300 | |
| 301 | \subsection{Example} |
| 302 | \nodename{Socket Example} |
| 303 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 304 | Here are two minimal example programs using the TCP/IP protocol:\ a |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 305 | server that echoes all data that it receives back (servicing only one |
| 306 | client), and a client using it. Note that a server must perform the |
| 307 | sequence \code{socket}, \code{bind}, \code{listen}, \code{accept} |
| 308 | (possibly repeating the \code{accept} to service more than one client), |
| 309 | while a client only needs the sequence \code{socket}, \code{connect}. |
| 310 | Also note that the server does not \code{send}/\code{receive} on the |
| 311 | socket it is listening on but on the new socket returned by |
| 312 | \code{accept}. |
| 313 | |
| 314 | \bcode\begin{verbatim} |
| 315 | # Echo server program |
| 316 | from socket import * |
| 317 | HOST = '' # Symbolic name meaning the local host |
| 318 | PORT = 50007 # Arbitrary non-privileged server |
| 319 | s = socket(AF_INET, SOCK_STREAM) |
| 320 | s.bind(HOST, PORT) |
Guido van Rossum | 5da5755 | 1994-03-02 10:52:16 +0000 | [diff] [blame] | 321 | s.listen(1) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 322 | conn, addr = s.accept() |
| 323 | print 'Connected by', addr |
| 324 | while 1: |
| 325 | data = conn.recv(1024) |
| 326 | if not data: break |
| 327 | conn.send(data) |
| 328 | conn.close() |
| 329 | \end{verbatim}\ecode |
| 330 | |
| 331 | \bcode\begin{verbatim} |
| 332 | # Echo client program |
| 333 | from socket import * |
| 334 | HOST = 'daring.cwi.nl' # The remote host |
| 335 | PORT = 50007 # The same port as used by the server |
| 336 | s = socket(AF_INET, SOCK_STREAM) |
| 337 | s.connect(HOST, PORT) |
| 338 | s.send('Hello, world') |
| 339 | data = s.recv(1024) |
| 340 | s.close() |
| 341 | print 'Received', `data` |
| 342 | \end{verbatim}\ecode |