| 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. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 22 | For IPv6-ready APIs, readers may want to refer to RFC2553 titled | 
 | 23 | \cite{Basic Socket Interface Extensions for IPv6}. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 24 |  | 
 | 25 | The Python interface is a straightforward transliteration of the | 
 | 26 | \UNIX{} system call and library interface for sockets to Python's | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 27 | object-oriented style: the \function{socket()} function returns a | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 28 | \dfn{socket object}\obindex{socket} whose methods implement the | 
 | 29 | various socket system calls.  Parameter types are somewhat | 
 | 30 | higher-level than in the C interface: as with \method{read()} and | 
 | 31 | \method{write()} operations on Python files, buffer allocation on | 
 | 32 | receive operations is automatic, and buffer length is implicit on send | 
 | 33 | operations. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 34 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 35 | Socket addresses are represented as follows: | 
 | 36 | A single string is used for the \constant{AF_UNIX} address family. | 
 | 37 | A pair \code{(\var{host}, \var{port})} is used for the | 
 | 38 | \constant{AF_INET} address family, where \var{host} is a string | 
 | 39 | representing either a hostname in Internet domain notation like | 
 | 40 | \code{'daring.cwi.nl'} or an IPv4 address like \code{'100.50.200.5'}, | 
 | 41 | and \var{port} is an integral port number. | 
 | 42 | For \constant{AF_INET6} address family, a four-tuple | 
 | 43 | \code{(\var{host}, \var{port}, \var{flowinfo}, \var{scopeid})} is | 
 | 44 | used, where \var{flowinfo} and \var{scopeid} represents | 
 | 45 | \code{sin6_flowinfo} and \code{sin6_scope_id} member in | 
 | 46 | \constant{struct sockaddr_in6} in C. | 
 | 47 | For \module{socket} module methods, \var{flowinfo} and \var{scopeid} | 
 | 48 | can be omitted just for backward compatibility. Note, however, | 
 | 49 | omission of \var{scopeid} can cause problems in manipulating scoped | 
 | 50 | IPv6 addresses. Other address families are currently not supported. | 
 | 51 | The address format required by a particular socket object is | 
 | 52 | automatically selected based on the address family specified when the | 
 | 53 | socket object was created. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 54 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 55 | For IPv4 addresses, two special forms are accepted instead of a host | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 56 | address: the empty string represents \constant{INADDR_ANY}, and the string | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 57 | \code{'<broadcast>'} represents \constant{INADDR_BROADCAST}. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 58 | The behavior is not available for IPv6 for backward compatibility, | 
 | 59 | therefore, you may want to avoid these if you intend to support IPv6 with | 
 | 60 | your Python programs. | 
 | 61 |  | 
 | 62 | If you use a hostname in the \var{host} portion of IPv4/v6 socket | 
 | 63 | address, the program may show a nondeterministic behavior, as Python | 
 | 64 | uses the first address returned from the DNS resolution.  The socket | 
 | 65 | address will be resolved differently into an actual IPv4/v6 address, | 
 | 66 | depending on the results from DNS resolution and/or the host | 
 | 67 | configuration.  For deterministic behavior use a numeric address in | 
 | 68 | \var{host} portion. | 
| Guido van Rossum | e4f347e | 1997-05-09 02:21:51 +0000 | [diff] [blame] | 69 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 70 | All errors raise exceptions.  The normal exceptions for invalid | 
 | 71 | argument types and out-of-memory conditions can be raised; errors | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 72 | related to socket or address semantics raise the error | 
 | 73 | \exception{socket.error}. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 74 |  | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 75 | Non-blocking mode is supported through the | 
 | 76 | \method{setblocking()} method. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 77 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 78 | The module \module{socket} exports the following constants and functions: | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 79 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 80 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 81 | \begin{excdesc}{error} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 82 | This exception is raised for socket-related errors. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 83 | The accompanying value is either a string telling what went wrong or a | 
 | 84 | pair \code{(\var{errno}, \var{string})} | 
 | 85 | representing an error returned by a system | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 86 | call, similar to the value accompanying \exception{os.error}. | 
 | 87 | See the module \refmodule{errno}\refbimodindex{errno}, which contains | 
