| Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{socket} --- | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 2 |          Low-level networking interface} | 
| Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 |  | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{socket} | 
| Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Low-level networking interface.} | 
 | 6 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 7 |  | 
| Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 8 | This module provides access to the BSD \emph{socket} interface. | 
| Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 9 | It is available on all modern \UNIX{} systems, Windows, MacOS, BeOS, | 
 | 10 | OS/2, and probably additional platforms. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 11 |  | 
 | 12 | For an introduction to socket programming (in C), see the following | 
| Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 13 | papers: \citetitle{An Introductory 4.3BSD Interprocess Communication | 
 | 14 | Tutorial}, by Stuart Sechrest and \citetitle{An Advanced 4.3BSD | 
 | 15 | Interprocess Communication Tutorial}, by Samuel J.  Leffler et al, | 
 | 16 | both in the \citetitle{\UNIX{} Programmer's Manual, Supplementary Documents 1} | 
| Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 17 | (sections PS1:7 and PS1:8).  The platform-specific reference material | 
 | 18 | for the various socket-related system calls are also a valuable source | 
 | 19 | of information on the details of socket semantics.  For \UNIX, refer | 
 | 20 | to the manual pages; for Windows, see the WinSock (or Winsock 2) | 
 | 21 | specification. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 22 |  | 
 | 23 | The Python interface is a straightforward transliteration of the | 
 | 24 | \UNIX{} system call and library interface for sockets to Python's | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 25 | object-oriented style: the \function{socket()} function returns a | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 26 | \dfn{socket object}\obindex{socket} whose methods implement the | 
 | 27 | various socket system calls.  Parameter types are somewhat | 
 | 28 | higher-level than in the C interface: as with \method{read()} and | 
 | 29 | \method{write()} operations on Python files, buffer allocation on | 
 | 30 | receive operations is automatic, and buffer length is implicit on send | 
 | 31 | operations. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 32 |  | 
 | 33 | Socket addresses are represented as a single string for the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 34 | \constant{AF_UNIX} address family and as a pair | 
 | 35 | \code{(\var{host}, \var{port})} for the \constant{AF_INET} address | 
 | 36 | family, where \var{host} is a string representing | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 37 | either a hostname in Internet domain notation like | 
 | 38 | \code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'}, | 
 | 39 | and \var{port} is an integral port number.  Other address families are | 
 | 40 | currently not supported.  The address format required by a particular | 
 | 41 | socket object is automatically selected based on the address family | 
 | 42 | specified when the socket object was created. | 
 | 43 |  | 
| Guido van Rossum | e4f347e | 1997-05-09 02:21:51 +0000 | [diff] [blame] | 44 | For IP addresses, two special forms are accepted instead of a host | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 45 | address: the empty string represents \constant{INADDR_ANY}, and the string | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 46 | \code{'<broadcast>'} represents \constant{INADDR_BROADCAST}. | 
| Guido van Rossum | e4f347e | 1997-05-09 02:21:51 +0000 | [diff] [blame] | 47 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 48 | All errors raise exceptions.  The normal exceptions for invalid | 
 | 49 | argument types and out-of-memory conditions can be raised; errors | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 50 | related to socket or address semantics raise the error | 
 | 51 | \exception{socket.error}. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 52 |  | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 53 | Non-blocking mode is supported through the | 
 | 54 | \method{setblocking()} method. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 55 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 56 | The module \module{socket} exports the following constants and functions: | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 57 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 58 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 59 | \begin{excdesc}{error} | 
 | 60 | This exception is raised for socket- or address-related errors. | 
 | 61 | The accompanying value is either a string telling what went wrong or a | 
 | 62 | pair \code{(\var{errno}, \var{string})} | 
 | 63 | representing an error returned by a system | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 64 | call, similar to the value accompanying \exception{os.error}. | 
 | 65 | See the module \refmodule{errno}\refbimodindex{errno}, which contains | 
| Guido van Rossum | 8e1e68d | 1998-02-06 15:18:25 +0000 | [diff] [blame] | 66 | names for the error codes defined by the underlying operating system. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 67 | \end{excdesc} | 
 | 68 |  | 
 | 69 | \begin{datadesc}{AF_UNIX} | 
 | 70 | \dataline{AF_INET} | 
 | 71 | These constants represent the address (and protocol) families, | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 72 | used for the first argument to \function{socket()}.  If the | 
 | 73 | \constant{AF_UNIX} constant is not defined then this protocol is | 
 | 74 | unsupported. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 75 | \end{datadesc} | 
 | 76 |  | 
 | 77 | \begin{datadesc}{SOCK_STREAM} | 
 | 78 | \dataline{SOCK_DGRAM} | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 79 | \dataline{SOCK_RAW} | 
 | 80 | \dataline{SOCK_RDM} | 
 | 81 | \dataline{SOCK_SEQPACKET} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 82 | These constants represent the socket types, | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 83 | used for the second argument to \function{socket()}. | 
 | 84 | (Only \constant{SOCK_STREAM} and | 
 | 85 | \constant{SOCK_DGRAM} appear to be generally useful.) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 86 | \end{datadesc} | 
 | 87 |  | 
| Guido van Rossum | ed2bad8 | 1995-02-16 16:29:18 +0000 | [diff] [blame] | 88 | \begin{datadesc}{SO_*} | 
 | 89 | \dataline{SOMAXCONN} | 
 | 90 | \dataline{MSG_*} | 
 | 91 | \dataline{SOL_*} | 
 | 92 | \dataline{IPPROTO_*} | 
 | 93 | \dataline{IPPORT_*} | 
 | 94 | \dataline{INADDR_*} | 
 | 95 | \dataline{IP_*} | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 96 | Many constants of these forms, documented in the \UNIX{} documentation on | 
| Guido van Rossum | ed2bad8 | 1995-02-16 16:29:18 +0000 | [diff] [blame] | 97 | sockets and/or the IP protocol, are also defined in the socket module. | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 98 | They are generally used in arguments to the \method{setsockopt()} and | 
 | 99 | \method{getsockopt()} methods of socket objects.  In most cases, only | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 100 | 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] | 101 | for a few symbols, default values are provided. | 
 | 102 | \end{datadesc} | 
 | 103 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 104 | \begin{funcdesc}{gethostbyname}{hostname} | 
 | 105 | Translate a host name to IP address format.  The IP address is | 
 | 106 | returned as a string, e.g.,  \code{'100.50.200.5'}.  If the host name | 
