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