| Guido van Rossum | 8e1e68d | 1998-02-06 15:18:25 +0000 | [diff] [blame] | 88 | names for the error codes defined by the underlying operating system. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 89 | \end{excdesc} | 
 | 90 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 91 | \begin{excdesc}{herror} | 
 | 92 | This exception is raised for address-related errors, i.e. for | 
 | 93 | functions that use \var{h_errno} in C API, including | 
 | 94 | \function{gethostbyname_ex} and \function{gethostbyaddr}. | 
 | 95 |  | 
 | 96 | The accompanying value is a pair \code{(\var{h_errno}, \var{string})} | 
 | 97 | representing an error returned by a library call. \var{string} | 
 | 98 | represents the description of \var{h_errno}, as returned by | 
 | 99 | \cfunction{hstrerror} C API.  | 
 | 100 | \end{excdesc} | 
 | 101 |  | 
 | 102 | \begin{excdesc}{gaierror} | 
 | 103 | This exception is raised for address-related errors, for | 
 | 104 | \function{getaddrinfo} and \function{getnameinfo}. | 
 | 105 | The accompanying value is a pair \code{(\var{error}, \var{string})} | 
 | 106 | representing an error returned by a library call. | 
 | 107 | \var{string} represents the description of \var{error}, as returned | 
 | 108 | by \cfunction{gai_strerror} C API. | 
 | 109 | \end{excdesc} | 
 | 110 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 111 | \begin{datadesc}{AF_UNIX} | 
 | 112 | \dataline{AF_INET} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 113 | \dataline{AF_INET6} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 114 | These constants represent the address (and protocol) families, | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 115 | used for the first argument to \function{socket()}.  If the | 
 | 116 | \constant{AF_UNIX} constant is not defined then this protocol is | 
 | 117 | unsupported. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 118 | \end{datadesc} | 
 | 119 |  | 
 | 120 | \begin{datadesc}{SOCK_STREAM} | 
 | 121 | \dataline{SOCK_DGRAM} | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 122 | \dataline{SOCK_RAW} | 
 | 123 | \dataline{SOCK_RDM} | 
 | 124 | \dataline{SOCK_SEQPACKET} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 125 | These constants represent the socket types, | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 126 | used for the second argument to \function{socket()}. | 
 | 127 | (Only \constant{SOCK_STREAM} and | 
 | 128 | \constant{SOCK_DGRAM} appear to be generally useful.) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 129 | \end{datadesc} | 
 | 130 |  | 
| Guido van Rossum | ed2bad8 | 1995-02-16 16:29:18 +0000 | [diff] [blame] | 131 | \begin{datadesc}{SO_*} | 
 | 132 | \dataline{SOMAXCONN} | 
 | 133 | \dataline{MSG_*} | 
 | 134 | \dataline{SOL_*} | 
 | 135 | \dataline{IPPROTO_*} | 
 | 136 | \dataline{IPPORT_*} | 
 | 137 | \dataline{INADDR_*} | 
 | 138 | \dataline{IP_*} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 139 | \dataline{IPV6_*} | 
 | 140 | \dataline{EAI_*} | 
 | 141 | \dataline{AI_*} | 
 | 142 | \dataline{NI_*} | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 143 | Many constants of these forms, documented in the \UNIX{} documentation on | 
| Guido van Rossum | ed2bad8 | 1995-02-16 16:29:18 +0000 | [diff] [blame] | 144 | 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] | 145 | They are generally used in arguments to the \method{setsockopt()} and | 
 | 146 | \method{getsockopt()} methods of socket objects.  In most cases, only | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 147 | 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] | 148 | for a few symbols, default values are provided. | 
 | 149 | \end{datadesc} | 
 | 150 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 151 | \begin{funcdesc}{getaddrinfo}{host, port\optional{, family, socktype, proto, flags}} | 
 | 152 |  | 
 | 153 | Resolves the \var{host}/\var{port} argument, into a sequence of | 
 | 154 | 5-tuples that contain all the necessary argument for the sockets | 
 | 155 | manipulation. \var{host} is a domain name, a string representation of | 
 | 156 | IPv4/v6 address or \code{None}. | 
 | 157 | \var{port} is a string service name (like \code{``http''}), a numeric | 
 | 158 | port number or \code{None}. | 
 | 159 |  | 
 | 160 | The rest of the arguments are optional and must be numeric if | 
 | 161 | specified.  For \var{host} and \var{port}, by passing either an empty | 
 | 162 | string or \code{None}, you can pass \code{NULL} to the C API.  The | 
 | 163 | \function{getaddrinfo()} function returns a list of 5-tuples with | 
 | 164 | the following structure: | 
 | 165 |  | 
 | 166 | \code{(\var{family}, \var{socktype}, \var{proto}, \var{canonname}, \var{sockaddr})}. | 
 | 167 |  | 
 | 168 | \var{family}, \var{socktype}, \var{proto} are all integer and are meant to | 
 | 169 | be passed to the \function{socket()} function. | 
 | 170 | \var{canonname} is a string representing the canonical name of the \var{host}. | 
 | 171 | It can be a numeric IPv4/v6 address when \code{AI_CANONNAME} is specified | 
 | 172 | for a numeric \var{host}. | 
 | 173 | \var{sockaddr} is a tuple describing a socket address, as described above. | 
 | 174 | See \code{Lib/httplib.py} and other library files | 
 | 175 | for a typical usage of the function. | 
 | 176 | \versionadded{2.2} | 
 | 177 | \end{funcdesc} | 
 | 178 |  | 