| Guido van Rossum | cdf6af1 | 1998-08-07 18:07:36 +0000 | [diff] [blame] | 107 | is an IP address itself it is returned unchanged.  See | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 108 | \function{gethostbyname_ex()} for a more complete interface. | 
| Guido van Rossum | cdf6af1 | 1998-08-07 18:07:36 +0000 | [diff] [blame] | 109 | \end{funcdesc} | 
 | 110 |  | 
 | 111 | \begin{funcdesc}{gethostbyname_ex}{hostname} | 
 | 112 | Translate a host name to IP address format, extended interface. | 
 | 113 | Return a triple \code{(hostname, aliaslist, ipaddrlist)} where | 
 | 114 | \code{hostname} is the primary host name responding to the given | 
 | 115 | \var{ip_address}, \code{aliaslist} is a (possibly empty) list of | 
 | 116 | alternative host names for the same address, and \code{ipaddrlist} is | 
 | 117 | a list of IP addresses for the same interface on the same | 
 | 118 | host (often but not always a single address). | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 119 | \end{funcdesc} | 
 | 120 |  | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 121 | \begin{funcdesc}{gethostname}{} | 
| Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 122 | Return a string containing the hostname of the machine where  | 
 | 123 | the Python interpreter is currently executing.  If you want to know the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 124 | current machine's IP address, use \code{gethostbyname(gethostname())}. | 
 | 125 | Note: \function{gethostname()} doesn't always return the fully qualified | 
 | 126 | domain name; use \code{gethostbyaddr(gethostname())} | 
