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