| Fred Drake | 5772c86 | 2000-08-16 14:21:42 +0000 | [diff] [blame] | 179 | \begin{funcdesc}{getfqdn}{\optional{name}} | 
 | 180 | Return a fully qualified domain name for \var{name}. | 
 | 181 | If \var{name} is omitted or empty, it is interpreted as the local | 
 | 182 | host.  To find the fully qualified name, the hostname returned by | 
 | 183 | \function{gethostbyaddr()} is checked, then aliases for the host, if | 
 | 184 | available.  The first name which includes a period is selected.  In | 
 | 185 | case no fully qualified domain name is available, the hostname is | 
 | 186 | returned. | 
| Fred Drake | 8b2e8f8 | 2000-09-06 02:22:16 +0000 | [diff] [blame] | 187 | \versionadded{2.0} | 
| Fred Drake | 5772c86 | 2000-08-16 14:21:42 +0000 | [diff] [blame] | 188 | \end{funcdesc} | 
 | 189 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 190 | \begin{funcdesc}{gethostbyname}{hostname} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 191 | Translate a host name to IPv4 address format.  The IPv4 address is | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 192 | returned as a string, e.g.,  \code{'100.50.200.5'}.  If the host name | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 193 | is an IPv4 address itself it is returned unchanged.  See | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 194 | \function{gethostbyname_ex()} for a more complete interface. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 195 | \function{gethostbyname()} does not support IPv6 name resolution, and | 
 | 196 | \function{getaddrinfo()} should be used instead for IPv4/v6 dual stack support. | 
| Guido van Rossum | cdf6af1 | 1998-08-07 18:07:36 +0000 | [diff] [blame] | 197 | \end{funcdesc} | 
 | 198 |  | 
 | 199 | \begin{funcdesc}{gethostbyname_ex}{hostname} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 200 | Translate a host name to IPv4 address format, extended interface. | 
| Guido van Rossum | cdf6af1 | 1998-08-07 18:07:36 +0000 | [diff] [blame] | 201 | Return a triple \code{(hostname, aliaslist, ipaddrlist)} where | 
 | 202 | \code{hostname} is the primary host name responding to the given | 
 | 203 | \var{ip_address}, \code{aliaslist} is a (possibly empty) list of | 
 | 204 | alternative host names for the same address, and \code{ipaddrlist} is | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 205 | a list of IPv4 addresses for the same interface on the same | 
| Guido van Rossum | cdf6af1 | 1998-08-07 18:07:36 +0000 | [diff] [blame] | 206 | host (often but not always a single address). | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 207 | \function{gethostbyname_ex()} does not support IPv6 name resolution, and | 
 | 208 | \function{getaddrinfo()} should be used instead for IPv4/v6 dual stack support. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 209 | \end{funcdesc} | 
 | 210 |  | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 211 | \begin{funcdesc}{gethostname}{} | 
| Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 212 | Return a string containing the hostname of the machine where  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 213 | the Python interpreter is currently executing. | 
 | 214 | If you want to know the current machine's IP address, you may want to use | 
 | 215 | \code{gethostbyname(gethostname())}. | 
 | 216 | This operation assumes that there is a valid address-to-host mapping for | 
 | 217 | the host, and the assumption does not always hold. | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 218 | Note: \function{gethostname()} doesn't always return the fully qualified | 
 | 219 | domain name; use \code{gethostbyaddr(gethostname())} | 
| Guido van Rossum | fe27a50 | 1997-01-11 17:04:56 +0000 | [diff] [blame] | 220 | (see below). | 
| Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 221 | \end{funcdesc} | 
 | 222 |  | 
 | 223 | \begin{funcdesc}{gethostbyaddr}{ip_address} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 224 | Return a triple \code{(\var{hostname}, \var{aliaslist}, | 
 | 225 | \var{ipaddrlist})} where \var{hostname} is the primary host name | 
 | 226 | responding to the given \var{ip_address}, \var{aliaslist} is a | 
 | 227 | (possibly empty) list of alternative host names for the same address, | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 228 | and \var{ipaddrlist} is a list of IPv4/v6 addresses for the same interface | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 229 | on the same host (most likely containing only a single address). | 