| Guido van Rossum | fe27a50 | 1997-01-11 17:04:56 +0000 | [diff] [blame] | 127 | (see below). | 
| Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 128 | \end{funcdesc} | 
 | 129 |  | 
 | 130 | \begin{funcdesc}{gethostbyaddr}{ip_address} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 131 | Return a triple \code{(\var{hostname}, \var{aliaslist}, | 
 | 132 | \var{ipaddrlist})} where \var{hostname} is the primary host name | 
 | 133 | responding to the given \var{ip_address}, \var{aliaslist} is a | 
 | 134 | (possibly empty) list of alternative host names for the same address, | 
 | 135 | and \var{ipaddrlist} is a list of IP addresses for the same interface | 
 | 136 | on the same host (most likely containing only a single address). | 
| Guido van Rossum | fe27a50 | 1997-01-11 17:04:56 +0000 | [diff] [blame] | 137 | To find the fully qualified domain name, check \var{hostname} and the | 
 | 138 | 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] | 139 | \end{funcdesc} | 
 | 140 |  | 
| Guido van Rossum | 62ac99e | 1996-12-19 16:43:25 +0000 | [diff] [blame] | 141 | \begin{funcdesc}{getprotobyname}{protocolname} | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 142 | Translate an Internet protocol name (e.g.\ \code{'icmp'}) to a constant | 
| Guido van Rossum | 62ac99e | 1996-12-19 16:43:25 +0000 | [diff] [blame] | 143 | suitable for passing as the (optional) third argument to the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 144 | \function{socket()} function.  This is usually only needed for sockets | 
 | 145 | opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket | 
 | 146 | modes, the correct protocol is chosen automatically if the protocol is | 
| Guido van Rossum | 62ac99e | 1996-12-19 16:43:25 +0000 | [diff] [blame] | 147 | omitted or zero. | 
 | 148 | \end{funcdesc} | 
 | 149 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 150 | \begin{funcdesc}{getservbyname}{servicename, protocolname} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 151 | Translate an Internet service name and protocol name to a port number | 
 | 152 | for that service.  The protocol name should be \code{'tcp'} or | 
 | 153 | \code{'udp'}. | 
 | 154 | \end{funcdesc} | 
 | 155 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 156 | \begin{funcdesc}{socket}{family, type\optional{, proto}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 157 | Create a new socket using the given address family, socket type and | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 158 | protocol number.  The address family should be \constant{AF_INET} or | 
 | 159 | \constant{AF_UNIX}.  The socket type should be \constant{SOCK_STREAM}, | 
 | 160 | \constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 161 | The protocol number is usually zero and may be omitted in that case. | 
 | 162 | \end{funcdesc} | 
 | 163 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 164 | \begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 165 | Build a socket object from an existing file descriptor (an integer as | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 166 | returned by a file object's \method{fileno()} method).  Address family, | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 167 | socket type and protocol number are as for the \function{socket()} function | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 168 | above.  The file descriptor should refer to a socket, but this is not | 
 | 169 | checked --- subsequent operations on the object may fail if the file | 
 | 170 | descriptor is invalid.  This function is rarely needed, but can be | 
 | 171 | 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] | 172 | 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] | 173 | daemon). | 
 | 174 | \end{funcdesc} | 
 | 175 |  | 
