1 | \section{\module{socket} ---
|
---|
2 | Low-level networking interface}
|
---|
3 |
|
---|
4 | \declaremodule{builtin}{socket}
|
---|
5 | \modulesynopsis{Low-level networking interface.}
|
---|
6 |
|
---|
7 |
|
---|
8 | This module provides access to the BSD \emph{socket} interface.
|
---|
9 | It is available on all modern \UNIX{} systems, Windows, MacOS, BeOS,
|
---|
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.}
|
---|
12 |
|
---|
13 | For an introduction to socket programming (in C), see the following
|
---|
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}
|
---|
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.
|
---|
23 | For IPv6-ready APIs, readers may want to refer to \rfc{2553} titled
|
---|
24 | \citetitle{Basic Socket Interface Extensions for IPv6}.
|
---|
25 |
|
---|
26 | The Python interface is a straightforward transliteration of the
|
---|
27 | \UNIX{} system call and library interface for sockets to Python's
|
---|
28 | object-oriented style: the \function{socket()} function returns a
|
---|
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.
|
---|
35 |
|
---|
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.
|
---|
55 |
|
---|
56 | For IPv4 addresses, two special forms are accepted instead of a host
|
---|
57 | address: the empty string represents \constant{INADDR_ANY}, and the string
|
---|
58 | \code{'<broadcast>'} represents \constant{INADDR_BROADCAST}.
|
---|
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.
|
---|
70 |
|
---|
71 | \versionadded[AF_NETLINK sockets are represented as
|
---|
72 | pairs \code{\var{pid}, \var{groups}}]{2.5}
|
---|
73 |
|
---|
74 | All errors raise exceptions. The normal exceptions for invalid
|
---|
75 | argument types and out-of-memory conditions can be raised; errors
|
---|
76 | related to socket or address semantics raise the error
|
---|
77 | \exception{socket.error}.
|
---|
78 |
|
---|
79 | Non-blocking mode is supported through
|
---|
80 | \method{setblocking()}. A generalization of this based on timeouts
|
---|
81 | is supported through \method{settimeout()}.
|
---|
82 |
|
---|
83 | The module \module{socket} exports the following constants and functions:
|
---|
84 |
|
---|
85 |
|
---|
86 | \begin{excdesc}{error}
|
---|
87 | This exception is raised for socket-related errors.
|
---|
88 | The accompanying value is either a string telling what went wrong or a
|
---|
89 | pair \code{(\var{errno}, \var{string})}
|
---|
90 | representing an error returned by a system
|
---|
91 | call, similar to the value accompanying \exception{os.error}.
|
---|
92 | See the module \refmodule{errno}\refbimodindex{errno}, which contains
|
---|
93 | names for the error codes defined by the underlying operating system.
|
---|
94 | \end{excdesc}
|
---|
95 |
|
---|
96 | \begin{excdesc}{herror}
|
---|
97 | This exception is raised for address-related errors, i.e. for
|
---|
98 | functions that use \var{h_errno} in the C API, including
|
---|
99 | \function{gethostbyname_ex()} and \function{gethostbyaddr()}.
|
---|
100 |
|
---|
101 | The accompanying value is a pair \code{(\var{h_errno}, \var{string})}
|
---|
102 | representing an error returned by a library call. \var{string}
|
---|
103 | represents the description of \var{h_errno}, as returned by
|
---|
104 | the \cfunction{hstrerror()} C function.
|
---|
105 | \end{excdesc}
|
---|
106 |
|
---|
107 | \begin{excdesc}{gaierror}
|
---|
108 | This exception is raised for address-related errors, for
|
---|
109 | \function{getaddrinfo()} and \function{getnameinfo()}.
|
---|
110 | The accompanying value is a pair \code{(\var{error}, \var{string})}
|
---|
111 | representing an error returned by a library call.
|
---|
112 | \var{string} represents the description of \var{error}, as returned
|
---|
113 | by the \cfunction{gai_strerror()} C function.
|
---|
114 | The \var{error} value will match one of the \constant{EAI_*} constants
|
---|
115 | defined in this module.
|
---|
116 | \end{excdesc}
|
---|
117 |
|
---|
118 | \begin{excdesc}{timeout}
|
---|
119 | This exception is raised when a timeout occurs on a socket which has
|
---|
120 | had timeouts enabled via a prior call to \method{settimeout()}. The
|
---|
121 | accompanying value is a string whose value is currently always ``timed
|
---|
122 | out''.
|
---|
123 | \versionadded{2.3}
|
---|
124 | \end{excdesc}
|
---|
125 |
|
---|
126 | \begin{datadesc}{AF_UNIX}
|
---|
127 | \dataline{AF_INET}
|
---|
128 | \dataline{AF_INET6}
|
---|
129 | These constants represent the address (and protocol) families,
|
---|
130 | used for the first argument to \function{socket()}. If the
|
---|
131 | \constant{AF_UNIX} constant is not defined then this protocol is
|
---|
132 | unsupported.
|
---|
133 | \end{datadesc}
|
---|
134 |
|
---|
135 | \begin{datadesc}{SOCK_STREAM}
|
---|
136 | \dataline{SOCK_DGRAM}
|
---|
137 | \dataline{SOCK_RAW}
|
---|
138 | \dataline{SOCK_RDM}
|
---|
139 | \dataline{SOCK_SEQPACKET}
|
---|
140 | These constants represent the socket types,
|
---|
141 | used for the second argument to \function{socket()}.
|
---|
142 | (Only \constant{SOCK_STREAM} and
|
---|
143 | \constant{SOCK_DGRAM} appear to be generally useful.)
|
---|
144 | \end{datadesc}
|
---|
145 |
|
---|
146 | \begin{datadesc}{SO_*}
|
---|
147 | \dataline{SOMAXCONN}
|
---|
148 | \dataline{MSG_*}
|
---|
149 | \dataline{SOL_*}
|
---|
150 | \dataline{IPPROTO_*}
|
---|
151 | \dataline{IPPORT_*}
|
---|
152 | \dataline{INADDR_*}
|
---|
153 | \dataline{IP_*}
|
---|
154 | \dataline{IPV6_*}
|
---|
155 | \dataline{EAI_*}
|
---|
156 | \dataline{AI_*}
|
---|
157 | \dataline{NI_*}
|
---|
158 | \dataline{TCP_*}
|
---|
159 | Many constants of these forms, documented in the \UNIX{} documentation on
|
---|
160 | sockets and/or the IP protocol, are also defined in the socket module.
|
---|
161 | They are generally used in arguments to the \method{setsockopt()} and
|
---|
162 | \method{getsockopt()} methods of socket objects. In most cases, only
|
---|
163 | those symbols that are defined in the \UNIX{} header files are defined;
|
---|
164 | for a few symbols, default values are provided.
|
---|
165 | \end{datadesc}
|
---|
166 |
|
---|
167 | \begin{datadesc}{has_ipv6}
|
---|
168 | This constant contains a boolean value which indicates if IPv6 is
|
---|
169 | supported on this platform.
|
---|
170 | \versionadded{2.3}
|
---|
171 | \end{datadesc}
|
---|
172 |
|
---|
173 | \begin{funcdesc}{getaddrinfo}{host, port\optional{, family\optional{,
|
---|
174 | socktype\optional{, proto\optional{,
|
---|
175 | flags}}}}}
|
---|
176 | Resolves the \var{host}/\var{port} argument, into a sequence of
|
---|
177 | 5-tuples that contain all the necessary argument for the sockets
|
---|
178 | manipulation. \var{host} is a domain name, a string representation of
|
---|
179 | IPv4/v6 address or \code{None}.
|
---|
180 | \var{port} is a string service name (like \code{'http'}), a numeric
|
---|
181 | port number or \code{None}.
|
---|
182 |
|
---|
183 | The rest of the arguments are optional and must be numeric if
|
---|
184 | specified. For \var{host} and \var{port}, by passing either an empty
|
---|
185 | string or \code{None}, you can pass \code{NULL} to the C API. The
|
---|
186 | \function{getaddrinfo()} function returns a list of 5-tuples with
|
---|
187 | the following structure:
|
---|
188 |
|
---|
189 | \code{(\var{family}, \var{socktype}, \var{proto}, \var{canonname},
|
---|
190 | \var{sockaddr})}
|
---|
191 |
|
---|
192 | \var{family}, \var{socktype}, \var{proto} are all integer and are meant to
|
---|
193 | be passed to the \function{socket()} function.
|
---|
194 | \var{canonname} is a string representing the canonical name of the \var{host}.
|
---|
195 | It can be a numeric IPv4/v6 address when \constant{AI_CANONNAME} is specified
|
---|
196 | for a numeric \var{host}.
|
---|
197 | \var{sockaddr} is a tuple describing a socket address, as described above.
|
---|
198 | See the source for the \refmodule{httplib} and other library modules
|
---|
199 | for a typical usage of the function.
|
---|
200 | \versionadded{2.2}
|
---|
201 | \end{funcdesc}
|
---|
202 |
|
---|
203 | \begin{funcdesc}{getfqdn}{\optional{name}}
|
---|
204 | Return a fully qualified domain name for \var{name}.
|
---|
205 | If \var{name} is omitted or empty, it is interpreted as the local
|
---|
206 | host. To find the fully qualified name, the hostname returned by
|
---|
207 | \function{gethostbyaddr()} is checked, then aliases for the host, if
|
---|
208 | available. The first name which includes a period is selected. In
|
---|
209 | case no fully qualified domain name is available, the hostname as
|
---|
210 | returned by \function{gethostname()} is returned.
|
---|
211 | \versionadded{2.0}
|
---|
212 | \end{funcdesc}
|
---|
213 |
|
---|
214 | \begin{funcdesc}{gethostbyname}{hostname}
|
---|
215 | Translate a host name to IPv4 address format. The IPv4 address is
|
---|
216 | returned as a string, such as \code{'100.50.200.5'}. If the host name
|
---|
217 | is an IPv4 address itself it is returned unchanged. See
|
---|
218 | \function{gethostbyname_ex()} for a more complete interface.
|
---|
219 | \function{gethostbyname()} does not support IPv6 name resolution, and
|
---|
220 | \function{getaddrinfo()} should be used instead for IPv4/v6 dual stack support.
|
---|
221 | \end{funcdesc}
|
---|
222 |
|
---|
223 | \begin{funcdesc}{gethostbyname_ex}{hostname}
|
---|
224 | Translate a host name to IPv4 address format, extended interface.
|
---|
225 | Return a triple \code{(\var{hostname}, \var{aliaslist},
|
---|
226 | \var{ipaddrlist})} where
|
---|
227 | \var{hostname} is the primary host name responding to the given
|
---|
228 | \var{ip_address}, \var{aliaslist} is a (possibly empty) list of
|
---|
229 | alternative host names for the same address, and \var{ipaddrlist} is
|
---|
230 | a list of IPv4 addresses for the same interface on the same
|
---|
231 | host (often but not always a single address).
|
---|
232 | \function{gethostbyname_ex()} does not support IPv6 name resolution, and
|
---|
233 | \function{getaddrinfo()} should be used instead for IPv4/v6 dual stack support.
|
---|
234 | \end{funcdesc}
|
---|
235 |
|
---|
236 | \begin{funcdesc}{gethostname}{}
|
---|
237 | Return a string containing the hostname of the machine where
|
---|
238 | the Python interpreter is currently executing.
|
---|
239 | If you want to know the current machine's IP address, you may want to use
|
---|
240 | \code{gethostbyname(gethostname())}.
|
---|
241 | This operation assumes that there is a valid address-to-host mapping for
|
---|
242 | the host, and the assumption does not always hold.
|
---|
243 | Note: \function{gethostname()} doesn't always return the fully qualified
|
---|
244 | domain name; use \code{gethostbyaddr(gethostname())}
|
---|
245 | (see below).
|
---|
246 | \end{funcdesc}
|
---|
247 |
|
---|
248 | \begin{funcdesc}{gethostbyaddr}{ip_address}
|
---|
249 | Return a triple \code{(\var{hostname}, \var{aliaslist},
|
---|
250 | \var{ipaddrlist})} where \var{hostname} is the primary host name
|
---|
251 | responding to the given \var{ip_address}, \var{aliaslist} is a
|
---|
252 | (possibly empty) list of alternative host names for the same address,
|
---|
253 | and \var{ipaddrlist} is a list of IPv4/v6 addresses for the same interface
|
---|
254 | on the same host (most likely containing only a single address).
|
---|
255 | To find the fully qualified domain name, use the function
|
---|
256 | \function{getfqdn()}.
|
---|
257 | \function{gethostbyaddr} supports both IPv4 and IPv6.
|
---|
258 | \end{funcdesc}
|
---|
259 |
|
---|
260 | \begin{funcdesc}{getnameinfo}{sockaddr, flags}
|
---|
261 | Translate a socket address \var{sockaddr} into a 2-tuple
|
---|
262 | \code{(\var{host}, \var{port})}.
|
---|
263 | Depending on the settings of \var{flags}, the result can contain a
|
---|
264 | fully-qualified domain name or numeric address representation in
|
---|
265 | \var{host}. Similarly, \var{port} can contain a string port name or a
|
---|
266 | numeric port number.
|
---|
267 | \versionadded{2.2}
|
---|
268 | \end{funcdesc}
|
---|
269 |
|
---|
270 | \begin{funcdesc}{getprotobyname}{protocolname}
|
---|
271 | Translate an Internet protocol name (for example, \code{'icmp'}) to a constant
|
---|
272 | suitable for passing as the (optional) third argument to the
|
---|
273 | \function{socket()} function. This is usually only needed for sockets
|
---|
274 | opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket
|
---|
275 | modes, the correct protocol is chosen automatically if the protocol is
|
---|
276 | omitted or zero.
|
---|
277 | \end{funcdesc}
|
---|
278 |
|
---|
279 | \begin{funcdesc}{getservbyname}{servicename\optional{, protocolname}}
|
---|
280 | Translate an Internet service name and protocol name to a port number
|
---|
281 | for that service. The optional protocol name, if given, should be
|
---|
282 | \code{'tcp'} or \code{'udp'}, otherwise any protocol will match.
|
---|
283 | \end{funcdesc}
|
---|
284 |
|
---|
285 | \begin{funcdesc}{getservbyport}{port\optional{, protocolname}}
|
---|
286 | Translate an Internet port number and protocol name to a service name
|
---|
287 | for that service. The optional protocol name, if given, should be
|
---|
288 | \code{'tcp'} or \code{'udp'}, otherwise any protocol will match.
|
---|
289 | \end{funcdesc}
|
---|
290 |
|
---|
291 | \begin{funcdesc}{socket}{\optional{family\optional{,
|
---|
292 | type\optional{, proto}}}}
|
---|
293 | Create a new socket using the given address family, socket type and
|
---|
294 | protocol number. The address family should be \constant{AF_INET} (the
|
---|
295 | default), \constant{AF_INET6} or \constant{AF_UNIX}. The socket type
|
---|
296 | should be \constant{SOCK_STREAM} (the default), \constant{SOCK_DGRAM}
|
---|
297 | or perhaps one of the other \samp{SOCK_} constants. The protocol
|
---|
298 | number is usually zero and may be omitted in that case.
|
---|
299 | \end{funcdesc}
|
---|
300 |
|
---|
301 | \begin{funcdesc}{ssl}{sock\optional{, keyfile, certfile}}
|
---|
302 | Initiate a SSL connection over the socket \var{sock}. \var{keyfile} is
|
---|
303 | the name of a PEM formatted file that contains your private
|
---|
304 | key. \var{certfile} is a PEM formatted certificate chain file. On
|
---|
305 | success, a new \class{SSLObject} is returned.
|
---|
306 |
|
---|
307 | \warning{This does not do any certificate verification!}
|
---|
308 | \end{funcdesc}
|
---|
309 |
|
---|
310 | \begin{funcdesc}{socketpair}{\optional{family\optional{, type\optional{, proto}}}}
|
---|
311 | Build a pair of connected socket objects using the given address
|
---|
312 | family, socket type, and protocol number. Address family, socket type,
|
---|
313 | and protocol number are as for the \function{socket()} function above.
|
---|
314 | The default family is \constant{AF_UNIX} if defined on the platform;
|
---|
315 | otherwise, the default is \constant{AF_INET}.
|
---|
316 | Availability: \UNIX. \versionadded{2.4}
|
---|
317 | \end{funcdesc}
|
---|
318 |
|
---|
319 | \begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}}
|
---|
320 | Duplicate the file descriptor \var{fd} (an integer as returned by a file
|
---|
321 | object's \method{fileno()} method) and build a socket object from the
|
---|
322 | result. Address family, socket type and protocol number are as for the
|
---|
323 | \function{socket()} function above.
|
---|
324 | The file descriptor should refer to a socket, but this is not
|
---|
325 | checked --- subsequent operations on the object may fail if the file
|
---|
326 | descriptor is invalid. This function is rarely needed, but can be
|
---|
327 | used to get or set socket options on a socket passed to a program as
|
---|
328 | standard input or output (such as a server started by the \UNIX{} inet
|
---|
329 | daemon). The socket is assumed to be in blocking mode.
|
---|
330 | Availability: \UNIX.
|
---|
331 | \end{funcdesc}
|
---|
332 |
|
---|
333 | \begin{funcdesc}{ntohl}{x}
|
---|
334 | Convert 32-bit integers from network to host byte order. On machines
|
---|
335 | where the host byte order is the same as network byte order, this is a
|
---|
336 | no-op; otherwise, it performs a 4-byte swap operation.
|
---|
337 | \end{funcdesc}
|
---|
338 |
|
---|
339 | \begin{funcdesc}{ntohs}{x}
|
---|
340 | Convert 16-bit integers from network to host byte order. On machines
|
---|
341 | where the host byte order is the same as network byte order, this is a
|
---|
342 | no-op; otherwise, it performs a 2-byte swap operation.
|
---|
343 | \end{funcdesc}
|
---|
344 |
|
---|
345 | \begin{funcdesc}{htonl}{x}
|
---|
346 | Convert 32-bit integers from host to network byte order. On machines
|
---|
347 | where the host byte order is the same as network byte order, this is a
|
---|
348 | no-op; otherwise, it performs a 4-byte swap operation.
|
---|
349 | \end{funcdesc}
|
---|
350 |
|
---|
351 | \begin{funcdesc}{htons}{x}
|
---|
352 | Convert 16-bit integers from host to network byte order. On machines
|
---|
353 | where the host byte order is the same as network byte order, this is a
|
---|
354 | no-op; otherwise, it performs a 2-byte swap operation.
|
---|
355 | \end{funcdesc}
|
---|
356 |
|
---|
357 | \begin{funcdesc}{inet_aton}{ip_string}
|
---|
358 | Convert an IPv4 address from dotted-quad string format (for example,
|
---|
359 | '123.45.67.89') to 32-bit packed binary format, as a string four
|
---|
360 | characters in length. This is useful when conversing with a program
|
---|
361 | that uses the standard C library and needs objects of type
|
---|
362 | \ctype{struct in_addr}, which is the C type for the 32-bit packed
|
---|
363 | binary this function returns.
|
---|
364 |
|
---|
365 | If the IPv4 address string passed to this function is invalid,
|
---|
366 | \exception{socket.error} will be raised. Note that exactly what is
|
---|
367 | valid depends on the underlying C implementation of
|
---|
368 | \cfunction{inet_aton()}.
|
---|
369 |
|
---|
370 | \function{inet_aton()} does not support IPv6, and
|
---|
371 | \function{getnameinfo()} should be used instead for IPv4/v6 dual stack
|
---|
372 | support.
|
---|
373 | \end{funcdesc}
|
---|
374 |
|
---|
375 | \begin{funcdesc}{inet_ntoa}{packed_ip}
|
---|
376 | Convert a 32-bit packed IPv4 address (a string four characters in
|
---|
377 | length) to its standard dotted-quad string representation (for
|
---|
378 | example, '123.45.67.89'). This is useful when conversing with a
|
---|
379 | program that uses the standard C library and needs objects of type
|
---|
380 | \ctype{struct in_addr}, which is the C type for the 32-bit packed
|
---|
381 | binary data this function takes as an argument.
|
---|
382 |
|
---|
383 | If the string passed to this function is not exactly 4 bytes in
|
---|
384 | length, \exception{socket.error} will be raised.
|
---|
385 | \function{inet_ntoa()} does not support IPv6, and
|
---|
386 | \function{getnameinfo()} should be used instead for IPv4/v6 dual stack
|
---|
387 | support.
|
---|
388 | \end{funcdesc}
|
---|
389 |
|
---|
390 | \begin{funcdesc}{inet_pton}{address_family, ip_string}
|
---|
391 | Convert an IP address from its family-specific string format to a packed,
|
---|
392 | binary format.
|
---|
393 | \function{inet_pton()} is useful when a library or network protocol calls for
|
---|
394 | an object of type \ctype{struct in_addr} (similar to \function{inet_aton()})
|
---|
395 | or \ctype{struct in6_addr}.
|
---|
396 |
|
---|
397 | Supported values for \var{address_family} are currently
|
---|
398 | \constant{AF_INET} and \constant{AF_INET6}.
|
---|
399 | If the IP address string \var{ip_string} is invalid,
|
---|
400 | \exception{socket.error} will be raised. Note that exactly what is valid
|
---|
401 | depends on both the value of \var{address_family} and the underlying
|
---|
402 | implementation of \cfunction{inet_pton()}.
|
---|
403 |
|
---|
404 | Availability: \UNIX{} (maybe not all platforms).
|
---|
405 | \versionadded{2.3}
|
---|
406 | \end{funcdesc}
|
---|
407 |
|
---|
408 | \begin{funcdesc}{inet_ntop}{address_family, packed_ip}
|
---|
409 | Convert a packed IP address (a string of some number of characters) to
|
---|
410 | its standard, family-specific string representation (for example,
|
---|
411 | \code{'7.10.0.5'} or \code{'5aef:2b::8'})
|
---|
412 | \function{inet_ntop()} is useful when a library or network protocol returns
|
---|
413 | an object of type \ctype{struct in_addr} (similar to \function{inet_ntoa()})
|
---|
414 | or \ctype{struct in6_addr}.
|
---|
415 |
|
---|
416 | Supported values for \var{address_family} are currently
|
---|
417 | \constant{AF_INET} and \constant{AF_INET6}.
|
---|
418 | If the string \var{packed_ip} is not the correct length for the
|
---|
419 | specified address family, \exception{ValueError} will be raised. A
|
---|
420 | \exception{socket.error} is raised for errors from the call to
|
---|
421 | \function{inet_ntop()}.
|
---|
422 |
|
---|
423 | Availability: \UNIX{} (maybe not all platforms).
|
---|
424 | \versionadded{2.3}
|
---|
425 | \end{funcdesc}
|
---|
426 |
|
---|
427 | \begin{funcdesc}{getdefaulttimeout}{}
|
---|
428 | Return the default timeout in floating seconds for new socket objects.
|
---|
429 | A value of \code{None} indicates that new socket objects have no timeout.
|
---|
430 | When the socket module is first imported, the default is \code{None}.
|
---|
431 | \versionadded{2.3}
|
---|
432 | \end{funcdesc}
|
---|
433 |
|
---|
434 | \begin{funcdesc}{setdefaulttimeout}{timeout}
|
---|
435 | Set the default timeout in floating seconds for new socket objects.
|
---|
436 | A value of \code{None} indicates that new socket objects have no timeout.
|
---|
437 | When the socket module is first imported, the default is \code{None}.
|
---|
438 | \versionadded{2.3}
|
---|
439 | \end{funcdesc}
|
---|
440 |
|
---|
441 | \begin{datadesc}{SocketType}
|
---|
442 | This is a Python type object that represents the socket object type.
|
---|
443 | It is the same as \code{type(socket(...))}.
|
---|
444 | \end{datadesc}
|
---|
445 |
|
---|
446 |
|
---|
447 | \begin{seealso}
|
---|
448 | \seemodule{SocketServer}{Classes that simplify writing network servers.}
|
---|
449 | \end{seealso}
|
---|
450 |
|
---|
451 |
|
---|
452 | \subsection{Socket Objects \label{socket-objects}}
|
---|
453 |
|
---|
454 | Socket objects have the following methods. Except for
|
---|
455 | \method{makefile()} these correspond to \UNIX{} system calls
|
---|
456 | applicable to sockets.
|
---|
457 |
|
---|
458 | \begin{methoddesc}[socket]{accept}{}
|
---|
459 | Accept a connection.
|
---|
460 | The socket must be bound to an address and listening for connections.
|
---|
461 | The return value is a pair \code{(\var{conn}, \var{address})}
|
---|
462 | where \var{conn} is a \emph{new} socket object usable to send and
|
---|
463 | receive data on the connection, and \var{address} is the address bound
|
---|
464 | to the socket on the other end of the connection.
|
---|
465 | \end{methoddesc}
|
---|
466 |
|
---|
467 | \begin{methoddesc}[socket]{bind}{address}
|
---|
468 | Bind the socket to \var{address}. The socket must not already be bound.
|
---|
469 | (The format of \var{address} depends on the address family --- see
|
---|
470 | above.) \note{This method has historically accepted a pair
|
---|
471 | of parameters for \constant{AF_INET} addresses instead of only a
|
---|
472 | tuple. This was never intentional and is no longer available in
|
---|
473 | Python 2.0 and later.}
|
---|
474 | \end{methoddesc}
|
---|
475 |
|
---|
476 | \begin{methoddesc}[socket]{close}{}
|
---|
477 | Close the socket. All future operations on the socket object will fail.
|
---|
478 | The remote end will receive no more data (after queued data is flushed).
|
---|
479 | Sockets are automatically closed when they are garbage-collected.
|
---|
480 | \end{methoddesc}
|
---|
481 |
|
---|
482 | \begin{methoddesc}[socket]{connect}{address}
|
---|
483 | Connect to a remote socket at \var{address}.
|
---|
484 | (The format of \var{address} depends on the address family --- see
|
---|
485 | above.) \note{This method has historically accepted a pair
|
---|
486 | of parameters for \constant{AF_INET} addresses instead of only a
|
---|
487 | tuple. This was never intentional and is no longer available in
|
---|
488 | Python 2.0 and later.}
|
---|
489 | \end{methoddesc}
|
---|
490 |
|
---|
491 | \begin{methoddesc}[socket]{connect_ex}{address}
|
---|
492 | Like \code{connect(\var{address})}, but return an error indicator
|
---|
493 | instead of raising an exception for errors returned by the C-level
|
---|
494 | \cfunction{connect()} call (other problems, such as ``host not found,''
|
---|
495 | can still raise exceptions). The error indicator is \code{0} if the
|
---|
496 | operation succeeded, otherwise the value of the \cdata{errno}
|
---|
497 | variable. This is useful to support, for example, asynchronous connects.
|
---|
498 | \note{This method has historically accepted a pair of
|
---|
499 | parameters for \constant{AF_INET} addresses instead of only a tuple.
|
---|
500 | This was never intentional and is no longer available in Python
|
---|
501 | 2.0 and later.}
|
---|
502 | \end{methoddesc}
|
---|
503 |
|
---|
504 | \begin{methoddesc}[socket]{fileno}{}
|
---|
505 | Return the socket's file descriptor (a small integer). This is useful
|
---|
506 | with \function{select.select()}.
|
---|
507 |
|
---|
508 | Under Windows the small integer returned by this method cannot be used where
|
---|
509 | a file descriptor can be used (such as \function{os.fdopen()}). \UNIX{} does
|
---|
510 | not have this limitation.
|
---|
511 | \end{methoddesc}
|
---|
512 |
|
---|
513 | \begin{methoddesc}[socket]{getpeername}{}
|
---|
514 | Return the remote address to which the socket is connected. This is
|
---|
515 | useful to find out the port number of a remote IPv4/v6 socket, for instance.
|
---|
516 | (The format of the address returned depends on the address family ---
|
---|
517 | see above.) On some systems this function is not supported.
|
---|
518 | \end{methoddesc}
|
---|
519 |
|
---|
520 | \begin{methoddesc}[socket]{getsockname}{}
|
---|
521 | Return the socket's own address. This is useful to find out the port
|
---|
522 | number of an IPv4/v6 socket, for instance.
|
---|
523 | (The format of the address returned depends on the address family ---
|
---|
524 | see above.)
|
---|
525 | \end{methoddesc}
|
---|
526 |
|
---|
527 | \begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}}
|
---|
528 | Return the value of the given socket option (see the \UNIX{} man page
|
---|
529 | \manpage{getsockopt}{2}). The needed symbolic constants
|
---|
530 | (\constant{SO_*} etc.) are defined in this module. If \var{buflen}
|
---|
531 | is absent, an integer option is assumed and its integer value
|
---|
532 | is returned by the function. If \var{buflen} is present, it specifies
|
---|
533 | the maximum length of the buffer used to receive the option in, and
|
---|
534 | this buffer is returned as a string. It is up to the caller to decode
|
---|
535 | the contents of the buffer (see the optional built-in module
|
---|
536 | \refmodule{struct} for a way to decode C structures encoded as strings).
|
---|
537 | \end{methoddesc}
|
---|
538 |
|
---|
539 | \begin{methoddesc}[socket]{listen}{backlog}
|
---|
540 | Listen for connections made to the socket. The \var{backlog} argument
|
---|
541 | specifies the maximum number of queued connections and should be at
|
---|
542 | least 1; the maximum value is system-dependent (usually 5).
|
---|
543 | \end{methoddesc}
|
---|
544 |
|
---|
545 | \begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}}
|
---|
546 | Return a \dfn{file object} associated with the socket. (File objects
|
---|
547 | are described in \ref{bltin-file-objects}, ``File Objects.'')
|
---|
548 | The file object references a \cfunction{dup()}ped version of the
|
---|
549 | socket file descriptor, so the file object and socket object may be
|
---|
550 | closed or garbage-collected independently.
|
---|
551 | The socket must be in blocking mode.
|
---|
552 | \index{I/O control!buffering}The optional \var{mode}
|
---|
553 | and \var{bufsize} arguments are interpreted the same way as by the
|
---|
554 | built-in \function{file()} function; see ``Built-in Functions''
|
---|
555 | (section \ref{built-in-funcs}) for more information.
|
---|
556 | \end{methoddesc}
|
---|
557 |
|
---|
558 | \begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}}
|
---|
559 | Receive data from the socket. The return value is a string representing
|
---|
560 | the data received. The maximum amount of data to be received
|
---|
561 | at once is specified by \var{bufsize}. See the \UNIX{} manual page
|
---|
562 | \manpage{recv}{2} for the meaning of the optional argument
|
---|
563 | \var{flags}; it defaults to zero.
|
---|
564 | \note{For best match with hardware and network realities, the value of
|
---|
565 | \var{bufsize} should be a relatively small power of 2, for example, 4096.}
|
---|
566 | \end{methoddesc}
|
---|
567 |
|
---|
568 | \begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}}
|
---|
569 | Receive data from the socket. The return value is a pair
|
---|
570 | \code{(\var{string}, \var{address})} where \var{string} is a string
|
---|
571 | representing the data received and \var{address} is the address of the
|
---|
572 | socket sending the data. The optional \var{flags} argument has the
|
---|
573 | same meaning as for \method{recv()} above.
|
---|
574 | (The format of \var{address} depends on the address family --- see above.)
|
---|
575 | \end{methoddesc}
|
---|
576 |
|
---|
577 | \begin{methoddesc}[socket]{send}{string\optional{, flags}}
|
---|
578 | Send data to the socket. The socket must be connected to a remote
|
---|
579 | socket. The optional \var{flags} argument has the same meaning as for
|
---|
580 | \method{recv()} above. Returns the number of bytes sent.
|
---|
581 | Applications are responsible for checking that all data has been sent;
|
---|
582 | if only some of the data was transmitted, the application needs to
|
---|
583 | attempt delivery of the remaining data.
|
---|
584 | \end{methoddesc}
|
---|
585 |
|
---|
586 | \begin{methoddesc}[socket]{sendall}{string\optional{, flags}}
|
---|
587 | Send data to the socket. The socket must be connected to a remote
|
---|
588 | socket. The optional \var{flags} argument has the same meaning as for
|
---|
589 | \method{recv()} above. Unlike \method{send()}, this method continues
|
---|
590 | to send data from \var{string} until either all data has been sent or
|
---|
591 | an error occurs. \code{None} is returned on success. On error, an
|
---|
592 | exception is raised, and there is no way to determine how much data,
|
---|
593 | if any, was successfully sent.
|
---|
594 | \end{methoddesc}
|
---|
595 |
|
---|
596 | \begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address}
|
---|
597 | Send data to the socket. The socket should not be connected to a
|
---|
598 | remote socket, since the destination socket is specified by
|
---|
599 | \var{address}. The optional \var{flags} argument has the same
|
---|
600 | meaning as for \method{recv()} above. Return the number of bytes sent.
|
---|
601 | (The format of \var{address} depends on the address family --- see above.)
|
---|
602 | \end{methoddesc}
|
---|
603 |
|
---|
604 | \begin{methoddesc}[socket]{setblocking}{flag}
|
---|
605 | Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
|
---|
606 | the socket is set to non-blocking, else to blocking mode. Initially
|
---|
607 | all sockets are in blocking mode. In non-blocking mode, if a
|
---|
608 | \method{recv()} call doesn't find any data, or if a
|
---|
609 | \method{send()} call can't immediately dispose of the data, a
|
---|
610 | \exception{error} exception is raised; in blocking mode, the calls
|
---|
611 | block until they can proceed.
|
---|
612 | \code{s.setblocking(0)} is equivalent to \code{s.settimeout(0)};
|
---|
613 | \code{s.setblocking(1)} is equivalent to \code{s.settimeout(None)}.
|
---|
614 | \end{methoddesc}
|
---|
615 |
|
---|
616 | \begin{methoddesc}[socket]{settimeout}{value}
|
---|
617 | Set a timeout on blocking socket operations. The \var{value} argument
|
---|
618 | can be a nonnegative float expressing seconds, or \code{None}.
|
---|
619 | If a float is
|
---|
620 | given, subsequent socket operations will raise an \exception{timeout}
|
---|
621 | exception if the timeout period \var{value} has elapsed before the
|
---|
622 | operation has completed. Setting a timeout of \code{None} disables
|
---|
623 | timeouts on socket operations.
|
---|
624 | \code{s.settimeout(0.0)} is equivalent to \code{s.setblocking(0)};
|
---|
625 | \code{s.settimeout(None)} is equivalent to \code{s.setblocking(1)}.
|
---|
626 | \versionadded{2.3}
|
---|
627 | \end{methoddesc}
|
---|
628 |
|
---|
629 | \begin{methoddesc}[socket]{gettimeout}{}
|
---|
630 | Return the timeout in floating seconds associated with socket
|
---|
631 | operations, or \code{None} if no timeout is set. This reflects
|
---|
632 | the last call to \method{setblocking()} or \method{settimeout()}.
|
---|
633 | \versionadded{2.3}
|
---|
634 | \end{methoddesc}
|
---|
635 |
|
---|
636 | Some notes on socket blocking and timeouts: A socket object can be in
|
---|
637 | one of three modes: blocking, non-blocking, or timeout. Sockets are
|
---|
638 | always created in blocking mode. In blocking mode, operations block
|
---|
639 | until complete. In non-blocking mode, operations fail (with an error
|
---|
640 | that is unfortunately system-dependent) if they cannot be completed
|
---|
641 | immediately. In timeout mode, operations fail if they cannot be
|
---|
642 | completed within the timeout specified for the socket. The
|
---|
643 | \method{setblocking()} method is simply a shorthand for certain
|
---|
644 | \method{settimeout()} calls.
|
---|
645 |
|
---|
646 | Timeout mode internally sets the socket in non-blocking mode. The
|
---|
647 | blocking and timeout modes are shared between file descriptors and
|
---|
648 | socket objects that refer to the same network endpoint. A consequence
|
---|
649 | of this is that file objects returned by the \method{makefile()}
|
---|
650 | method must only be used when the socket is in blocking mode; in
|
---|
651 | timeout or non-blocking mode file operations that cannot be completed
|
---|
652 | immediately will fail.
|
---|
653 |
|
---|
654 | Note that the \method{connect()} operation is subject to the timeout
|
---|
655 | setting, and in general it is recommended to call
|
---|
656 | \method{settimeout()} before calling \method{connect()}.
|
---|
657 |
|
---|
658 | \begin{methoddesc}[socket]{setsockopt}{level, optname, value}
|
---|
659 | Set the value of the given socket option (see the \UNIX{} manual page
|
---|
660 | \manpage{setsockopt}{2}). The needed symbolic constants are defined in
|
---|
661 | the \module{socket} module (\constant{SO_*} etc.). The value can be an
|
---|
662 | integer or a string representing a buffer. In the latter case it is
|
---|
663 | up to the caller to ensure that the string contains the proper bits
|
---|
664 | (see the optional built-in module
|
---|
665 | \refmodule{struct}\refbimodindex{struct} for a way to encode C
|
---|
666 | structures as strings).
|
---|
667 | \end{methoddesc}
|
---|
668 |
|
---|
669 | \begin{methoddesc}[socket]{shutdown}{how}
|
---|
670 | Shut down one or both halves of the connection. If \var{how} is
|
---|
671 | \constant{SHUT_RD}, further receives are disallowed. If \var{how} is \constant{SHUT_WR},
|
---|
672 | further sends are disallowed. If \var{how} is \constant{SHUT_RDWR}, further sends
|
---|
673 | and receives are disallowed.
|
---|
674 | \end{methoddesc}
|
---|
675 |
|
---|
676 | Note that there are no methods \method{read()} or \method{write()};
|
---|
677 | use \method{recv()} and \method{send()} without \var{flags} argument
|
---|
678 | instead.
|
---|
679 |
|
---|
680 |
|
---|
681 | Socket objects also have these (read-only) attributes that correspond
|
---|
682 | to the values given to the \class{socket} constructor.
|
---|
683 |
|
---|
684 | \begin{memberdesc}[socket]{family}
|
---|
685 | The socket family.
|
---|
686 | \versionadded{2.5}
|
---|
687 | \end{memberdesc}
|
---|
688 |
|
---|
689 | \begin{memberdesc}[socket]{type}
|
---|
690 | The socket type.
|
---|
691 | \versionadded{2.5}
|
---|
692 | \end{memberdesc}
|
---|
693 |
|
---|
694 | \begin{memberdesc}[socket]{proto}
|
---|
695 | The socket protocol.
|
---|
696 | \versionadded{2.5}
|
---|
697 | \end{memberdesc}
|
---|
698 |
|
---|
699 |
|
---|
700 | \subsection{SSL Objects \label{ssl-objects}}
|
---|
701 |
|
---|
702 | SSL objects have the following methods.
|
---|
703 |
|
---|
704 | \begin{methoddesc}{write}{s}
|
---|
705 | Writes the string \var{s} to the on the object's SSL connection.
|
---|
706 | The return value is the number of bytes written.
|
---|
707 | \end{methoddesc}
|
---|
708 |
|
---|
709 | \begin{methoddesc}{read}{\optional{n}}
|
---|
710 | If \var{n} is provided, read \var{n} bytes from the SSL connection, otherwise
|
---|
711 | read until EOF. The return value is a string of the bytes read.
|
---|
712 | \end{methoddesc}
|
---|
713 |
|
---|
714 | \begin{methoddesc}{server}{}
|
---|
715 | Returns a string containing the ASN.1 distinguished name identifying the
|
---|
716 | server's certificate. (See below for an example
|
---|
717 | showing what distinguished names look like.)
|
---|
718 | \end{methoddesc}
|
---|
719 |
|
---|
720 | \begin{methoddesc}{issuer}{}
|
---|
721 | Returns a string containing the ASN.1 distinguished name identifying the
|
---|
722 | issuer of the server's certificate.
|
---|
723 | \end{methoddesc}
|
---|
724 |
|
---|
725 | \subsection{Example \label{socket-example}}
|
---|
726 |
|
---|
727 | Here are four minimal example programs using the TCP/IP protocol:\ a
|
---|
728 | server that echoes all data that it receives back (servicing only one
|
---|
729 | client), and a client using it. Note that a server must perform the
|
---|
730 | sequence \function{socket()}, \method{bind()}, \method{listen()},
|
---|
731 | \method{accept()} (possibly repeating the \method{accept()} to service
|
---|
732 | more than one client), while a client only needs the sequence
|
---|
733 | \function{socket()}, \method{connect()}. Also note that the server
|
---|
734 | does not \method{send()}/\method{recv()} on the
|
---|
735 | socket it is listening on but on the new socket returned by
|
---|
736 | \method{accept()}.
|
---|
737 |
|
---|
738 | The first two examples support IPv4 only.
|
---|
739 |
|
---|
740 | \begin{verbatim}
|
---|
741 | # Echo server program
|
---|
742 | import socket
|
---|
743 |
|
---|
744 | HOST = '' # Symbolic name meaning the local host
|
---|
745 | PORT = 50007 # Arbitrary non-privileged port
|
---|
746 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
---|
747 | s.bind((HOST, PORT))
|
---|
748 | s.listen(1)
|
---|
749 | conn, addr = s.accept()
|
---|
750 | print 'Connected by', addr
|
---|
751 | while 1:
|
---|
752 | data = conn.recv(1024)
|
---|
753 | if not data: break
|
---|
754 | conn.send(data)
|
---|
755 | conn.close()
|
---|
756 | \end{verbatim}
|
---|
757 |
|
---|
758 | \begin{verbatim}
|
---|
759 | # Echo client program
|
---|
760 | import socket
|
---|
761 |
|
---|
762 | HOST = 'daring.cwi.nl' # The remote host
|
---|
763 | PORT = 50007 # The same port as used by the server
|
---|
764 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
---|
765 | s.connect((HOST, PORT))
|
---|
766 | s.send('Hello, world')
|
---|
767 | data = s.recv(1024)
|
---|
768 | s.close()
|
---|
769 | print 'Received', repr(data)
|
---|
770 | \end{verbatim}
|
---|
771 |
|
---|
772 | The next two examples are identical to the above two, but support both
|
---|
773 | IPv4 and IPv6.
|
---|
774 | The server side will listen to the first address family available
|
---|
775 | (it should listen to both instead).
|
---|
776 | On most of IPv6-ready systems, IPv6 will take precedence
|
---|
777 | and the server may not accept IPv4 traffic.
|
---|
778 | The client side will try to connect to the all addresses returned as a result
|
---|
779 | of the name resolution, and sends traffic to the first one connected
|
---|
780 | successfully.
|
---|
781 |
|
---|
782 | \begin{verbatim}
|
---|
783 | # Echo server program
|
---|
784 | import socket
|
---|
785 | import sys
|
---|
786 |
|
---|
787 | HOST = '' # Symbolic name meaning the local host
|
---|
788 | PORT = 50007 # Arbitrary non-privileged port
|
---|
789 | s = None
|
---|
790 | for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
|
---|
791 | af, socktype, proto, canonname, sa = res
|
---|
792 | try:
|
---|
793 | s = socket.socket(af, socktype, proto)
|
---|
794 | except socket.error, msg:
|
---|
795 | s = None
|
---|
796 | continue
|
---|
797 | try:
|
---|
798 | s.bind(sa)
|
---|
799 | s.listen(1)
|
---|
800 | except socket.error, msg:
|
---|
801 | s.close()
|
---|
802 | s = None
|
---|
803 | continue
|
---|
804 | break
|
---|
805 | if s is None:
|
---|
806 | print 'could not open socket'
|
---|
807 | sys.exit(1)
|
---|
808 | conn, addr = s.accept()
|
---|
809 | print 'Connected by', addr
|
---|
810 | while 1:
|
---|
811 | data = conn.recv(1024)
|
---|
812 | if not data: break
|
---|
813 | conn.send(data)
|
---|
814 | conn.close()
|
---|
815 | \end{verbatim}
|
---|
816 |
|
---|
817 | \begin{verbatim}
|
---|
818 | # Echo client program
|
---|
819 | import socket
|
---|
820 | import sys
|
---|
821 |
|
---|
822 | HOST = 'daring.cwi.nl' # The remote host
|
---|
823 | PORT = 50007 # The same port as used by the server
|
---|
824 | s = None
|
---|
825 | for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
|
---|
826 | af, socktype, proto, canonname, sa = res
|
---|
827 | try:
|
---|
828 | s = socket.socket(af, socktype, proto)
|
---|
829 | except socket.error, msg:
|
---|
830 | s = None
|
---|
831 | continue
|
---|
832 | try:
|
---|
833 | s.connect(sa)
|
---|
834 | except socket.error, msg:
|
---|
835 | s.close()
|
---|
836 | s = None
|
---|
837 | continue
|
---|
838 | break
|
---|
839 | if s is None:
|
---|
840 | print 'could not open socket'
|
---|
841 | sys.exit(1)
|
---|
842 | s.send('Hello, world')
|
---|
843 | data = s.recv(1024)
|
---|
844 | s.close()
|
---|
845 | print 'Received', repr(data)
|
---|
846 | \end{verbatim}
|
---|
847 |
|
---|
848 | This example connects to an SSL server, prints the
|
---|
849 | server and issuer's distinguished names, sends some bytes,
|
---|
850 | and reads part of the response:
|
---|
851 |
|
---|
852 | \begin{verbatim}
|
---|
853 | import socket
|
---|
854 |
|
---|
855 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
---|
856 | s.connect(('www.verisign.com', 443))
|
---|
857 |
|
---|
858 | ssl_sock = socket.ssl(s)
|
---|
859 |
|
---|
860 | print repr(ssl_sock.server())
|
---|
861 | print repr(ssl_sock.issuer())
|
---|
862 |
|
---|
863 | # Set a simple HTTP request -- use httplib in actual code.
|
---|
864 | ssl_sock.write("""GET / HTTP/1.0\r
|
---|
865 | Host: www.verisign.com\r\n\r\n""")
|
---|
866 |
|
---|
867 | # Read a chunk of data. Will not necessarily
|
---|
868 | # read all the data returned by the server.
|
---|
869 | data = ssl_sock.read()
|
---|
870 |
|
---|
871 | # Note that you need to close the underlying socket, not the SSL object.
|
---|
872 | del ssl_sock
|
---|
873 | s.close()
|
---|
874 | \end{verbatim}
|
---|
875 |
|
---|
876 | At this writing, this SSL example prints the following output (line
|
---|
877 | breaks inserted for readability):
|
---|
878 |
|
---|
879 | \begin{verbatim}
|
---|
880 | '/C=US/ST=California/L=Mountain View/
|
---|
881 | O=VeriSign, Inc./OU=Production Services/
|
---|
882 | OU=Terms of use at www.verisign.com/rpa (c)00/
|
---|
883 | CN=www.verisign.com'
|
---|
884 | '/O=VeriSign Trust Network/OU=VeriSign, Inc./
|
---|
885 | OU=VeriSign International Server CA - Class 3/
|
---|
886 | OU=www.verisign.com/CPS Incorp.by Ref. LIABILITY LTD.(c)97 VeriSign'
|
---|
887 | \end{verbatim}
|
---|