| Fred Drake | 5772c86 | 2000-08-16 14:21:42 +0000 | [diff] [blame] | 230 | To find the fully qualified domain name, use the function | 
 | 231 | \function{getfqdn()}. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 232 | \function{gethostbyaddr} supports both IPv4 and IPv6. | 
 | 233 | \end{funcdesc} | 
 | 234 |  | 
 | 235 | \begin{funcdesc}{getnameinfo}{sockaddr, flags} | 
 | 236 | Translate a socket address \var{sockaddr} into a 2-tuple | 
 | 237 | \code{(\var{host}, \var{port})}. | 
 | 238 | Depending on the settings of \var{flags}, the result can contain a | 
 | 239 | fully-qualified domain name or numeric address representation in | 
 | 240 | \var{host}.  Similarly, \var{port} can contain a string port name or a | 
 | 241 | numeric port number. | 
 | 242 | \versionadded{2.2} | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 243 | \end{funcdesc} | 
 | 244 |  | 
| Guido van Rossum | 62ac99e | 1996-12-19 16:43:25 +0000 | [diff] [blame] | 245 | \begin{funcdesc}{getprotobyname}{protocolname} | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 246 | 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] | 247 | suitable for passing as the (optional) third argument to the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 248 | \function{socket()} function.  This is usually only needed for sockets | 
 | 249 | opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket | 
 | 250 | modes, the correct protocol is chosen automatically if the protocol is | 
| Guido van Rossum | 62ac99e | 1996-12-19 16:43:25 +0000 | [diff] [blame] | 251 | omitted or zero. | 
 | 252 | \end{funcdesc} | 
 | 253 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 254 | \begin{funcdesc}{getservbyname}{servicename, protocolname} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 255 | Translate an Internet service name and protocol name to a port number | 
 | 256 | for that service.  The protocol name should be \code{'tcp'} or | 
 | 257 | \code{'udp'}. | 
 | 258 | \end{funcdesc} | 
 | 259 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 260 | \begin{funcdesc}{socket}{family, type\optional{, proto}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 261 | Create a new socket using the given address family, socket type and | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 262 | protocol number.  The address family should be \constant{AF_INET}, \constant{AF_INET6} or | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 263 | \constant{AF_UNIX}.  The socket type should be \constant{SOCK_STREAM}, | 
 | 264 | \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] | 265 | The protocol number is usually zero and may be omitted in that case. | 
 | 266 | \end{funcdesc} | 
 | 267 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 268 | \begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 269 | Build a socket object from an existing file descriptor (an integer as | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 270 | returned by a file object's \method{fileno()} method).  Address family, | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 271 | 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] | 272 | above.  The file descriptor should refer to a socket, but this is not | 
 | 273 | checked --- subsequent operations on the object may fail if the file | 
 | 274 | descriptor is invalid.  This function is rarely needed, but can be | 
 | 275 | 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] | 276 | 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] | 277 | daemon). | 
 | 278 | \end{funcdesc} | 
 | 279 |  | 
| Guido van Rossum | bda7ca7 | 1996-12-02 17:24:10 +0000 | [diff] [blame] | 280 | \begin{funcdesc}{ntohl}{x} | 
| Fred Drake | c5aec05 | 1997-12-08 21:25:41 +0000 | [diff] [blame] | 281 | Convert 32-bit integers from network to host byte order.  On machines | 
 | 282 | where the host byte order is the same as network byte order, this is a | 
 | 283 | no-op; otherwise, it performs a 4-byte swap operation. | 
 | 284 | \end{funcdesc} | 
 | 285 |  | 
 | 286 | \begin{funcdesc}{ntohs}{x} | 
 | 287 | Convert 16-bit integers from network to host byte order.  On machines | 
 | 288 | where the host byte order is the same as network byte order, this is a | 
 | 289 | no-op; otherwise, it performs a 2-byte swap operation. | 
 | 290 | \end{funcdesc} | 
 | 291 |  | 
 | 292 | \begin{funcdesc}{htonl}{x} | 
 | 293 | Convert 32-bit integers from host to network byte order.  On machines | 
 | 294 | where the host byte order is the same as network byte order, this is a | 
 | 295 | no-op; otherwise, it performs a 4-byte swap operation. | 
 | 296 | \end{funcdesc} | 
 | 297 |  | 
 | 298 | \begin{funcdesc}{htons}{x} | 
 | 299 | Convert 16-bit integers from host to network byte order.  On machines | 
 | 300 | where the host byte order is the same as network byte order, this is a | 
 | 301 | no-op; otherwise, it performs a 2-byte swap operation. | 