| Guido van Rossum | bda7ca7 | 1996-12-02 17:24:10 +0000 | [diff] [blame] | 176 | \begin{funcdesc}{ntohl}{x} | 
| Fred Drake | c5aec05 | 1997-12-08 21:25:41 +0000 | [diff] [blame] | 177 | Convert 32-bit integers from network to host byte order.  On machines | 
 | 178 | where the host byte order is the same as network byte order, this is a | 
 | 179 | no-op; otherwise, it performs a 4-byte swap operation. | 
 | 180 | \end{funcdesc} | 
 | 181 |  | 
 | 182 | \begin{funcdesc}{ntohs}{x} | 
 | 183 | Convert 16-bit integers from network to host byte order.  On machines | 
 | 184 | where the host byte order is the same as network byte order, this is a | 
 | 185 | no-op; otherwise, it performs a 2-byte swap operation. | 
 | 186 | \end{funcdesc} | 
 | 187 |  | 
 | 188 | \begin{funcdesc}{htonl}{x} | 
 | 189 | Convert 32-bit integers from host to network byte order.  On machines | 
 | 190 | where the host byte order is the same as network byte order, this is a | 
 | 191 | no-op; otherwise, it performs a 4-byte swap operation. | 
 | 192 | \end{funcdesc} | 
 | 193 |  | 
 | 194 | \begin{funcdesc}{htons}{x} | 
 | 195 | Convert 16-bit integers from host to network byte order.  On machines | 
 | 196 | where the host byte order is the same as network byte order, this is a | 
 | 197 | no-op; otherwise, it performs a 2-byte swap operation. | 
| Guido van Rossum | bda7ca7 | 1996-12-02 17:24:10 +0000 | [diff] [blame] | 198 | \end{funcdesc} | 
 | 199 |  | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 200 | \begin{funcdesc}{inet_aton}{ip_string} | 
 | 201 | Convert an IP address from dotted-quad string format | 
 | 202 | (e.g.\ '123.45.67.89') to 32-bit packed binary format, as a string four | 
 | 203 | characters in length. | 
 | 204 |  | 
 | 205 | Useful when conversing with a program that uses the standard C library | 
 | 206 | and needs objects of type \ctype{struct in_addr}, which is the C type | 
 | 207 | for the 32-bit packed binary this function returns. | 
 | 208 |  | 
 | 209 | If the IP address string passed to this function is invalid, | 
 | 210 | \exception{socket.error} will be raised. Note that exactly what is | 
 | 211 | valid depends on the underlying C implementation of | 
 | 212 | \cfunction{inet_aton()}. | 
 | 213 | \end{funcdesc} | 
 | 214 |  | 
 | 215 | \begin{funcdesc}{inet_ntoa}{packed_ip} | 
 | 216 | Convert a 32-bit packed IP address (a string four characters in | 
 | 217 | length) to its standard dotted-quad string representation | 
 | 218 | (e.g. '123.45.67.89'). | 
 | 219 |  | 
 | 220 | Useful when conversing with a program that uses the standard C library | 
 | 221 | and needs objects of type \ctype{struct in_addr}, which is the C type | 
 | 222 | for the 32-bit packed binary this function takes as an argument. | 
 | 223 |  | 
 | 224 | If the string passed to this function is not exactly 4 bytes in | 
 | 225 | length, \exception{socket.error} will be raised. | 
 | 226 | \end{funcdesc} | 
 | 227 |  | 
| Fred Drake | 5451d67 | 1997-10-13 21:31:02 +0000 | [diff] [blame] | 228 | \begin{datadesc}{SocketType} | 
| Guido van Rossum | 2335c5e | 1997-05-21 14:41:42 +0000 | [diff] [blame] | 229 | This is a Python type object that represents the socket object type. | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 230 | It is the same as \code{type(socket(...))}. | 
| Guido van Rossum | 2335c5e | 1997-05-21 14:41:42 +0000 | [diff] [blame] | 231 | \end{datadesc} | 
 | 232 |  | 
| Fred Drake | aa7524c | 2000-07-06 18:37:08 +0000 | [diff] [blame] | 233 |  | 
 | 234 | \begin{seealso} | 
 | 235 |   \seemodule{SocketServer}{Classes that simplify writing network servers.} | 
 | 236 | \end{seealso} | 
 | 237 |  | 
 | 238 |  | 
