Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{socket}} |
| 2 | |
| 3 | \bimodindex{socket} |
| 4 | This module provides access to the BSD {\em socket} interface. |
| 5 | It is available on \UNIX{} systems that support this interface. |
| 6 | |
| 7 | For an introduction to socket programming (in C), see the following |
| 8 | papers: \emph{An Introductory 4.3BSD Interprocess Communication |
| 9 | Tutorial}, by Stuart Sechrest and \emph{An Advanced 4.3BSD Interprocess |
| 10 | Communication Tutorial}, by Samuel J. Leffler et al, both in the |
| 11 | \UNIX{} Programmer's Manual, Supplementary Documents 1 (sections PS1:7 |
| 12 | and PS1:8). The \UNIX{} manual pages for the various socket-related |
| 13 | system calls also a valuable source of information on the details of |
| 14 | socket semantics. |
| 15 | |
| 16 | The Python interface is a straightforward transliteration of the |
| 17 | \UNIX{} system call and library interface for sockets to Python's |
| 18 | object-oriented style: the \code{socket()} function returns a |
| 19 | \dfn{socket object} whose methods implement the various socket system |
| 20 | calls. Parameter types are somewhat higer-level than in the C |
| 21 | interface: as with \code{read()} and \code{write()} operations on Python |
| 22 | files, buffer allocation on receive operations is automatic, and |
| 23 | buffer length is implicit on send operations. |
| 24 | |
| 25 | Socket addresses are represented as a single string for the |
| 26 | \code{AF_UNIX} address family and as a pair |
| 27 | \code{(\var{host}, \var{port})} for the \code{AF_INET} address family, |
| 28 | where \var{host} is a string representing |
| 29 | either a hostname in Internet domain notation like |
| 30 | \code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'}, |
| 31 | and \var{port} is an integral port number. Other address families are |
| 32 | currently not supported. The address format required by a particular |
| 33 | socket object is automatically selected based on the address family |
| 34 | specified when the socket object was created. |
| 35 | |
| 36 | All errors raise exceptions. The normal exceptions for invalid |
| 37 | argument types and out-of-memory conditions can be raised; errors |
| 38 | related to socket or address semantics raise the error \code{socket.error}. |
| 39 | |
| 40 | Non-blocking and asynchronous mode are not supported; see module |
| 41 | \code{select} for a way to do non-blocking socket I/O. |
| 42 | |
| 43 | The module \code{socket} exports the following constants and functions: |
| 44 | |
| 45 | \renewcommand{\indexsubitem}{(in module socket)} |
| 46 | \begin{excdesc}{error} |
| 47 | This exception is raised for socket- or address-related errors. |
| 48 | The accompanying value is either a string telling what went wrong or a |
| 49 | pair \code{(\var{errno}, \var{string})} |
| 50 | representing an error returned by a system |
| 51 | call, similar to the value accompanying \code{posix.error}. |
| 52 | \end{excdesc} |
| 53 | |
| 54 | \begin{datadesc}{AF_UNIX} |
| 55 | \dataline{AF_INET} |
| 56 | These constants represent the address (and protocol) families, |
| 57 | used for the first argument to \code{socket()}. |
| 58 | \end{datadesc} |
| 59 | |
| 60 | \begin{datadesc}{SOCK_STREAM} |
| 61 | \dataline{SOCK_DGRAM} |
| 62 | These constants represent the socket types, |
| 63 | used for the second argument to \code{socket()}. |
| 64 | (There are other types, but only \code{SOCK_STREAM} and |
| 65 | \code{SOCK_DGRAM} appear to be generally useful.) |
| 66 | \end{datadesc} |
| 67 | |
| 68 | \begin{funcdesc}{gethostbyname}{hostname} |
| 69 | Translate a host name to IP address format. The IP address is |
| 70 | returned as a string, e.g., \code{'100.50.200.5'}. If the host name |
| 71 | is an IP address itself it is returned unchanged. |
| 72 | \end{funcdesc} |
| 73 | |
| 74 | \begin{funcdesc}{getservbyname}{servicename\, protocolname} |
| 75 | Translate an Internet service name and protocol name to a port number |
| 76 | for that service. The protocol name should be \code{'tcp'} or |
| 77 | \code{'udp'}. |
| 78 | \end{funcdesc} |
| 79 | |
| 80 | \begin{funcdesc}{socket}{family\, type\, proto} |
| 81 | Create a new socket using the given address family, socket type and |
| 82 | protocol number. The address family should be \code{AF_INET} or |
| 83 | \code{AF_UNIX}. The socket type should be \code{SOCK_STREAM}, |
| 84 | \code{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants. |
| 85 | The protocol number is usually zero and may be omitted in that case. |
| 86 | \end{funcdesc} |
| 87 | |
| 88 | \begin{funcdesc}{fromfd}{fd\, family\, type\, proto} |
| 89 | Build a socket object from an existing file descriptor (an integer as |
| 90 | returned by a file object's \code{fileno} method). Address family, |
| 91 | socket type and protocol number are as for the \code{socket} function |
| 92 | above. The file descriptor should refer to a socket, but this is not |
| 93 | checked --- subsequent operations on the object may fail if the file |
| 94 | descriptor is invalid. This function is rarely needed, but can be |
| 95 | used to get or set socket options on a socket passed to a program as |
| 96 | standard input or output (e.g. a server started by the \UNIX{} inet |
| 97 | daemon). |
| 98 | \end{funcdesc} |
| 99 | |
| 100 | \subsection{Socket Object Methods} |
| 101 | |
| 102 | \noindent |
| 103 | Socket objects have the following methods. Except for |
| 104 | \code{makefile()} these correspond to \UNIX{} system calls applicable to |
| 105 | sockets. |
| 106 | |
| 107 | \renewcommand{\indexsubitem}{(socket method)} |
| 108 | \begin{funcdesc}{accept}{} |
| 109 | Accept a connection. |
| 110 | The socket must be bound to an address and listening for connections. |
| 111 | The return value is a pair \code{(\var{conn}, \var{address})} |
| 112 | where \var{conn} is a \emph{new} socket object usable to send and |
| 113 | receive data on the connection, and \var{address} is the address bound |
| 114 | to the socket on the other end of the connection. |
| 115 | \end{funcdesc} |
| 116 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 117 | \begin{funcdesc}{bind}{address} |
| 118 | Bind the socket to an address. The socket must not already be bound. |
| 119 | \end{funcdesc} |
| 120 | |
| 121 | \begin{funcdesc}{close}{} |
| 122 | Close the socket. All future operations on the socket object will fail. |
| 123 | The remote end will receive no more data (after queued data is flushed). |
| 124 | Sockets are automatically closed when they are garbage-collected. |
| 125 | \end{funcdesc} |
| 126 | |
| 127 | \begin{funcdesc}{connect}{address} |
| 128 | Connect to a remote socket. |
| 129 | \end{funcdesc} |
| 130 | |
| 131 | \begin{funcdesc}{fileno}{} |
| 132 | Return the socket's file descriptor (a small integer). This is useful |
| 133 | with \code{select}. |
| 134 | \end{funcdesc} |
| 135 | |
| 136 | \begin{funcdesc}{getpeername}{} |
| 137 | Return the remote address to which the socket is connected. This is |
| 138 | useful to find out the port number of a remote IP socket, for instance. |
| 139 | \end{funcdesc} |
| 140 | |
| 141 | \begin{funcdesc}{getsockname}{} |
| 142 | Return the socket's own address. This is useful to find out the port |
| 143 | number of an IP socket, for instance. |
| 144 | \end{funcdesc} |
| 145 | |
| 146 | \begin{funcdesc}{getsockopt}{level\, optname\, buflen} |
| 147 | Return the value of the given socket option (see the \UNIX{} man page |
| 148 | {\it getsockopt}(2)). The needed symbolic constants are defined in module |
| 149 | SOCKET. If the optional third argument is absent, an integer option |
| 150 | is assumed and its integer value is returned by the function. If |
| 151 | \var{buflen} is present, it specifies the maximum length of the buffer used |
| 152 | to receive the option in, and this buffer is returned as a string. |
| 153 | It's up to the caller to decode the contents of the buffer (see the |
| 154 | optional built-in module \code{struct} for a way to decode C structures |
| 155 | encoded as strings). |
| 156 | \end{funcdesc} |
| 157 | |
| 158 | \begin{funcdesc}{listen}{backlog} |
| 159 | Listen for connections made to the socket. |
Guido van Rossum | 5da5755 | 1994-03-02 10:52:16 +0000 | [diff] [blame] | 160 | The argument specifies the maximum number of queued connections and |
| 161 | should be at least 1; the maximum value is system-dependent. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 162 | \end{funcdesc} |
| 163 | |
| 164 | \begin{funcdesc}{makefile}{mode} |
| 165 | Return a \dfn{file object} associated with the socket. |
| 166 | (File objects were described earlier under Built-in Types.) |
| 167 | The file object references a \code{dup}ped version of the socket file |
| 168 | descriptor, so the file object and socket object may be closed or |
| 169 | garbage-collected independently. |
| 170 | \end{funcdesc} |
| 171 | |
| 172 | \begin{funcdesc}{recv}{bufsize\, flags} |
| 173 | Receive data from the socket. The return value is a string representing |
| 174 | the data received. The maximum amount of data to be received |
| 175 | at once is specified by \var{bufsize}. See the \UNIX{} manual page |
| 176 | for the meaning of the optional argument \var{flags}; it defaults to |
| 177 | zero. |
| 178 | \end{funcdesc} |
| 179 | |
| 180 | \begin{funcdesc}{recvfrom}{bufsize} |
| 181 | Receive data from the socket. The return value is a pair |
| 182 | \code{(\var{string}, \var{address})} where \var{string} is a string |
| 183 | representing the data received and \var{address} is the address of the |
| 184 | socket sending the data. |
| 185 | \end{funcdesc} |
| 186 | |
| 187 | \begin{funcdesc}{send}{string} |
| 188 | Send data to the socket. The socket must be connected to a remote |
| 189 | socket. |
| 190 | \end{funcdesc} |
| 191 | |
| 192 | \begin{funcdesc}{sendto}{string\, address} |
| 193 | Send data to the socket. The socket should not be connected to a |
| 194 | remote socket, since the destination socket is specified by |
| 195 | \code{address}. |
| 196 | \end{funcdesc} |
| 197 | |
| 198 | \begin{funcdesc}{setsockopt}{level\, optname\, value} |
| 199 | Set the value of the given socket option (see the \UNIX{} man page |
| 200 | {\it setsockopt}(2)). The needed symbolic constants are defined in module |
| 201 | \code{SOCKET}. The value can be an integer or a string representing a |
| 202 | buffer. In the latter case it is up to the caller to ensure that the |
| 203 | string contains the proper bits (see the optional built-in module |
| 204 | \code{struct} for a way to encode C structures as strings). |
| 205 | \end{funcdesc} |
| 206 | |
| 207 | \begin{funcdesc}{shutdown}{how} |
| 208 | Shut down one or both halves of the connection. If \var{how} is \code{0}, |
| 209 | further receives are disallowed. If \var{how} is \code{1}, further sends are |
| 210 | disallowed. If \var{how} is \code{2}, further sends and receives are |
| 211 | disallowed. |
| 212 | \end{funcdesc} |
| 213 | |
| 214 | Note that there are no methods \code{read()} or \code{write()}; use |
| 215 | \code{recv()} and \code{send()} without \var{flags} argument instead. |
| 216 | |
| 217 | \subsection{Example} |
| 218 | \nodename{Socket Example} |
| 219 | |
| 220 | Here are two minimal example programs using the TCP/IP protocol: a |
| 221 | server that echoes all data that it receives back (servicing only one |
| 222 | client), and a client using it. Note that a server must perform the |
| 223 | sequence \code{socket}, \code{bind}, \code{listen}, \code{accept} |
| 224 | (possibly repeating the \code{accept} to service more than one client), |
| 225 | while a client only needs the sequence \code{socket}, \code{connect}. |
| 226 | Also note that the server does not \code{send}/\code{receive} on the |
| 227 | socket it is listening on but on the new socket returned by |
| 228 | \code{accept}. |
| 229 | |
| 230 | \bcode\begin{verbatim} |
| 231 | # Echo server program |
| 232 | from socket import * |
| 233 | HOST = '' # Symbolic name meaning the local host |
| 234 | PORT = 50007 # Arbitrary non-privileged server |
| 235 | s = socket(AF_INET, SOCK_STREAM) |
| 236 | s.bind(HOST, PORT) |
Guido van Rossum | 5da5755 | 1994-03-02 10:52:16 +0000 | [diff] [blame] | 237 | s.listen(1) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 238 | conn, addr = s.accept() |
| 239 | print 'Connected by', addr |
| 240 | while 1: |
| 241 | data = conn.recv(1024) |
| 242 | if not data: break |
| 243 | conn.send(data) |
| 244 | conn.close() |
| 245 | \end{verbatim}\ecode |
| 246 | |
| 247 | \bcode\begin{verbatim} |
| 248 | # Echo client program |
| 249 | from socket import * |
| 250 | HOST = 'daring.cwi.nl' # The remote host |
| 251 | PORT = 50007 # The same port as used by the server |
| 252 | s = socket(AF_INET, SOCK_STREAM) |
| 253 | s.connect(HOST, PORT) |
| 254 | s.send('Hello, world') |
| 255 | data = s.recv(1024) |
| 256 | s.close() |
| 257 | print 'Received', `data` |
| 258 | \end{verbatim}\ecode |