| Guido van Rossum | bda7ca7 | 1996-12-02 17:24:10 +0000 | [diff] [blame] | 302 | \end{funcdesc} | 
 | 303 |  | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 304 | \begin{funcdesc}{inet_aton}{ip_string} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 305 | Convert an IPv4 address from dotted-quad string format | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 306 | (e.g.\ '123.45.67.89') to 32-bit packed binary format, as a string four | 
 | 307 | characters in length. | 
 | 308 |  | 
 | 309 | Useful when conversing with a program that uses the standard C library | 
 | 310 | and needs objects of type \ctype{struct in_addr}, which is the C type | 
 | 311 | for the 32-bit packed binary this function returns. | 
 | 312 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 313 | If the IPv4 address string passed to this function is invalid, | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 314 | \exception{socket.error} will be raised. Note that exactly what is | 
 | 315 | valid depends on the underlying C implementation of | 
 | 316 | \cfunction{inet_aton()}. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 317 |  | 
 | 318 | \function{inet_aton} does not support IPv6, and | 
 | 319 | \function{getnameinfo()} should be used instead for IPv4/v6 dual stack support. | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 320 | \end{funcdesc} | 
 | 321 |  | 
 | 322 | \begin{funcdesc}{inet_ntoa}{packed_ip} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 323 | Convert a 32-bit packed IPv4 address (a string four characters in | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 324 | length) to its standard dotted-quad string representation | 
 | 325 | (e.g. '123.45.67.89'). | 
 | 326 |  | 
 | 327 | Useful when conversing with a program that uses the standard C library | 
 | 328 | and needs objects of type \ctype{struct in_addr}, which is the C type | 
 | 329 | for the 32-bit packed binary this function takes as an argument. | 
 | 330 |  | 
 | 331 | If the string passed to this function is not exactly 4 bytes in | 
 | 332 | length, \exception{socket.error} will be raised. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 333 |  | 
 | 334 | \function{inet_ntoa} does not support IPv6, and | 
 | 335 | \function{getnameinfo()} should be used instead for IPv4/v6 dual stack support. | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 336 | \end{funcdesc} | 
 | 337 |  | 
| Fred Drake | 5451d67 | 1997-10-13 21:31:02 +0000 | [diff] [blame] | 338 | \begin{datadesc}{SocketType} | 
| Guido van Rossum | 2335c5e | 1997-05-21 14:41:42 +0000 | [diff] [blame] | 339 | This is a Python type object that represents the socket object type. | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 340 | It is the same as \code{type(socket(...))}. | 
| Guido van Rossum | 2335c5e | 1997-05-21 14:41:42 +0000 | [diff] [blame] | 341 | \end{datadesc} | 
 | 342 |  | 
| Fred Drake | aa7524c | 2000-07-06 18:37:08 +0000 | [diff] [blame] | 343 |  | 
 | 344 | \begin{seealso} | 
 | 345 |   \seemodule{SocketServer}{Classes that simplify writing network servers.} | 
 | 346 | \end{seealso} | 
 | 347 |  | 
 | 348 |  | 
| Fred Drake | a94f676 | 1999-08-05 13:41:04 +0000 | [diff] [blame] | 349 | \subsection{Socket Objects \label{socket-objects}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 350 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 351 | Socket objects have the following methods.  Except for | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 352 | \method{makefile()} these correspond to \UNIX{} system calls | 
 | 353 | applicable to sockets. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 354 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 355 | \begin{methoddesc}[socket]{accept}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 356 | Accept a connection. | 
 | 357 | The socket must be bound to an address and listening for connections. | 
 | 358 | The return value is a pair \code{(\var{conn}, \var{address})} | 
 | 359 | where \var{conn} is a \emph{new} socket object usable to send and | 
 | 360 | receive data on the connection, and \var{address} is the address bound | 
 | 361 | to the socket on the other end of the connection. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 362 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 363 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 364 | \begin{methoddesc}[socket]{bind}{address} | 
| Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 365 | 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] | 366 | (The format of \var{address} depends on the address family --- see | 
 | 367 | above.)  \strong{Note:}  This method has historically accepted a pair | 
 | 368 | of parameters for \constant{AF_INET} addresses instead of only a | 
| Eric S. Raymond | 8321026 | 2001-01-10 19:34:52 +0000 | [diff] [blame] | 369 | tuple.  This was never intentional and is no longer be available in | 
 | 370 | Python 2.0. | 
| 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]{close}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 374 | Close the socket.  All future operations on the socket object will fail. | 
 | 375 | The remote end will receive no more data (after queued data is flushed). | 
 | 376 | Sockets are automatically closed when they are garbage-collected. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 377 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 378 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 379 | \begin{methoddesc}[socket]{connect}{address} | 
| Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 380 | Connect to a remote socket at \var{address}. | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 381 | (The format of \var{address} depends on the address family --- see | 
| Fred Drake | 7d68690 | 2000-04-04 17:48:30 +0000 | [diff] [blame] | 382 | above.)  \strong{Note:}  This method has historically accepted a pair | 
 | 383 | of parameters for \constant{AF_INET} addresses instead of only a | 