| Fred Drake | a94f676 | 1999-08-05 13:41:04 +0000 | [diff] [blame] | 239 | \subsection{Socket Objects \label{socket-objects}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 240 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 241 | Socket objects have the following methods.  Except for | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 242 | \method{makefile()} these correspond to \UNIX{} system calls | 
 | 243 | applicable to sockets. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 244 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 245 | \begin{methoddesc}[socket]{accept}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 246 | Accept a connection. | 
 | 247 | The socket must be bound to an address and listening for connections. | 
 | 248 | The return value is a pair \code{(\var{conn}, \var{address})} | 
 | 249 | where \var{conn} is a \emph{new} socket object usable to send and | 
 | 250 | receive data on the connection, and \var{address} is the address bound | 
 | 251 | to the socket on the other end of the connection. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 252 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 253 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 254 | \begin{methoddesc}[socket]{bind}{address} | 
| Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 255 | Bind the socket to \var{address}.  The socket must not already be bound. | 
| Fred Drake | 7d68690 | 2000-04-04 17:48:30 +0000 | [diff] [blame] | 256 | (The format of \var{address} depends on the address family --- see | 
 | 257 | above.)  \strong{Note:}  This method has historically accepted a pair | 
 | 258 | of parameters for \constant{AF_INET} addresses instead of only a | 
 | 259 | tuple.  This was never intentional and will no longer be available in | 
 | 260 | Python 1.7. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 261 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 262 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 263 | \begin{methoddesc}[socket]{close}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 264 | Close the socket.  All future operations on the socket object will fail. | 
 | 265 | The remote end will receive no more data (after queued data is flushed). | 
 | 266 | Sockets are automatically closed when they are garbage-collected. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 267 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 268 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 269 | \begin{methoddesc}[socket]{connect}{address} | 
| Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 270 | Connect to a remote socket at \var{address}. | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 271 | (The format of \var{address} depends on the address family --- see | 
| Fred Drake | 7d68690 | 2000-04-04 17:48:30 +0000 | [diff] [blame] | 272 | above.)  \strong{Note:}  This method has historically accepted a pair | 
 | 273 | of parameters for \constant{AF_INET} addresses instead of only a | 
 | 274 | tuple.  This was never intentional and will no longer be available in | 
 | 275 | Python 1.7. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 276 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 277 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 278 | \begin{methoddesc}[socket]{connect_ex}{address} | 
| Guido van Rossum | eefcba6 | 1997-12-09 19:47:24 +0000 | [diff] [blame] | 279 | Like \code{connect(\var{address})}, but return an error indicator | 
| Fred Drake | b0bc7f2 | 1999-05-06 22:03:50 +0000 | [diff] [blame] | 280 | instead of raising an exception for errors returned by the C-level | 
 | 281 | \cfunction{connect()} call (other problems, such as ``host not found,'' | 
 | 282 | can still raise exceptions).  The error indicator is \code{0} if the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 283 | operation succeeded, otherwise the value of the \cdata{errno} | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 284 | variable.  This is useful, e.g., for asynchronous connects. | 
| Fred Drake | 7d68690 | 2000-04-04 17:48:30 +0000 | [diff] [blame] | 285 | \strong{Note:}  This method has historically accepted a pair of | 
 | 286 | parameters for \constant{AF_INET} addresses instead of only a tuple. | 
 | 287 | This was never intentional and will no longer be available in Python | 
 | 288 | 1.7. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 289 | \end{methoddesc} | 
| Guido van Rossum | f7790c6 | 1997-11-18 15:29:20 +0000 | [diff] [blame] | 290 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 291 | \begin{methoddesc}[socket]{fileno}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 292 | Return the socket's file descriptor (a small integer).  This is useful | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 293 | with \function{select.select()}. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 294 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 295 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 296 | \begin{methoddesc}[socket]{getpeername}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 297 | Return the remote address to which the socket is connected.  This is | 
 | 298 | 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] | 299 | (The format of the address returned depends on the address family --- | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 300 | see above.)  On some systems this function is not supported. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 301 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 302 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 303 | \begin{methoddesc}[socket]{getsockname}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 304 | Return the socket's own address.  This is useful to find out the port | 
 | 305 | number of an IP socket, for instance. | 
| Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 306 | (The format of the address returned depends on the address family --- | 
| Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 307 | see above.) | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 308 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 309 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 310 | \begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 311 | Return the value of the given socket option (see the \UNIX{} man page | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 312 | \manpage{getsockopt}{2}).  The needed symbolic constants | 
 | 313 | (\constant{SO_*} etc.) are defined in this module.  If \var{buflen} | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 314 | is absent, an integer option is assumed and its integer value | 
| Guido van Rossum | 8df3637 | 1995-02-27 17:52:15 +0000 | [diff] [blame] | 315 | is returned by the function.  If \var{buflen} is present, it specifies | 
 | 316 | 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] | 317 | 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] | 318 | the contents of the buffer (see the optional built-in module | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 319 | \refmodule{struct} for a way to decode C structures encoded as strings). | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 320 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 321 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 322 | \begin{methoddesc}[socket]{listen}{backlog} | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 323 | Listen for connections made to the socket.  The \var{backlog} argument | 
 | 324 | specifies the maximum number of queued connections and should be at | 
 | 325 | least 1; the maximum value is system-dependent (usually 5). | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 326 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 327 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 328 | \begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}} | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 329 | Return a \dfn{file object} associated with the socket.  (File objects | 
| Fred Drake | a94f676 | 1999-08-05 13:41:04 +0000 | [diff] [blame] | 330 | are described in \ref{bltin-file-objects}, ``File Objects.'') | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 331 | The file object references a \cfunction{dup()}ped version of the | 
 | 332 | socket file descriptor, so the file object and socket object may be | 
| Fred Drake | a94f676 | 1999-08-05 13:41:04 +0000 | [diff] [blame] | 333 | closed or garbage-collected independently. | 
 | 334 | \index{I/O control!buffering}The optional \var{mode} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 335 | and \var{bufsize} arguments are interpreted the same way as by the | 
 | 336 | built-in \function{open()} function. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 337 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 338 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 339 | \begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 340 | Receive data from the socket.  The return value is a string representing | 
 | 341 | the data received.  The maximum amount of data to be received | 
 | 342 | at once is specified by \var{bufsize}.  See the \UNIX{} manual page | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 343 | \manpage{recv}{2} for the meaning of the optional argument | 
 | 344 | \var{flags}; it defaults to zero. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 345 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 346 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 347 | \begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 348 | Receive data from the socket.  The return value is a pair | 
 | 349 | \code{(\var{string}, \var{address})} where \var{string} is a string | 
 | 350 | 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] | 351 | socket sending the data.  The optional \var{flags} argument has the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 352 | same meaning as for \method{recv()} above. | 
| Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 353 | (The format of \var{address} depends on the address family --- see above.) | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 354 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 355 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 356 | \begin{methoddesc}[socket]{send}{string\optional{, flags}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 357 | 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] | 358 | socket.  The optional \var{flags} argument has the same meaning as for | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 359 | \method{recv()} above.  Returns the number of bytes sent. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 360 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 361 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 362 | \begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 363 | Send data to the socket.  The socket should not be connected to a | 
 | 364 | remote socket, since the destination socket is specified by | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 365 | \var{address}.  The optional \var{flags} argument has the same | 
 | 366 | meaning as for \method{recv()} above.  Return the number of bytes sent. | 
| Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 367 | (The format of \var{address} depends on the address family --- see above.) | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 368 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 369 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 370 | \begin{methoddesc}[socket]{setblocking}{flag} | 
| Guido van Rossum | 9195148 | 1994-09-07 14:39:14 +0000 | [diff] [blame] | 371 | Set blocking or non-blocking mode of the socket: if \var{flag} is 0, | 
 | 372 | the socket is set to non-blocking, else to blocking mode.  Initially | 
 | 373 | all sockets are in blocking mode.  In non-blocking mode, if a | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 374 | \method{recv()} call doesn't find any data, or if a | 
 | 375 | \method{send()} call can't immediately dispose of the data, a | 
 | 376 | \exception{error} exception is raised; in blocking mode, the calls | 
 | 377 | block until they can proceed. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 378 | \end{methoddesc} | 
