| 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, | 
| Neal Norwitz | d157b1d | 2005-10-03 00:44:06 +0000 | [diff] [blame] | 10 | OS/2, and probably additional platforms.  \note{Some behavior may be | 
|  | 11 | platform dependent, since calls are made to the operating system socket APIs.} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 12 |  | 
|  | 13 | For an introduction to socket programming (in C), see the following | 
| Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 14 | papers: \citetitle{An Introductory 4.3BSD Interprocess Communication | 
|  | 15 | Tutorial}, by Stuart Sechrest and \citetitle{An Advanced 4.3BSD | 
|  | 16 | Interprocess Communication Tutorial}, by Samuel J.  Leffler et al, | 
|  | 17 | both in the \citetitle{\UNIX{} Programmer's Manual, Supplementary Documents 1} | 
| Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 18 | (sections PS1:7 and PS1:8).  The platform-specific reference material | 
|  | 19 | for the various socket-related system calls are also a valuable source | 
|  | 20 | of information on the details of socket semantics.  For \UNIX, refer | 
|  | 21 | to the manual pages; for Windows, see the WinSock (or Winsock 2) | 
|  | 22 | specification. | 
| Fred Drake | 3fc291a | 2001-09-27 04:17:20 +0000 | [diff] [blame] | 23 | For IPv6-ready APIs, readers may want to refer to \rfc{2553} titled | 
|  | 24 | \citetitle{Basic Socket Interface Extensions for IPv6}. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 25 |  | 
|  | 26 | The Python interface is a straightforward transliteration of the | 
|  | 27 | \UNIX{} system call and library interface for sockets to Python's | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 28 | object-oriented style: the \function{socket()} function returns a | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 29 | \dfn{socket object}\obindex{socket} whose methods implement the | 
|  | 30 | various socket system calls.  Parameter types are somewhat | 
|  | 31 | higher-level than in the C interface: as with \method{read()} and | 
|  | 32 | \method{write()} operations on Python files, buffer allocation on | 
|  | 33 | receive operations is automatic, and buffer length is implicit on send | 
|  | 34 | operations. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 35 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 36 | Socket addresses are represented as follows: | 
|  | 37 | A single string is used for the \constant{AF_UNIX} address family. | 
|  | 38 | A pair \code{(\var{host}, \var{port})} is used for the | 
|  | 39 | \constant{AF_INET} address family, where \var{host} is a string | 
|  | 40 | representing either a hostname in Internet domain notation like | 
|  | 41 | \code{'daring.cwi.nl'} or an IPv4 address like \code{'100.50.200.5'}, | 
|  | 42 | and \var{port} is an integral port number. | 
|  | 43 | For \constant{AF_INET6} address family, a four-tuple | 
|  | 44 | \code{(\var{host}, \var{port}, \var{flowinfo}, \var{scopeid})} is | 
|  | 45 | used, where \var{flowinfo} and \var{scopeid} represents | 
|  | 46 | \code{sin6_flowinfo} and \code{sin6_scope_id} member in | 
|  | 47 | \constant{struct sockaddr_in6} in C. | 
|  | 48 | For \module{socket} module methods, \var{flowinfo} and \var{scopeid} | 
|  | 49 | can be omitted just for backward compatibility. Note, however, | 
|  | 50 | omission of \var{scopeid} can cause problems in manipulating scoped | 
|  | 51 | IPv6 addresses. Other address families are currently not supported. | 
|  | 52 | The address format required by a particular socket object is | 
|  | 53 | automatically selected based on the address family specified when the | 
|  | 54 | socket object was created. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 55 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 56 | For IPv4 addresses, two special forms are accepted instead of a host | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 57 | address: the empty string represents \constant{INADDR_ANY}, and the string | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 58 | \code{'<broadcast>'} represents \constant{INADDR_BROADCAST}. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 59 | The behavior is not available for IPv6 for backward compatibility, | 
|  | 60 | therefore, you may want to avoid these if you intend to support IPv6 with | 
|  | 61 | your Python programs. | 
|  | 62 |  | 
|  | 63 | If you use a hostname in the \var{host} portion of IPv4/v6 socket | 
|  | 64 | address, the program may show a nondeterministic behavior, as Python | 
|  | 65 | uses the first address returned from the DNS resolution.  The socket | 
|  | 66 | address will be resolved differently into an actual IPv4/v6 address, | 
|  | 67 | depending on the results from DNS resolution and/or the host | 
|  | 68 | configuration.  For deterministic behavior use a numeric address in | 
|  | 69 | \var{host} portion. | 
| Guido van Rossum | e4f347e | 1997-05-09 02:21:51 +0000 | [diff] [blame] | 70 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 71 | All errors raise exceptions.  The normal exceptions for invalid | 
|  | 72 | argument types and out-of-memory conditions can be raised; errors | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 73 | related to socket or address semantics raise the error | 
|  | 74 | \exception{socket.error}. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 75 |  | 
| Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 76 | Non-blocking mode is supported through | 
|  | 77 | \method{setblocking()}.  A generalization of this based on timeouts | 
|  | 78 | is supported through \method{settimeout()}. | 
| 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 | The module \module{socket} exports the following constants and functions: | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 81 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 82 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 83 | \begin{excdesc}{error} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 84 | This exception is raised for socket-related errors. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 85 | The accompanying value is either a string telling what went wrong or a | 
|  | 86 | pair \code{(\var{errno}, \var{string})} | 
|  | 87 | representing an error returned by a system | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 88 | call, similar to the value accompanying \exception{os.error}. | 
|  | 89 | See the module \refmodule{errno}\refbimodindex{errno}, which contains | 
| Guido van Rossum | 8e1e68d | 1998-02-06 15:18:25 +0000 | [diff] [blame] | 90 | names for the error codes defined by the underlying operating system. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 91 | \end{excdesc} | 
|  | 92 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 93 | \begin{excdesc}{herror} | 
|  | 94 | This exception is raised for address-related errors, i.e. for | 
| Fred Drake | 87fa3aa | 2001-12-21 17:45:03 +0000 | [diff] [blame] | 95 | functions that use \var{h_errno} in the C API, including | 
|  | 96 | \function{gethostbyname_ex()} and \function{gethostbyaddr()}. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 97 |  | 
|  | 98 | The accompanying value is a pair \code{(\var{h_errno}, \var{string})} | 
|  | 99 | representing an error returned by a library call. \var{string} | 
|  | 100 | represents the description of \var{h_errno}, as returned by | 
| Fred Drake | 87fa3aa | 2001-12-21 17:45:03 +0000 | [diff] [blame] | 101 | the \cfunction{hstrerror()} C function. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 102 | \end{excdesc} | 
|  | 103 |  | 
|  | 104 | \begin{excdesc}{gaierror} | 
|  | 105 | This exception is raised for address-related errors, for | 
| Fred Drake | 87fa3aa | 2001-12-21 17:45:03 +0000 | [diff] [blame] | 106 | \function{getaddrinfo()} and \function{getnameinfo()}. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 107 | The accompanying value is a pair \code{(\var{error}, \var{string})} | 
|  | 108 | representing an error returned by a library call. | 
|  | 109 | \var{string} represents the description of \var{error}, as returned | 
| Fred Drake | 87fa3aa | 2001-12-21 17:45:03 +0000 | [diff] [blame] | 110 | by the \cfunction{gai_strerror()} C function. | 
| Fred Drake | cee8879 | 2004-05-05 04:18:11 +0000 | [diff] [blame] | 111 | The \var{error} value will match one of the \constant{EAI_*} constants | 
|  | 112 | defined in this module. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 113 | \end{excdesc} | 
|  | 114 |  | 
| Raymond Hettinger | be2528d | 2003-06-29 04:55:59 +0000 | [diff] [blame] | 115 | \begin{excdesc}{timeout} | 
|  | 116 | This exception is raised when a timeout occurs on a socket which has | 
|  | 117 | had timeouts enabled via a prior call to \method{settimeout()}.  The | 
|  | 118 | accompanying value is a string whose value is currently always ``timed | 
|  | 119 | out''. | 
|  | 120 | \versionadded{2.3} | 
|  | 121 | \end{excdesc} | 
|  | 122 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 123 | \begin{datadesc}{AF_UNIX} | 
|  | 124 | \dataline{AF_INET} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 125 | \dataline{AF_INET6} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 126 | These constants represent the address (and protocol) families, | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 127 | used for the first argument to \function{socket()}.  If the | 
|  | 128 | \constant{AF_UNIX} constant is not defined then this protocol is | 
|  | 129 | unsupported. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 130 | \end{datadesc} | 
|  | 131 |  | 
|  | 132 | \begin{datadesc}{SOCK_STREAM} | 
|  | 133 | \dataline{SOCK_DGRAM} | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 134 | \dataline{SOCK_RAW} | 
|  | 135 | \dataline{SOCK_RDM} | 
|  | 136 | \dataline{SOCK_SEQPACKET} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 137 | These constants represent the socket types, | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 138 | used for the second argument to \function{socket()}. | 
|  | 139 | (Only \constant{SOCK_STREAM} and | 
|  | 140 | \constant{SOCK_DGRAM} appear to be generally useful.) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 141 | \end{datadesc} | 
|  | 142 |  | 
| Guido van Rossum | ed2bad8 | 1995-02-16 16:29:18 +0000 | [diff] [blame] | 143 | \begin{datadesc}{SO_*} | 
|  | 144 | \dataline{SOMAXCONN} | 
|  | 145 | \dataline{MSG_*} | 
|  | 146 | \dataline{SOL_*} | 
|  | 147 | \dataline{IPPROTO_*} | 
|  | 148 | \dataline{IPPORT_*} | 
|  | 149 | \dataline{INADDR_*} | 
|  | 150 | \dataline{IP_*} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 151 | \dataline{IPV6_*} | 
|  | 152 | \dataline{EAI_*} | 
|  | 153 | \dataline{AI_*} | 
|  | 154 | \dataline{NI_*} | 
| Fred Drake | 39960f6 | 2001-12-22 19:07:58 +0000 | [diff] [blame] | 155 | \dataline{TCP_*} | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 156 | Many constants of these forms, documented in the \UNIX{} documentation on | 
| Guido van Rossum | ed2bad8 | 1995-02-16 16:29:18 +0000 | [diff] [blame] | 157 | 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] | 158 | They are generally used in arguments to the \method{setsockopt()} and | 
|  | 159 | \method{getsockopt()} methods of socket objects.  In most cases, only | 
| Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 160 | 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] | 161 | for a few symbols, default values are provided. | 
|  | 162 | \end{datadesc} | 
|  | 163 |  | 
| Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 164 | \begin{datadesc}{has_ipv6} | 
|  | 165 | This constant contains a boolean value which indicates if IPv6 is | 
|  | 166 | supported on this platform. | 
| Neal Norwitz | 6eb502f | 2003-04-25 14:53:48 +0000 | [diff] [blame] | 167 | \versionadded{2.3} | 
| Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 168 | \end{datadesc} | 
|  | 169 |  | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 170 | \begin{funcdesc}{getaddrinfo}{host, port\optional{, family\optional{, | 
|  | 171 | socktype\optional{, proto\optional{, | 
|  | 172 | flags}}}}} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 173 | Resolves the \var{host}/\var{port} argument, into a sequence of | 
|  | 174 | 5-tuples that contain all the necessary argument for the sockets | 
|  | 175 | manipulation. \var{host} is a domain name, a string representation of | 
|  | 176 | IPv4/v6 address or \code{None}. | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 177 | \var{port} is a string service name (like \code{'http'}), a numeric | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 178 | port number or \code{None}. | 
|  | 179 |  | 
|  | 180 | The rest of the arguments are optional and must be numeric if | 
|  | 181 | specified.  For \var{host} and \var{port}, by passing either an empty | 
|  | 182 | string or \code{None}, you can pass \code{NULL} to the C API.  The | 
|  | 183 | \function{getaddrinfo()} function returns a list of 5-tuples with | 
|  | 184 | the following structure: | 
|  | 185 |  | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 186 | \code{(\var{family}, \var{socktype}, \var{proto}, \var{canonname}, | 
|  | 187 | \var{sockaddr})} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 188 |  | 
|  | 189 | \var{family}, \var{socktype}, \var{proto} are all integer and are meant to | 
|  | 190 | be passed to the \function{socket()} function. | 
|  | 191 | \var{canonname} is a string representing the canonical name of the \var{host}. | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 192 | It can be a numeric IPv4/v6 address when \constant{AI_CANONNAME} is specified | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 193 | for a numeric \var{host}. | 
|  | 194 | \var{sockaddr} is a tuple describing a socket address, as described above. | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 195 | See the source for the \refmodule{httplib} and other library modules | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 196 | for a typical usage of the function. | 
|  | 197 | \versionadded{2.2} | 
|  | 198 | \end{funcdesc} | 
|  | 199 |  | 
| Fred Drake | 5772c86 | 2000-08-16 14:21:42 +0000 | [diff] [blame] | 200 | \begin{funcdesc}{getfqdn}{\optional{name}} | 
|  | 201 | Return a fully qualified domain name for \var{name}. | 
|  | 202 | If \var{name} is omitted or empty, it is interpreted as the local | 
|  | 203 | host.  To find the fully qualified name, the hostname returned by | 
|  | 204 | \function{gethostbyaddr()} is checked, then aliases for the host, if | 
|  | 205 | available.  The first name which includes a period is selected.  In | 
| Brett Cannon | 01668a1 | 2005-03-11 00:04:17 +0000 | [diff] [blame] | 206 | case no fully qualified domain name is available, the hostname as | 
|  | 207 | returned by \function{gethostname()} is returned. | 
| Fred Drake | 8b2e8f8 | 2000-09-06 02:22:16 +0000 | [diff] [blame] | 208 | \versionadded{2.0} | 
| Fred Drake | 5772c86 | 2000-08-16 14:21:42 +0000 | [diff] [blame] | 209 | \end{funcdesc} | 
|  | 210 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 211 | \begin{funcdesc}{gethostbyname}{hostname} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 212 | Translate a host name to IPv4 address format.  The IPv4 address is | 
| Fred Drake | 87fa3aa | 2001-12-21 17:45:03 +0000 | [diff] [blame] | 213 | returned as a string, such as  \code{'100.50.200.5'}.  If the host name | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 214 | is an IPv4 address itself it is returned unchanged.  See | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 215 | \function{gethostbyname_ex()} for a more complete interface. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 216 | \function{gethostbyname()} does not support IPv6 name resolution, and | 
|  | 217 | \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] | 218 | \end{funcdesc} | 
|  | 219 |  | 
|  | 220 | \begin{funcdesc}{gethostbyname_ex}{hostname} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 221 | Translate a host name to IPv4 address format, extended interface. | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 222 | Return a triple \code{(\var{hostname}, \var{aliaslist}, | 
|  | 223 | \var{ipaddrlist})} where | 
|  | 224 | \var{hostname} is the primary host name responding to the given | 
|  | 225 | \var{ip_address}, \var{aliaslist} is a (possibly empty) list of | 
|  | 226 | alternative host names for the same address, and \var{ipaddrlist} is | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 227 | 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] | 228 | host (often but not always a single address). | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 229 | \function{gethostbyname_ex()} does not support IPv6 name resolution, and | 
|  | 230 | \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] | 231 | \end{funcdesc} | 
|  | 232 |  | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 233 | \begin{funcdesc}{gethostname}{} | 
| Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 234 | Return a string containing the hostname of the machine where | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 235 | the Python interpreter is currently executing. | 
|  | 236 | If you want to know the current machine's IP address, you may want to use | 
|  | 237 | \code{gethostbyname(gethostname())}. | 
|  | 238 | This operation assumes that there is a valid address-to-host mapping for | 
|  | 239 | the host, and the assumption does not always hold. | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 240 | Note: \function{gethostname()} doesn't always return the fully qualified | 
|  | 241 | domain name; use \code{gethostbyaddr(gethostname())} | 
| Guido van Rossum | fe27a50 | 1997-01-11 17:04:56 +0000 | [diff] [blame] | 242 | (see below). | 
| Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 243 | \end{funcdesc} | 
|  | 244 |  | 
|  | 245 | \begin{funcdesc}{gethostbyaddr}{ip_address} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 246 | Return a triple \code{(\var{hostname}, \var{aliaslist}, | 
|  | 247 | \var{ipaddrlist})} where \var{hostname} is the primary host name | 
|  | 248 | responding to the given \var{ip_address}, \var{aliaslist} is a | 
|  | 249 | (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] | 250 | 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] | 251 | on the same host (most likely containing only a single address). | 
| Fred Drake | 5772c86 | 2000-08-16 14:21:42 +0000 | [diff] [blame] | 252 | To find the fully qualified domain name, use the function | 
|  | 253 | \function{getfqdn()}. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 254 | \function{gethostbyaddr} supports both IPv4 and IPv6. | 
|  | 255 | \end{funcdesc} | 
|  | 256 |  | 
|  | 257 | \begin{funcdesc}{getnameinfo}{sockaddr, flags} | 
|  | 258 | Translate a socket address \var{sockaddr} into a 2-tuple | 
|  | 259 | \code{(\var{host}, \var{port})}. | 
|  | 260 | Depending on the settings of \var{flags}, the result can contain a | 
|  | 261 | fully-qualified domain name or numeric address representation in | 
|  | 262 | \var{host}.  Similarly, \var{port} can contain a string port name or a | 
|  | 263 | numeric port number. | 
|  | 264 | \versionadded{2.2} | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 265 | \end{funcdesc} | 
|  | 266 |  | 
| Guido van Rossum | 62ac99e | 1996-12-19 16:43:25 +0000 | [diff] [blame] | 267 | \begin{funcdesc}{getprotobyname}{protocolname} | 
| Fred Drake | 87fa3aa | 2001-12-21 17:45:03 +0000 | [diff] [blame] | 268 | Translate an Internet protocol name (for example, \code{'icmp'}) to a constant | 
| Guido van Rossum | 62ac99e | 1996-12-19 16:43:25 +0000 | [diff] [blame] | 269 | suitable for passing as the (optional) third argument to the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 270 | \function{socket()} function.  This is usually only needed for sockets | 
|  | 271 | opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket | 
|  | 272 | modes, the correct protocol is chosen automatically if the protocol is | 
| Guido van Rossum | 62ac99e | 1996-12-19 16:43:25 +0000 | [diff] [blame] | 273 | omitted or zero. | 
|  | 274 | \end{funcdesc} | 
|  | 275 |  | 
| Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 276 | \begin{funcdesc}{getservbyname}{servicename\optional{, protocolname}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 277 | Translate an Internet service name and protocol name to a port number | 
| Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 278 | for that service.  The optional protocol name, if given, should be | 
|  | 279 | \code{'tcp'} or \code{'udp'}, otherwise any protocol will match. | 
|  | 280 | \end{funcdesc} | 
|  | 281 |  | 
|  | 282 | \begin{funcdesc}{getservbyport}{port\optional{, protocolname}} | 
|  | 283 | Translate an Internet port number and protocol name to a service name | 
|  | 284 | for that service.  The optional protocol name, if given, should be | 
|  | 285 | \code{'tcp'} or \code{'udp'}, otherwise any protocol will match. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 286 | \end{funcdesc} | 
|  | 287 |  | 
| Fred Drake | fcc5176 | 2004-01-27 18:21:26 +0000 | [diff] [blame] | 288 | \begin{funcdesc}{socket}{\optional{family\optional{, | 
|  | 289 | type\optional{, proto}}}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 290 | Create a new socket using the given address family, socket type and | 
| Fred Drake | fcc5176 | 2004-01-27 18:21:26 +0000 | [diff] [blame] | 291 | protocol number.  The address family should be \constant{AF_INET} (the | 
|  | 292 | default), \constant{AF_INET6} or \constant{AF_UNIX}.  The socket type | 
|  | 293 | should be \constant{SOCK_STREAM} (the default), \constant{SOCK_DGRAM} | 
|  | 294 | or perhaps one of the other \samp{SOCK_} constants.  The protocol | 
|  | 295 | number is usually zero and may be omitted in that case. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 296 | \end{funcdesc} | 
|  | 297 |  | 
| Jeremy Hylton | cb43c08 | 2001-10-11 16:17:22 +0000 | [diff] [blame] | 298 | \begin{funcdesc}{ssl}{sock\optional{, keyfile, certfile}} | 
| Fred Drake | 9081bb1 | 2001-09-25 15:48:11 +0000 | [diff] [blame] | 299 | Initiate a SSL connection over the socket \var{sock}. \var{keyfile} is | 
|  | 300 | the name of a PEM formatted file that contains your private | 
|  | 301 | key. \var{certfile} is a PEM formatted certificate chain file. On | 
|  | 302 | success, a new \class{SSLObject} is returned. | 
|  | 303 |  | 
| Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 304 | \warning{This does not do any certificate verification!} | 
| Fred Drake | 9081bb1 | 2001-09-25 15:48:11 +0000 | [diff] [blame] | 305 | \end{funcdesc} | 
|  | 306 |  | 
| Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 307 | \begin{funcdesc}{socketpair}{\optional{family\optional{, type\optional{, proto}}}} | 
|  | 308 | Build a pair of connected socket objects using the given address | 
| Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 309 | family, socket type, and protocol number.  Address family, socket type, | 
| Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 310 | and protocol number are as for the \function{socket()} function above. | 
| Dave Cole | e8bbfe4 | 2004-08-26 00:51:16 +0000 | [diff] [blame] | 311 | The default family is \constant{AF_UNIX} if defined on the platform; | 
|  | 312 | otherwise, the default is \constant{AF_INET}. | 
| Dave Cole | 07fda7e | 2004-08-23 05:16:23 +0000 | [diff] [blame] | 313 | Availability: \UNIX.  \versionadded{2.4} | 
| Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 314 | \end{funcdesc} | 
|  | 315 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 316 | \begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 317 | Build a socket object from an existing file descriptor (an integer as | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 318 | returned by a file object's \method{fileno()} method).  Address family, | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 319 | 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] | 320 | above.  The file descriptor should refer to a socket, but this is not | 
|  | 321 | checked --- subsequent operations on the object may fail if the file | 
|  | 322 | descriptor is invalid.  This function is rarely needed, but can be | 
|  | 323 | used to get or set socket options on a socket passed to a program as | 
| Fred Drake | 87fa3aa | 2001-12-21 17:45:03 +0000 | [diff] [blame] | 324 | standard input or output (such as a server started by the \UNIX{} inet | 
| Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 325 | daemon).  The socket is assumed to be in blocking mode. | 
| Fred Drake | 87fa3aa | 2001-12-21 17:45:03 +0000 | [diff] [blame] | 326 | Availability: \UNIX. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 327 | \end{funcdesc} | 
|  | 328 |  | 
| Guido van Rossum | bda7ca7 | 1996-12-02 17:24:10 +0000 | [diff] [blame] | 329 | \begin{funcdesc}{ntohl}{x} | 
| Fred Drake | c5aec05 | 1997-12-08 21:25:41 +0000 | [diff] [blame] | 330 | Convert 32-bit integers from network to host byte order.  On machines | 
|  | 331 | where the host byte order is the same as network byte order, this is a | 
|  | 332 | no-op; otherwise, it performs a 4-byte swap operation. | 
|  | 333 | \end{funcdesc} | 
|  | 334 |  | 
|  | 335 | \begin{funcdesc}{ntohs}{x} | 
|  | 336 | Convert 16-bit integers from network to host byte order.  On machines | 
|  | 337 | where the host byte order is the same as network byte order, this is a | 
|  | 338 | no-op; otherwise, it performs a 2-byte swap operation. | 
|  | 339 | \end{funcdesc} | 
|  | 340 |  | 
|  | 341 | \begin{funcdesc}{htonl}{x} | 
|  | 342 | Convert 32-bit integers from host to network byte order.  On machines | 
|  | 343 | where the host byte order is the same as network byte order, this is a | 
|  | 344 | no-op; otherwise, it performs a 4-byte swap operation. | 
|  | 345 | \end{funcdesc} | 
|  | 346 |  | 
|  | 347 | \begin{funcdesc}{htons}{x} | 
|  | 348 | Convert 16-bit integers from host to network byte order.  On machines | 
|  | 349 | where the host byte order is the same as network byte order, this is a | 
|  | 350 | no-op; otherwise, it performs a 2-byte swap operation. | 
| Guido van Rossum | bda7ca7 | 1996-12-02 17:24:10 +0000 | [diff] [blame] | 351 | \end{funcdesc} | 
|  | 352 |  | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 353 | \begin{funcdesc}{inet_aton}{ip_string} | 
| Fred Drake | 87fa3aa | 2001-12-21 17:45:03 +0000 | [diff] [blame] | 354 | Convert an IPv4 address from dotted-quad string format (for example, | 
|  | 355 | '123.45.67.89') to 32-bit packed binary format, as a string four | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 356 | characters in length.  This is useful when conversing with a program | 
|  | 357 | that uses the standard C library and needs objects of type | 
|  | 358 | \ctype{struct in_addr}, which is the C type for the 32-bit packed | 
|  | 359 | binary this function returns. | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 360 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 361 | If the IPv4 address string passed to this function is invalid, | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 362 | \exception{socket.error} will be raised. Note that exactly what is | 
|  | 363 | valid depends on the underlying C implementation of | 
|  | 364 | \cfunction{inet_aton()}. | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 365 |  | 
| Fred Drake | 39960f6 | 2001-12-22 19:07:58 +0000 | [diff] [blame] | 366 | \function{inet_aton()} does not support IPv6, and | 
|  | 367 | \function{getnameinfo()} should be used instead for IPv4/v6 dual stack | 
|  | 368 | support. | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 369 | \end{funcdesc} | 
|  | 370 |  | 
|  | 371 | \begin{funcdesc}{inet_ntoa}{packed_ip} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 372 | Convert a 32-bit packed IPv4 address (a string four characters in | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 373 | length) to its standard dotted-quad string representation (for | 
|  | 374 | example, '123.45.67.89').  This is useful when conversing with a | 
|  | 375 | program that uses the standard C library and needs objects of type | 
|  | 376 | \ctype{struct in_addr}, which is the C type for the 32-bit packed | 
|  | 377 | binary data this function takes as an argument. | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 378 |  | 
|  | 379 | If the string passed to this function is not exactly 4 bytes in | 
|  | 380 | length, \exception{socket.error} will be raised. | 
| Fred Drake | 39960f6 | 2001-12-22 19:07:58 +0000 | [diff] [blame] | 381 | \function{inet_ntoa()} does not support IPv6, and | 
|  | 382 | \function{getnameinfo()} should be used instead for IPv4/v6 dual stack | 
|  | 383 | support. | 
| Fred Drake | e6fb1c4 | 1999-09-16 15:50:00 +0000 | [diff] [blame] | 384 | \end{funcdesc} | 
|  | 385 |  | 
| Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 386 | \begin{funcdesc}{inet_pton}{address_family, ip_string} | 
|  | 387 | Convert an IP address from its family-specific string format to a packed, | 
|  | 388 | binary format. | 
| Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 389 | \function{inet_pton()} is useful when a library or network protocol calls for | 
|  | 390 | an object of type \ctype{struct in_addr} (similar to \function{inet_aton()}) | 
|  | 391 | or \ctype{struct in6_addr}. | 
|  | 392 |  | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 393 | Supported values for \var{address_family} are currently | 
|  | 394 | \constant{AF_INET} and \constant{AF_INET6}. | 
|  | 395 | If the IP address string \var{ip_string} is invalid, | 
| Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 396 | \exception{socket.error} will be raised. Note that exactly what is valid | 
|  | 397 | depends on both the value of \var{address_family} and the underlying | 
|  | 398 | implementation of \cfunction{inet_pton()}. | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 399 |  | 
|  | 400 | Availability: \UNIX{} (maybe not all platforms). | 
| Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 401 | \versionadded{2.3} | 
|  | 402 | \end{funcdesc} | 
|  | 403 |  | 
|  | 404 | \begin{funcdesc}{inet_ntop}{address_family, packed_ip} | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 405 | Convert a packed IP address (a string of some number of characters) to | 
|  | 406 | its standard, family-specific string representation (for example, | 
|  | 407 | \code{'7.10.0.5'} or \code{'5aef:2b::8'}) | 
| Guido van Rossum | b016752 | 2003-04-25 15:26:58 +0000 | [diff] [blame] | 408 | \function{inet_ntop()} is useful when a library or network protocol returns | 
|  | 409 | an object of type \ctype{struct in_addr} (similar to \function{inet_ntoa()}) | 
| Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 410 | or \ctype{struct in6_addr}. | 
|  | 411 |  | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 412 | Supported values for \var{address_family} are currently | 
|  | 413 | \constant{AF_INET} and \constant{AF_INET6}. | 
|  | 414 | If the string \var{packed_ip} is not the correct length for the | 
|  | 415 | specified address family, \exception{ValueError} will be raised.  A | 
|  | 416 | \exception{socket.error} is raised for errors from the call to | 
| Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 417 | \function{inet_ntop()}. | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 418 |  | 
|  | 419 | Availability: \UNIX{} (maybe not all platforms). | 
| Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 420 | \versionadded{2.3} | 
|  | 421 | \end{funcdesc} | 
|  | 422 |  | 
| Skip Montanaro | 2a403e8 | 2003-03-20 17:58:12 +0000 | [diff] [blame] | 423 | \begin{funcdesc}{getdefaulttimeout}{} | 
|  | 424 | Return the default timeout in floating seconds for new socket objects. | 
|  | 425 | A value of \code{None} indicates that new socket objects have no timeout. | 
|  | 426 | When the socket module is first imported, the default is \code{None}. | 
|  | 427 | \versionadded{2.3} | 
|  | 428 | \end{funcdesc} | 
|  | 429 |  | 
|  | 430 | \begin{funcdesc}{setdefaulttimeout}{timeout} | 
|  | 431 | Set the default timeout in floating seconds for new socket objects. | 
|  | 432 | A value of \code{None} indicates that new socket objects have no timeout. | 
|  | 433 | When the socket module is first imported, the default is \code{None}. | 
|  | 434 | \versionadded{2.3} | 
|  | 435 | \end{funcdesc} | 
|  | 436 |  | 
| Fred Drake | 5451d67 | 1997-10-13 21:31:02 +0000 | [diff] [blame] | 437 | \begin{datadesc}{SocketType} | 
| Guido van Rossum | 2335c5e | 1997-05-21 14:41:42 +0000 | [diff] [blame] | 438 | This is a Python type object that represents the socket object type. | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 439 | It is the same as \code{type(socket(...))}. | 
| Guido van Rossum | 2335c5e | 1997-05-21 14:41:42 +0000 | [diff] [blame] | 440 | \end{datadesc} | 
|  | 441 |  | 
| Fred Drake | aa7524c | 2000-07-06 18:37:08 +0000 | [diff] [blame] | 442 |  | 
|  | 443 | \begin{seealso} | 
|  | 444 | \seemodule{SocketServer}{Classes that simplify writing network servers.} | 
|  | 445 | \end{seealso} | 
|  | 446 |  | 
|  | 447 |  | 
| Fred Drake | a94f676 | 1999-08-05 13:41:04 +0000 | [diff] [blame] | 448 | \subsection{Socket Objects \label{socket-objects}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 449 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 450 | Socket objects have the following methods.  Except for | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 451 | \method{makefile()} these correspond to \UNIX{} system calls | 
|  | 452 | applicable to sockets. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 453 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 454 | \begin{methoddesc}[socket]{accept}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 455 | Accept a connection. | 
|  | 456 | The socket must be bound to an address and listening for connections. | 
|  | 457 | The return value is a pair \code{(\var{conn}, \var{address})} | 
|  | 458 | where \var{conn} is a \emph{new} socket object usable to send and | 
|  | 459 | receive data on the connection, and \var{address} is the address bound | 
|  | 460 | to the socket on the other end of the connection. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 461 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 462 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 463 | \begin{methoddesc}[socket]{bind}{address} | 
| Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 464 | 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] | 465 | (The format of \var{address} depends on the address family --- see | 
| Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 466 | above.)  \note{This method has historically accepted a pair | 
| Fred Drake | 7d68690 | 2000-04-04 17:48:30 +0000 | [diff] [blame] | 467 | of parameters for \constant{AF_INET} addresses instead of only a | 
| Neal Norwitz | ba813e2 | 2004-04-03 18:02:37 +0000 | [diff] [blame] | 468 | tuple.  This was never intentional and is no longer available in | 
|  | 469 | Python 2.0 and later.} | 
| 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]{close}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 473 | Close the socket.  All future operations on the socket object will fail. | 
|  | 474 | The remote end will receive no more data (after queued data is flushed). | 
|  | 475 | Sockets are automatically closed when they are garbage-collected. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 476 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 477 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 478 | \begin{methoddesc}[socket]{connect}{address} | 
| Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 479 | Connect to a remote socket at \var{address}. | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 480 | (The format of \var{address} depends on the address family --- see | 
| Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 481 | above.)  \note{This method has historically accepted a pair | 
| Fred Drake | 7d68690 | 2000-04-04 17:48:30 +0000 | [diff] [blame] | 482 | of parameters for \constant{AF_INET} addresses instead of only a | 
| Eric S. Raymond | 8321026 | 2001-01-10 19:34:52 +0000 | [diff] [blame] | 483 | tuple.  This was never intentional and is no longer available in | 
| Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 484 | Python 2.0 and later.} | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 485 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 486 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 487 | \begin{methoddesc}[socket]{connect_ex}{address} | 
| Guido van Rossum | eefcba6 | 1997-12-09 19:47:24 +0000 | [diff] [blame] | 488 | Like \code{connect(\var{address})}, but return an error indicator | 
| Fred Drake | b0bc7f2 | 1999-05-06 22:03:50 +0000 | [diff] [blame] | 489 | instead of raising an exception for errors returned by the C-level | 
|  | 490 | \cfunction{connect()} call (other problems, such as ``host not found,'' | 
|  | 491 | can still raise exceptions).  The error indicator is \code{0} if the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 492 | operation succeeded, otherwise the value of the \cdata{errno} | 
| Fred Drake | 87fa3aa | 2001-12-21 17:45:03 +0000 | [diff] [blame] | 493 | variable.  This is useful to support, for example, asynchronous connects. | 
| Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 494 | \note{This method has historically accepted a pair of | 
| Fred Drake | 7d68690 | 2000-04-04 17:48:30 +0000 | [diff] [blame] | 495 | parameters for \constant{AF_INET} addresses instead of only a tuple. | 
| Neal Norwitz | ba813e2 | 2004-04-03 18:02:37 +0000 | [diff] [blame] | 496 | This was never intentional and is no longer available in Python | 
| Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 497 | 2.0 and later.} | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 498 | \end{methoddesc} | 
| Guido van Rossum | f7790c6 | 1997-11-18 15:29:20 +0000 | [diff] [blame] | 499 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 500 | \begin{methoddesc}[socket]{fileno}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 501 | Return the socket's file descriptor (a small integer).  This is useful | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 502 | with \function{select.select()}. | 
| Brett Cannon | b278ac4 | 2003-08-05 03:51:24 +0000 | [diff] [blame] | 503 |  | 
|  | 504 | Under Windows the small integer returned by this method cannot be used where | 
|  | 505 | a file descriptor can be used (such as \function{os.fdopen()}).  \UNIX{} does | 
|  | 506 | not have this limitation. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 507 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 508 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 509 | \begin{methoddesc}[socket]{getpeername}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 510 | 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] | 511 | 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] | 512 | (The format of the address returned depends on the address family --- | 
| Guido van Rossum | 781db5d | 1994-08-05 13:37:36 +0000 | [diff] [blame] | 513 | see above.)  On some systems this function is not supported. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 514 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 515 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 516 | \begin{methoddesc}[socket]{getsockname}{} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 517 | 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] | 518 | number of an IPv4/v6 socket, for instance. | 
| Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 519 | (The format of the address returned depends on the address family --- | 
| Guido van Rossum | a84ec51 | 1994-06-23 12:13:52 +0000 | [diff] [blame] | 520 | see above.) | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 521 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 522 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 523 | \begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 524 | 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] | 525 | \manpage{getsockopt}{2}).  The needed symbolic constants | 
|  | 526 | (\constant{SO_*} etc.) are defined in this module.  If \var{buflen} | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 527 | is absent, an integer option is assumed and its integer value | 
| Guido van Rossum | 8df3637 | 1995-02-27 17:52:15 +0000 | [diff] [blame] | 528 | is returned by the function.  If \var{buflen} is present, it specifies | 
|  | 529 | 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] | 530 | 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] | 531 | the contents of the buffer (see the optional built-in module | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 532 | \refmodule{struct} for a way to decode C structures encoded as strings). | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 533 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 534 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 535 | \begin{methoddesc}[socket]{listen}{backlog} | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 536 | Listen for connections made to the socket.  The \var{backlog} argument | 
|  | 537 | specifies the maximum number of queued connections and should be at | 
|  | 538 | least 1; the maximum value is system-dependent (usually 5). | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 539 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 540 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 541 | \begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}} | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 542 | Return a \dfn{file object} associated with the socket.  (File objects | 
| Fred Drake | a94f676 | 1999-08-05 13:41:04 +0000 | [diff] [blame] | 543 | are described in \ref{bltin-file-objects}, ``File Objects.'') | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 544 | The file object references a \cfunction{dup()}ped version of the | 
|  | 545 | socket file descriptor, so the file object and socket object may be | 
| Fred Drake | a94f676 | 1999-08-05 13:41:04 +0000 | [diff] [blame] | 546 | closed or garbage-collected independently. | 
| Guido van Rossum | 715b861 | 2002-06-07 12:38:23 +0000 | [diff] [blame] | 547 | The socket should be in blocking mode. | 
| Fred Drake | a94f676 | 1999-08-05 13:41:04 +0000 | [diff] [blame] | 548 | \index{I/O control!buffering}The optional \var{mode} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 549 | and \var{bufsize} arguments are interpreted the same way as by the | 
| Fred Drake | aad8bb5 | 2001-10-19 17:22:29 +0000 | [diff] [blame] | 550 | built-in \function{file()} function; see ``Built-in Functions'' | 
|  | 551 | (section \ref{built-in-funcs}) for more information. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 552 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 553 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 554 | \begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 555 | Receive data from the socket.  The return value is a string representing | 
|  | 556 | the data received.  The maximum amount of data to be received | 
|  | 557 | at once is specified by \var{bufsize}.  See the \UNIX{} manual page | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 558 | \manpage{recv}{2} for the meaning of the optional argument | 
|  | 559 | \var{flags}; it defaults to zero. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 560 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 561 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 562 | \begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 563 | Receive data from the socket.  The return value is a pair | 
|  | 564 | \code{(\var{string}, \var{address})} where \var{string} is a string | 
|  | 565 | 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] | 566 | socket sending the data.  The optional \var{flags} argument has the | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 567 | same meaning as for \method{recv()} above. | 
| Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 568 | (The format of \var{address} depends on the address family --- see above.) | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 569 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 570 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 571 | \begin{methoddesc}[socket]{send}{string\optional{, flags}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 572 | 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] | 573 | socket.  The optional \var{flags} argument has the same meaning as for | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 574 | \method{recv()} above.  Returns the number of bytes sent. | 
| Fred Drake | 39368c1 | 2001-12-05 05:25:59 +0000 | [diff] [blame] | 575 | Applications are responsible for checking that all data has been sent; | 
|  | 576 | if only some of the data was transmitted, the application needs to | 
|  | 577 | attempt delivery of the remaining data. | 
|  | 578 | \end{methoddesc} | 
|  | 579 |  | 
|  | 580 | \begin{methoddesc}[socket]{sendall}{string\optional{, flags}} | 
|  | 581 | Send data to the socket.  The socket must be connected to a remote | 
|  | 582 | socket.  The optional \var{flags} argument has the same meaning as for | 
|  | 583 | \method{recv()} above.  Unlike \method{send()}, this method continues | 
|  | 584 | to send data from \var{string} until either all data has been sent or | 
|  | 585 | an error occurs.  \code{None} is returned on success.  On error, an | 
|  | 586 | exception is raised, and there is no way to determine how much data, | 
|  | 587 | if any, was successfully sent. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 588 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 589 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 590 | \begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 591 | Send data to the socket.  The socket should not be connected to a | 
|  | 592 | remote socket, since the destination socket is specified by | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 593 | \var{address}.  The optional \var{flags} argument has the same | 
|  | 594 | 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] | 595 | (The format of \var{address} depends on the address family --- see above.) | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 596 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 597 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 598 | \begin{methoddesc}[socket]{setblocking}{flag} | 
| Guido van Rossum | 9195148 | 1994-09-07 14:39:14 +0000 | [diff] [blame] | 599 | Set blocking or non-blocking mode of the socket: if \var{flag} is 0, | 
|  | 600 | the socket is set to non-blocking, else to blocking mode.  Initially | 
|  | 601 | all sockets are in blocking mode.  In non-blocking mode, if a | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 602 | \method{recv()} call doesn't find any data, or if a | 
|  | 603 | \method{send()} call can't immediately dispose of the data, a | 
|  | 604 | \exception{error} exception is raised; in blocking mode, the calls | 
|  | 605 | block until they can proceed. | 
| Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 606 | \code{s.setblocking(0)} is equivalent to \code{s.settimeout(0)}; | 
|  | 607 | \code{s.setblocking(1)} is equivalent to \code{s.settimeout(None)}. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 608 | \end{methoddesc} | 
| Guido van Rossum | 9195148 | 1994-09-07 14:39:14 +0000 | [diff] [blame] | 609 |  | 
| Guido van Rossum | be946bf | 2002-06-06 21:51:01 +0000 | [diff] [blame] | 610 | \begin{methoddesc}[socket]{settimeout}{value} | 
| Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 611 | Set a timeout on blocking socket operations.  The \var{value} argument | 
|  | 612 | can be a nonnegative float expressing seconds, or \code{None}. | 
|  | 613 | If a float is | 
| Raymond Hettinger | be2528d | 2003-06-29 04:55:59 +0000 | [diff] [blame] | 614 | given, subsequent socket operations will raise an \exception{timeout} | 
| Guido van Rossum | fc9823b | 2002-06-07 03:39:21 +0000 | [diff] [blame] | 615 | exception if the timeout period \var{value} has elapsed before the | 
|  | 616 | operation has completed.  Setting a timeout of \code{None} disables | 
|  | 617 | timeouts on socket operations. | 
| Neal Norwitz | 3a03de4 | 2003-06-20 17:11:39 +0000 | [diff] [blame] | 618 | \code{s.settimeout(0.0)} is equivalent to \code{s.setblocking(0)}; | 
| Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 619 | \code{s.settimeout(None)} is equivalent to \code{s.setblocking(1)}. | 
| Neal Norwitz | bdbd84f | 2002-06-06 22:24:10 +0000 | [diff] [blame] | 620 | \versionadded{2.3} | 
| Guido van Rossum | be946bf | 2002-06-06 21:51:01 +0000 | [diff] [blame] | 621 | \end{methoddesc} | 
|  | 622 |  | 
|  | 623 | \begin{methoddesc}[socket]{gettimeout}{} | 
| Fred Drake | 6c6d662 | 2002-06-06 21:57:48 +0000 | [diff] [blame] | 624 | Returns the timeout in floating seconds associated with socket | 
| Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 625 | operations, or \code{None} if no timeout is set.  This reflects | 
|  | 626 | the last call to \method{setblocking()} or \method{settimeout()}. | 
| Neal Norwitz | bdbd84f | 2002-06-06 22:24:10 +0000 | [diff] [blame] | 627 | \versionadded{2.3} | 
| Guido van Rossum | be946bf | 2002-06-06 21:51:01 +0000 | [diff] [blame] | 628 | \end{methoddesc} | 
|  | 629 |  | 
| Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 630 | Some notes on socket blocking and timeouts: A socket object can be in | 
| Raymond Hettinger | 476fcae | 2003-07-20 01:10:15 +0000 | [diff] [blame] | 631 | one of three modes: blocking, non-blocking, or timeout.  Sockets are | 
| Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 632 | always created in blocking mode.  In blocking mode, operations block | 
|  | 633 | until complete.  In non-blocking mode, operations fail (with an error | 
|  | 634 | that is unfortunately system-dependent) if they cannot be completed | 
|  | 635 | immediately.  In timeout mode, operations fail if they cannot be | 
|  | 636 | completed within the timeout specified for the socket.  The | 
|  | 637 | \method{setblocking()} method is simply a shorthand for certain | 
|  | 638 | \method{settimeout()} calls. | 
| Guido van Rossum | be946bf | 2002-06-06 21:51:01 +0000 | [diff] [blame] | 639 |  | 
| Guido van Rossum | 715b861 | 2002-06-07 12:38:23 +0000 | [diff] [blame] | 640 | Timeout mode internally sets the socket in non-blocking mode.  The | 
|  | 641 | blocking and timeout modes are shared between file descriptors and | 
|  | 642 | socket objects that refer to the same network endpoint.  A consequence | 
|  | 643 | of this is that file objects returned by the \method{makefile()} | 
|  | 644 | method should only be used when the socket is in blocking mode; in | 
|  | 645 | timeout or non-blocking mode file operations that cannot be completed | 
|  | 646 | immediately will fail. | 
|  | 647 |  | 
| Guido van Rossum | 5a92175 | 2003-12-13 22:12:53 +0000 | [diff] [blame] | 648 | Note that the \method{connect()} operation is subject to the timeout | 
|  | 649 | setting, and in general it is recommended to call | 
|  | 650 | \method{settimeout()} before calling \method{connect()}. | 
|  | 651 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 652 | \begin{methoddesc}[socket]{setsockopt}{level, optname, value} | 
| Fred Drake | 9a748aa | 2000-06-30 04:21:41 +0000 | [diff] [blame] | 653 | 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] | 654 | \manpage{setsockopt}{2}).  The needed symbolic constants are defined in | 
| Fred Drake | d198f38 | 2003-04-25 16:16:02 +0000 | [diff] [blame] | 655 | the \module{socket} module (\constant{SO_*} etc.).  The value can be an | 
| Guido van Rossum | 8df3637 | 1995-02-27 17:52:15 +0000 | [diff] [blame] | 656 | integer or a string representing a buffer.  In the latter case it is | 
|  | 657 | up to the caller to ensure that the string contains the proper bits | 
|  | 658 | (see the optional built-in module | 
| Fred Drake | 318c0b1 | 1999-04-21 17:29:14 +0000 | [diff] [blame] | 659 | \refmodule{struct}\refbimodindex{struct} for a way to encode C | 
|  | 660 | structures as strings). | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 661 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 662 |  | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 663 | \begin{methoddesc}[socket]{shutdown}{how} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 664 | Shut down one or both halves of the connection.  If \var{how} is | 
| Martin v. Löwis | 94681fc | 2003-11-27 19:40:22 +0000 | [diff] [blame] | 665 | \constant{SHUT_RD}, further receives are disallowed.  If \var{how} is \constant{SHUT_WR}, | 
|  | 666 | further sends are disallowed.  If \var{how} is \constant{SHUT_RDWR}, further sends | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 667 | and receives are disallowed. | 
| Fred Drake | 3f1c472 | 1998-04-03 07:04:45 +0000 | [diff] [blame] | 668 | \end{methoddesc} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 669 |  | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 670 | Note that there are no methods \method{read()} or \method{write()}; | 
|  | 671 | use \method{recv()} and \method{send()} without \var{flags} argument | 
|  | 672 | instead. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 673 |  | 
| Fred Drake | aa7524c | 2000-07-06 18:37:08 +0000 | [diff] [blame] | 674 |  | 
| Fred Drake | 9081bb1 | 2001-09-25 15:48:11 +0000 | [diff] [blame] | 675 | \subsection{SSL Objects \label{ssl-objects}} | 
|  | 676 |  | 
|  | 677 | SSL objects have the following methods. | 
|  | 678 |  | 
|  | 679 | \begin{methoddesc}{write}{s} | 
|  | 680 | Writes the string \var{s} to the on the object's SSL connection. | 
|  | 681 | The return value is the number of bytes written. | 
|  | 682 | \end{methoddesc} | 
|  | 683 |  | 
|  | 684 | \begin{methoddesc}{read}{\optional{n}} | 
|  | 685 | If \var{n} is provided, read \var{n} bytes from the SSL connection, otherwise | 
|  | 686 | read until EOF. The return value is a string of the bytes read. | 
|  | 687 | \end{methoddesc} | 
|  | 688 |  | 
| Fred Drake | aa7524c | 2000-07-06 18:37:08 +0000 | [diff] [blame] | 689 | \subsection{Example \label{socket-example}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 690 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 691 | 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] | 692 | server that echoes all data that it receives back (servicing only one | 
|  | 693 | 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] | 694 | sequence \function{socket()}, \method{bind()}, \method{listen()}, | 
|  | 695 | \method{accept()} (possibly repeating the \method{accept()} to service | 
|  | 696 | more than one client), while a client only needs the sequence | 
|  | 697 | \function{socket()}, \method{connect()}.  Also note that the server | 
|  | 698 | does not \method{send()}/\method{recv()} on the | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 699 | socket it is listening on but on the new socket returned by | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 700 | \method{accept()}. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 701 |  | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 702 | The first two examples support IPv4 only. | 
|  | 703 |  | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 704 | \begin{verbatim} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 705 | # Echo server program | 
| Fred Drake | ef52f60 | 2000-10-10 20:36:29 +0000 | [diff] [blame] | 706 | import socket | 
|  | 707 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 708 | HOST = ''                 # Symbolic name meaning the local host | 
| Fred Drake | ef52f60 | 2000-10-10 20:36:29 +0000 | [diff] [blame] | 709 | PORT = 50007              # Arbitrary non-privileged port | 
|  | 710 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 
| Fred Drake | 3d69c0e | 2000-05-03 19:40:32 +0000 | [diff] [blame] | 711 | s.bind((HOST, PORT)) | 
| Guido van Rossum | 5da5755 | 1994-03-02 10:52:16 +0000 | [diff] [blame] | 712 | s.listen(1) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 713 | conn, addr = s.accept() | 
|  | 714 | print 'Connected by', addr | 
|  | 715 | while 1: | 
|  | 716 | data = conn.recv(1024) | 
|  | 717 | if not data: break | 
|  | 718 | conn.send(data) | 
|  | 719 | conn.close() | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 720 | \end{verbatim} | 
| Fred Drake | d883ca1 | 1998-03-10 05:20:33 +0000 | [diff] [blame] | 721 |  | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 722 | \begin{verbatim} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 723 | # Echo client program | 
| Fred Drake | ef52f60 | 2000-10-10 20:36:29 +0000 | [diff] [blame] | 724 | import socket | 
|  | 725 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 726 | HOST = 'daring.cwi.nl'    # The remote host | 
|  | 727 | PORT = 50007              # The same port as used by the server | 
| Fred Drake | ef52f60 | 2000-10-10 20:36:29 +0000 | [diff] [blame] | 728 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 
| Fred Drake | 3d69c0e | 2000-05-03 19:40:32 +0000 | [diff] [blame] | 729 | s.connect((HOST, PORT)) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 730 | s.send('Hello, world') | 
|  | 731 | data = s.recv(1024) | 
|  | 732 | s.close() | 
| Fred Drake | 175d188 | 2004-06-03 16:23:23 +0000 | [diff] [blame] | 733 | print 'Received', repr(data) | 
| Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 734 | \end{verbatim} | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 735 |  | 
|  | 736 | The next two examples are identical to the above two, but support both | 
|  | 737 | IPv4 and IPv6. | 
|  | 738 | The server side will listen to the first address family available | 
|  | 739 | (it should listen to both instead). | 
|  | 740 | On most of IPv6-ready systems, IPv6 will take precedence | 
|  | 741 | and the server may not accept IPv4 traffic. | 
|  | 742 | The client side will try to connect to the all addresses returned as a result | 
|  | 743 | of the name resolution, and sends traffic to the first one connected | 
|  | 744 | successfully. | 
|  | 745 |  | 
|  | 746 | \begin{verbatim} | 
|  | 747 | # Echo server program | 
|  | 748 | import socket | 
|  | 749 | import sys | 
|  | 750 |  | 
|  | 751 | HOST = ''                 # Symbolic name meaning the local host | 
|  | 752 | PORT = 50007              # Arbitrary non-privileged port | 
|  | 753 | s = None | 
|  | 754 | for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): | 
|  | 755 | af, socktype, proto, canonname, sa = res | 
|  | 756 | try: | 
|  | 757 | s = socket.socket(af, socktype, proto) | 
|  | 758 | except socket.error, msg: | 
|  | 759 | s = None | 
|  | 760 | continue | 
|  | 761 | try: | 
|  | 762 | s.bind(sa) | 
|  | 763 | s.listen(1) | 
|  | 764 | except socket.error, msg: | 
|  | 765 | s.close() | 
|  | 766 | s = None | 
|  | 767 | continue | 
|  | 768 | break | 
|  | 769 | if s is None: | 
|  | 770 | print 'could not open socket' | 
|  | 771 | sys.exit(1) | 
|  | 772 | conn, addr = s.accept() | 
|  | 773 | print 'Connected by', addr | 
|  | 774 | while 1: | 
|  | 775 | data = conn.recv(1024) | 
|  | 776 | if not data: break | 
|  | 777 | conn.send(data) | 
|  | 778 | conn.close() | 
|  | 779 | \end{verbatim} | 
|  | 780 |  | 
|  | 781 | \begin{verbatim} | 
|  | 782 | # Echo client program | 
|  | 783 | import socket | 
|  | 784 | import sys | 
|  | 785 |  | 
|  | 786 | HOST = 'daring.cwi.nl'    # The remote host | 
|  | 787 | PORT = 50007              # The same port as used by the server | 
|  | 788 | s = None | 
|  | 789 | for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM): | 
|  | 790 | af, socktype, proto, canonname, sa = res | 
|  | 791 | try: | 
|  | 792 | s = socket.socket(af, socktype, proto) | 
|  | 793 | except socket.error, msg: | 
|  | 794 | s = None | 
|  | 795 | continue | 
|  | 796 | try: | 
|  | 797 | s.connect(sa) | 
|  | 798 | except socket.error, msg: | 
|  | 799 | s.close() | 
|  | 800 | s = None | 
|  | 801 | continue | 
|  | 802 | break | 
|  | 803 | if s is None: | 
|  | 804 | print 'could not open socket' | 
|  | 805 | sys.exit(1) | 
|  | 806 | s.send('Hello, world') | 
|  | 807 | data = s.recv(1024) | 
|  | 808 | s.close() | 
| Fred Drake | 175d188 | 2004-06-03 16:23:23 +0000 | [diff] [blame] | 809 | print 'Received', repr(data) | 
| Martin v. Löwis | c9908c4 | 2001-08-04 22:22:45 +0000 | [diff] [blame] | 810 | \end{verbatim} |