| Eric S. Raymond | 8321026 | 2001-01-10 19:34:52 +0000 | [diff] [blame] | 384 | tuple.  This was never intentional and is no longer available in | 
 | 385 | Python 2.0 and later. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 386 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 387 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 388 | \begin{methoddesc}[socket]{connect_ex}{address} | 
| Guido van Rossum | eefcba6 | 1997-12-09 19:47:24 +0000 | [diff] [blame] | 389 | Like \code{connect(\var{address})}, but return an error indicator | 
| Fred Drake | b0bc7f2 | 1999-05-06 22:03:50 +0000 | [diff] [blame] | 390 | instead of raising an exception for errors returned by the C-level | 
 | 391 | \cfunction{connect()} call (other problems, such as ``host not found,'' | 
 | 392 | can still raise exceptions).  The error indicator is \code{0} if the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 393 | operation succeeded, otherwise the value of the \cdata{errno} | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 394 | variable.  This is useful, e.g., for asynchronous connects. | 
| Fred Drake | 7d68690 | 2000-04-04 17:48:30 +0000 | [diff] [blame] | 395 | \strong{Note:}  This method has historically accepted a pair of | 
 | 396 | parameters for \constant{AF_INET} addresses instead of only a tuple. | 
| Eric S. Raymond | 8321026 | 2001-01-10 19:34:52 +0000 | [diff] [blame] | 397 | This was never intentional and is no longer be available in Python | 
 | 398 | 2.0 and later. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 399 | \end{methoddesc} | 
| Guido van Rossum | f7790c6 | 1997-11-18 15:29:20 +0000 | [diff] [blame] | 400 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 401 | \begin{methoddesc}[socket]{fileno}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 402 | Return the socket's file descriptor (a small integer).  This is useful | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 403 | with \function{select.select()}. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 404 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 405 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 406 | \begin{methoddesc}[socket]{getpeername}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 407 | Return the remote address to which the socket is connected.  This is | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 408 | useful to find out the port number of a remote IPv4/v6 socket, for instance. | 
| Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 409 | (The format of the address returned depends on the address family --- | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 410 | see above.)  On some systems this function is not supported. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 411 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 412 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 413 | \begin{methoddesc}[socket]{getsockname}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 414 | Return the socket's own address.  This is useful to find out the port | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 415 | number of an IPv4/v6 socket, for instance. | 
| Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 416 | (The format of the address returned depends on the address family --- | 
| Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 417 | see above.) | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 418 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 419 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 420 | \begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 421 | 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] | 422 | \manpage{getsockopt}{2}).  The needed symbolic constants | 
 | 423 | (\constant{SO_*} etc.) are defined in this module.  If \var{buflen} | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 424 | is absent, an integer option is assumed and its integer value | 
| Guido van Rossum | 8df3637 | 1995-02-27 17:52:15 +0000 | [diff] [blame] | 425 | is returned by the function.  If \var{buflen} is present, it specifies | 
 | 426 | 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] | 427 | 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] | 428 | the contents of the buffer (see the optional built-in module | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 429 | \refmodule{struct} for a way to decode C structures encoded as strings). | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 430 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 431 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 432 | \begin{methoddesc}[socket]{listen}{backlog} | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 433 | Listen for connections made to the socket.  The \var{backlog} argument | 
 | 434 | specifies the maximum number of queued connections and should be at | 
 | 435 | least 1; the maximum value is system-dependent (usually 5). | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 436 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 437 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 438 | \begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}} | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 439 | Return a \dfn{file object} associated with the socket.  (File objects | 
| Fred Drake | a94f676 | 1999-08-05 13:41:04 +0000 | [diff] [blame] | 440 | are described in \ref{bltin-file-objects}, ``File Objects.'') | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 441 | The file object references a \cfunction{dup()}ped version of the | 
 | 442 | socket file descriptor, so the file object and socket object may be | 
| Fred Drake | a94f676 | 1999-08-05 13:41:04 +0000 | [diff] [blame] | 443 | closed or garbage-collected independently. | 
 | 444 | \index{I/O control!buffering}The optional \var{mode} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 445 | and \var{bufsize} arguments are interpreted the same way as by the | 
 | 446 | built-in \function{open()} function. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 447 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 448 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 449 | \begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 450 | Receive data from the socket.  The return value is a string representing | 
 | 451 | the data received.  The maximum amount of data to be received | 
 | 452 | at once is specified by \var{bufsize}.  See the \UNIX{} manual page | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 453 | \manpage{recv}{2} for the meaning of the optional argument | 
 | 454 | \var{flags}; it defaults to zero. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 455 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 456 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 457 | \begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 458 | Receive data from the socket.  The return value is a pair | 
 | 459 | \code{(\var{string}, \var{address})} where \var{string} is a string | 
 | 460 | 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] | 461 | socket sending the data.  The optional \var{flags} argument has the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 462 | same meaning as for \method{recv()} above. | 
| Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 463 | (The format of \var{address} depends on the address family --- see above.) | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 464 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 465 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 466 | \begin{methoddesc}[socket]{send}{string\optional{, flags}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 467 | 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] | 468 | socket.  The optional \var{flags} argument has the same meaning as for | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 469 | \method{recv()} above.  Returns the number of bytes sent. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 470 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 471 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 472 | \begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 473 | Send data to the socket.  The socket should not be connected to a | 
 | 474 | remote socket, since the destination socket is specified by | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 475 | \var{address}.  The optional \var{flags} argument has the same | 
 | 476 | 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] | 477 | (The format of \var{address} depends on the address family --- see above.) | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 478 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 479 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 480 | \begin{methoddesc}[socket]{setblocking}{flag} | 
| Guido van Rossum | 9195148 | 1994-09-07 14:39:14 +0000 | [diff] [blame] | 481 | Set blocking or non-blocking mode of the socket: if \var{flag} is 0, | 
 | 482 | the socket is set to non-blocking, else to blocking mode.  Initially | 
 | 483 | all sockets are in blocking mode.  In non-blocking mode, if a | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 484 | \method{recv()} call doesn't find any data, or if a | 
 | 485 | \method{send()} call can't immediately dispose of the data, a | 
 | 486 | \exception{error} exception is raised; in blocking mode, the calls | 
 | 487 | block until they can proceed. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 488 | \end{methoddesc} | 
| Guido van Rossum | 9195148 | 1994-09-07 14:39:14 +0000 | [diff] [blame] | 489 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 490 | \begin{methoddesc}[socket]{setsockopt}{level, optname, value} | 
| Fred Drake | 9a748aa | 2000-06-30 04:21:41 +0000 | [diff] [blame] | 491 | 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] | 492 | \manpage{setsockopt}{2}).  The needed symbolic constants are defined in | 
 | 493 | 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] | 494 | integer or a string representing a buffer.  In the latter case it is | 
 | 495 | up to the caller to ensure that the string contains the proper bits | 
 | 496 | (see the optional built-in module | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 497 | \refmodule{struct}\refbimodindex{struct} for a way to encode C | 
 | 498 | structures as strings).  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 499 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 500 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 501 | \begin{methoddesc}[socket]{shutdown}{how} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 502 | Shut down one or both halves of the connection.  If \var{how} is | 
 | 503 | \code{0}, further receives are disallowed.  If \var{how} is \code{1}, | 
 | 504 | further sends are disallowed.  If \var{how} is \code{2}, further sends | 
 | 505 | and receives are disallowed. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 506 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 507 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 508 | Note that there are no methods \method{read()} or \method{write()}; | 
 | 509 | use \method{recv()} and \method{send()} without \var{flags} argument | 
 | 510 | instead. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 511 |  | 