| Guido van Rossum | 9195148 | 1994-09-07 14:39:14 +0000 | [diff] [blame] | 379 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 380 | \begin{methoddesc}[socket]{setsockopt}{level, optname, value} | 
| Fred Drake | 9a748aa | 2000-06-30 04:21:41 +0000 | [diff] [blame] | 381 | Set the value of the given socket option (see the \UNIX{} manual page | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 382 | \manpage{setsockopt}{2}).  The needed symbolic constants are defined in | 
 | 383 | the \module{socket} module (\code{SO_*} etc.).  The value can be an | 
| Guido van Rossum | 8df3637 | 1995-02-27 17:52:15 +0000 | [diff] [blame] | 384 | integer or a string representing a buffer.  In the latter case it is | 
 | 385 | up to the caller to ensure that the string contains the proper bits | 
 | 386 | (see the optional built-in module | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 387 | \refmodule{struct}\refbimodindex{struct} for a way to encode C | 
 | 388 | structures as strings).  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 389 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 390 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 391 | \begin{methoddesc}[socket]{shutdown}{how} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 392 | Shut down one or both halves of the connection.  If \var{how} is | 
 | 393 | \code{0}, further receives are disallowed.  If \var{how} is \code{1}, | 
 | 394 | further sends are disallowed.  If \var{how} is \code{2}, further sends | 
 | 395 | and receives are disallowed. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 396 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 397 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 398 | Note that there are no methods \method{read()} or \method{write()}; | 
 | 399 | use \method{recv()} and \method{send()} without \var{flags} argument | 
 | 400 | instead. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 401 |  | 
| Fred Drake | aa7524c | 2000-07-06 18:37:08 +0000 | [diff] [blame] | 402 |  | 
 | 403 | \subsection{Example \label{socket-example}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 404 |  | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 405 | 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] | 406 | server that echoes all data that it receives back (servicing only one | 
 | 407 | client), and a client using it.  Note that a server must perform the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 408 | sequence \function{socket()}, \method{bind()}, \method{listen()}, | 
 | 409 | \method{accept()} (possibly repeating the \method{accept()} to service | 
 | 410 | more than one client), while a client only needs the sequence | 
 | 411 | \function{socket()}, \method{connect()}.  Also note that the server | 
 | 412 | does not \method{send()}/\method{recv()} on the  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 413 | socket it is listening on but on the new socket returned by | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 414 | \method{accept()}. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 415 |  | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 416 | \begin{verbatim} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 417 | # Echo server program | 
 | 418 | from socket import * | 
 | 419 | HOST = ''                 # Symbolic name meaning the local host | 
 | 420 | PORT = 50007              # Arbitrary non-privileged server | 
 | 421 | s = socket(AF_INET, SOCK_STREAM) | 
| Fred Drake | 3d69c0e | 2000-05-03 19:40:32 +0000 | [diff] [blame] | 422 | s.bind((HOST, PORT)) | 
| Guido van Rossum | 5da5755 | 1994-03-02 10:52:16 +0000 | [diff] [blame] | 423 | s.listen(1) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 424 | conn, addr = s.accept() | 
 | 425 | print 'Connected by', addr | 
 | 426 | while 1: | 
 | 427 |     data = conn.recv(1024) | 
 | 428 |     if not data: break | 
 | 429 |     conn.send(data) | 
 | 430 | conn.close() | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 431 | \end{verbatim} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 432 |  | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 433 | \begin{verbatim} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 434 | # Echo client program | 
 | 435 | from socket import * | 
 | 436 | HOST = 'daring.cwi.nl'    # The remote host | 
 | 437 | PORT = 50007              # The same port as used by the server | 
 | 438 | s = socket(AF_INET, SOCK_STREAM) | 
| Fred Drake | 3d69c0e | 2000-05-03 19:40:32 +0000 | [diff] [blame] | 439 | s.connect((HOST, PORT)) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 440 | s.send('Hello, world') | 
 | 441 | data = s.recv(1024) | 
 | 442 | s.close() | 
 | 443 | print 'Received', `data` | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 444 | \end{verbatim} |