| Fred Drake | aa7524c | 2000-07-06 18:37:08 +0000 | [diff] [blame] | 512 |  | 
 | 513 | \subsection{Example \label{socket-example}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 514 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 515 | Here are four minimal example programs using the TCP/IP protocol:\ a | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 516 | server that echoes all data that it receives back (servicing only one | 
 | 517 | 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] | 518 | sequence \function{socket()}, \method{bind()}, \method{listen()}, | 
 | 519 | \method{accept()} (possibly repeating the \method{accept()} to service | 
 | 520 | more than one client), while a client only needs the sequence | 
 | 521 | \function{socket()}, \method{connect()}.  Also note that the server | 
 | 522 | does not \method{send()}/\method{recv()} on the  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 523 | socket it is listening on but on the new socket returned by | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 524 | \method{accept()}. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 525 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 526 | The first two examples support IPv4 only. | 
 | 527 |  | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 528 | \begin{verbatim} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 529 | # Echo server program | 
| Fred Drake | ef52f60 | 2000-10-10 20:36:29 +0000 | [diff] [blame] | 530 | import socket | 
 | 531 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 532 | HOST = ''                 # Symbolic name meaning the local host | 
| Fred Drake | ef52f60 | 2000-10-10 20:36:29 +0000 | [diff] [blame] | 533 | PORT = 50007              # Arbitrary non-privileged port | 
 | 534 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 
| Fred Drake | 3d69c0e | 2000-05-03 19:40:32 +0000 | [diff] [blame] | 535 | s.bind((HOST, PORT)) | 
| Guido van Rossum | 5da5755 | 1994-03-02 10:52:16 +0000 | [diff] [blame] | 536 | s.listen(1) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 537 | conn, addr = s.accept() | 
 | 538 | print 'Connected by', addr | 
 | 539 | while 1: | 
 | 540 |     data = conn.recv(1024) | 
 | 541 |     if not data: break | 
 | 542 |     conn.send(data) | 
 | 543 | conn.close() | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 544 | \end{verbatim} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 545 |  | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 546 | \begin{verbatim} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 547 | # Echo client program | 
| Fred Drake | ef52f60 | 2000-10-10 20:36:29 +0000 | [diff] [blame] | 548 | import socket | 
 | 549 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 550 | HOST = 'daring.cwi.nl'    # The remote host | 
 | 551 | PORT = 50007              # The same port as used by the server | 
| Fred Drake | ef52f60 | 2000-10-10 20:36:29 +0000 | [diff] [blame] | 552 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 
| Fred Drake | 3d69c0e | 2000-05-03 19:40:32 +0000 | [diff] [blame] | 553 | s.connect((HOST, PORT)) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 554 | s.send('Hello, world') | 
 | 555 | data = s.recv(1024) | 
 | 556 | s.close() | 
 | 557 | print 'Received', `data` | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 558 | \end{verbatim} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame^] | 559 |  | 
 | 560 | The next two examples are identical to the above two, but support both | 
 | 561 | IPv4 and IPv6. | 
 | 562 | The server side will listen to the first address family available | 
 | 563 | (it should listen to both instead). | 
 | 564 | On most of IPv6-ready systems, IPv6 will take precedence | 
 | 565 | and the server may not accept IPv4 traffic. | 
 | 566 | The client side will try to connect to the all addresses returned as a result | 
 | 567 | of the name resolution, and sends traffic to the first one connected | 
 | 568 | successfully. | 
 | 569 |  | 
 | 570 | \begin{verbatim} | 
 | 571 | # Echo server program | 
 | 572 | import socket | 
 | 573 | import sys | 
 | 574 |  | 
 | 575 | HOST = ''                 # Symbolic name meaning the local host | 
 | 576 | PORT = 50007              # Arbitrary non-privileged port | 
 | 577 | s = None | 
 | 578 | for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): | 
 | 579 |     af, socktype, proto, canonname, sa = res | 
 | 580 |     try: | 
 | 581 | 	s = socket.socket(af, socktype, proto) | 
 | 582 |     except socket.error, msg: | 
 | 583 | 	s = None | 
 | 584 | 	continue | 
 | 585 |     try: | 
 | 586 | 	s.bind(sa) | 
 | 587 | 	s.listen(1) | 
 | 588 |     except socket.error, msg: | 
 | 589 | 	s.close() | 
 | 590 | 	s = None | 
 | 591 | 	continue | 
 | 592 |     break | 
 | 593 | if s is None: | 
 | 594 |     print 'could not open socket' | 
 | 595 |     sys.exit(1) | 
 | 596 | conn, addr = s.accept() | 
 | 597 | print 'Connected by', addr | 
 | 598 | while 1: | 
 | 599 |     data = conn.recv(1024) | 
 | 600 |     if not data: break | 
 | 601 |     conn.send(data) | 
 | 602 | conn.close() | 
 | 603 | \end{verbatim} | 
 | 604 |  | 
 | 605 | \begin{verbatim} | 
 | 606 | # Echo client program | 
 | 607 | import socket | 
 | 608 | import sys | 
 | 609 |  | 
 | 610 | HOST = 'daring.cwi.nl'    # The remote host | 
 | 611 | PORT = 50007              # The same port as used by the server | 
 | 612 | s = None | 
 | 613 | for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM): | 
 | 614 |     af, socktype, proto, canonname, sa = res | 
 | 615 |     try: | 
 | 616 | 	s = socket.socket(af, socktype, proto) | 
 | 617 |     except socket.error, msg: | 
 | 618 | 	s = None | 
 | 619 | 	continue | 
 | 620 |     try: | 
 | 621 | 	s.connect(sa) | 
 | 622 |     except socket.error, msg: | 
 | 623 | 	s.close() | 
 | 624 | 	s = None | 
 | 625 | 	continue | 
 | 626 |     break | 
 | 627 | if s is None: | 
 | 628 |     print 'could not open socket' | 
 | 629 |     sys.exit(1) | 
 | 630 | s.send('Hello, world') | 
 | 631 | data = s.recv(1024) | 
 | 632 | s.close() | 
 | 633 | print 'Received', `data` | 
 | 634 | \end{verbatim} |