annotate Doc/lib/libsocket.tex @ 13163:a09e8635534d legacy-trunk

Don't try to guess the name of a .def file -- if one is supplied, use it, otherwise just generate an '/export:' option.
author Greg Ward <gward@python.net>
date Thu, 29 Jun 2000 23:09:20 +0000
parents 8bcd14313c11
children d37374552a15
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8918
acfffe14628e New section header style.
Fred Drake <fdrake@acm.org>
parents: 8880
diff changeset
1 \section{\module{socket} ---
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
2 Low-level networking interface}
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
3
8716
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
4 \declaremodule{builtin}{socket}
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
5 \modulesynopsis{Low-level networking interface.}
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
6
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
7096
5bb884527446 Consistency: "{\bf ...}" ==> "\strong{...}"
Fred Drake <fdrake@acm.org>
parents: 6804
diff changeset
8 This module provides access to the BSD \emph{socket} interface.
12223
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 11463
diff changeset
9 It is available on all modern \UNIX{} systems, Windows, MacOS, BeOS,
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 11463
diff changeset
10 OS/2, and probably additional platforms.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 For an introduction to socket programming (in C), see the following
11463
17e12305973e Use \citetitle and \programopt as appropriate.
Fred Drake <fdrake@acm.org>
parents: 11291
diff changeset
13 papers: \citetitle{An Introductory 4.3BSD Interprocess Communication
17e12305973e Use \citetitle and \programopt as appropriate.
Fred Drake <fdrake@acm.org>
parents: 11291
diff changeset
14 Tutorial}, by Stuart Sechrest and \citetitle{An Advanced 4.3BSD
17e12305973e Use \citetitle and \programopt as appropriate.
Fred Drake <fdrake@acm.org>
parents: 11291
diff changeset
15 Interprocess Communication Tutorial}, by Samuel J. Leffler et al,
17e12305973e Use \citetitle and \programopt as appropriate.
Fred Drake <fdrake@acm.org>
parents: 11291
diff changeset
16 both in the \citetitle{\UNIX{} Programmer's Manual, Supplementary Documents 1}
12223
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 11463
diff changeset
17 (sections PS1:7 and PS1:8). The platform-specific reference material
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 11463
diff changeset
18 for the various socket-related system calls are also a valuable source
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 11463
diff changeset
19 of information on the details of socket semantics. For \UNIX, refer
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 11463
diff changeset
20 to the manual pages; for Windows, see the WinSock (or Winsock 2)
e82e82b3b9ad Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents: 11463
diff changeset
21 specification.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 The Python interface is a straightforward transliteration of the
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 \UNIX{} system call and library interface for sockets to Python's
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
25 object-oriented style: the \function{socket()} function returns a
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
26 \dfn{socket object}\obindex{socket} whose methods implement the
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
27 various socket system calls. Parameter types are somewhat
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
28 higher-level than in the C interface: as with \method{read()} and
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
29 \method{write()} operations on Python files, buffer allocation on
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
30 receive operations is automatic, and buffer length is implicit on send
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
31 operations.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 Socket addresses are represented as a single string for the
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
34 \constant{AF_UNIX} address family and as a pair
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
35 \code{(\var{host}, \var{port})} for the \constant{AF_INET} address
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
36 family, where \var{host} is a string representing
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 either a hostname in Internet domain notation like
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 \code{'daring.cwi.nl'} or an IP address like \code{'100.50.200.5'},
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 and \var{port} is an integral port number. Other address families are
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 currently not supported. The address format required by a particular
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 socket object is automatically selected based on the address family
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 specified when the socket object was created.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
5461
0ded63bcf5de Document "" and "<broadcast>".
Guido van Rossum <guido@python.org>
parents: 4929
diff changeset
44 For IP addresses, two special forms are accepted instead of a host
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
45 address: the empty string represents \constant{INADDR_ANY}, and the string
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
46 \code{'<broadcast>'} represents \constant{INADDR_BROADCAST}.
5461
0ded63bcf5de Document "" and "<broadcast>".
Guido van Rossum <guido@python.org>
parents: 4929
diff changeset
47
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 All errors raise exceptions. The normal exceptions for invalid
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 argument types and out-of-memory conditions can be raised; errors
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
50 related to socket or address semantics raise the error
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
51 \exception{socket.error}.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
53 Non-blocking mode is supported through the
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
54 \method{setblocking()} method.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
56 The module \module{socket} exports the following constants and functions:
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
58
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 \begin{excdesc}{error}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 This exception is raised for socket- or address-related errors.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 The accompanying value is either a string telling what went wrong or a
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 pair \code{(\var{errno}, \var{string})}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 representing an error returned by a system
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
64 call, similar to the value accompanying \exception{os.error}.
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
65 See the module \refmodule{errno}\refbimodindex{errno}, which contains
7200
98a9da520f1d Add ref to errno module, and change ref to posix.error to os.error.
Guido van Rossum <guido@python.org>
parents: 7135
diff changeset
66 names for the error codes defined by the underlying operating system.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 \end{excdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 \begin{datadesc}{AF_UNIX}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 \dataline{AF_INET}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 These constants represent the address (and protocol) families,
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
72 used for the first argument to \function{socket()}. If the
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
73 \constant{AF_UNIX} constant is not defined then this protocol is
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
74 unsupported.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 \end{datadesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 \begin{datadesc}{SOCK_STREAM}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 \dataline{SOCK_DGRAM}
1759
4d421f9e8373 * Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents: 1750
diff changeset
79 \dataline{SOCK_RAW}
4d421f9e8373 * Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents: 1750
diff changeset
80 \dataline{SOCK_RDM}
4d421f9e8373 * Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents: 1750
diff changeset
81 \dataline{SOCK_SEQPACKET}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 These constants represent the socket types,
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
83 used for the second argument to \function{socket()}.
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
84 (Only \constant{SOCK_STREAM} and
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
85 \constant{SOCK_DGRAM} appear to be generally useful.)
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 \end{datadesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
2354
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
88 \begin{datadesc}{SO_*}
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
89 \dataline{SOMAXCONN}
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
90 \dataline{MSG_*}
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
91 \dataline{SOL_*}
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
92 \dataline{IPPROTO_*}
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
93 \dataline{IPPORT_*}
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
94 \dataline{INADDR_*}
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
95 \dataline{IP_*}
2527
815dfaa6a778 small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
96 Many constants of these forms, documented in the \UNIX{} documentation on
2354
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
97 sockets and/or the IP protocol, are also defined in the socket module.
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
98 They are generally used in arguments to the \method{setsockopt()} and
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
99 \method{getsockopt()} methods of socket objects. In most cases, only
2527
815dfaa6a778 small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
100 those symbols that are defined in the \UNIX{} header files are defined;
2354
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
101 for a few symbols, default values are provided.
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
102 \end{datadesc}
fbaf54169711 added lots of constants
Guido van Rossum <guido@python.org>
parents: 2064
diff changeset
103
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 \begin{funcdesc}{gethostbyname}{hostname}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 Translate a host name to IP address format. The IP address is
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 returned as a string, e.g., \code{'100.50.200.5'}. If the host name
8880
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
107 is an IP address itself it is returned unchanged. See
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
108 \function{gethostbyname_ex()} for a more complete interface.
8880
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
109 \end{funcdesc}
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
110
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
111 \begin{funcdesc}{gethostbyname_ex}{hostname}
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
112 Translate a host name to IP address format, extended interface.
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
113 Return a triple \code{(hostname, aliaslist, ipaddrlist)} where
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
114 \code{hostname} is the primary host name responding to the given
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
115 \var{ip_address}, \code{aliaslist} is a (possibly empty) list of
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
116 alternative host names for the same address, and \code{ipaddrlist} is
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
117 a list of IP addresses for the same interface on the same
a9eb265d2f57 Document gethostbyname_ex().
Guido van Rossum <guido@python.org>
parents: 8716
diff changeset
118 host (often but not always a single address).
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
1759
4d421f9e8373 * Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents: 1750
diff changeset
121 \begin{funcdesc}{gethostname}{}
1767
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1759
diff changeset
122 Return a string containing the hostname of the machine where
907605320a5f Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents: 1759
diff changeset
123 the Python interpreter is currently executing. If you want to know the
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
124 current machine's IP address, use \code{gethostbyname(gethostname())}.
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
125 Note: \function{gethostname()} doesn't always return the fully qualified
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
126 domain name; use \code{gethostbyaddr(gethostname())}
4929
5126d4b819e9 Added warning that gethostname() doesn't always return fqdn, and show
Guido van Rossum <guido@python.org>
parents: 4882
diff changeset
127 (see below).
2064
280f31e6c653 copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1871
diff changeset
128 \end{funcdesc}
280f31e6c653 copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1871
diff changeset
129
280f31e6c653 copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1871
diff changeset
130 \begin{funcdesc}{gethostbyaddr}{ip_address}
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
131 Return a triple \code{(\var{hostname}, \var{aliaslist},
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
132 \var{ipaddrlist})} where \var{hostname} is the primary host name
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
133 responding to the given \var{ip_address}, \var{aliaslist} is a
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
134 (possibly empty) list of alternative host names for the same address,
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
135 and \var{ipaddrlist} is a list of IP addresses for the same interface
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
136 on the same host (most likely containing only a single address).
4929
5126d4b819e9 Added warning that gethostname() doesn't always return fqdn, and show
Guido van Rossum <guido@python.org>
parents: 4882
diff changeset
137 To find the fully qualified domain name, check \var{hostname} and the
5126d4b819e9 Added warning that gethostname() doesn't always return fqdn, and show
Guido van Rossum <guido@python.org>
parents: 4882
diff changeset
138 items of \var{aliaslist} for an entry containing at least one period.
1759
4d421f9e8373 * Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents: 1750
diff changeset
139 \end{funcdesc}
4d421f9e8373 * Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents: 1750
diff changeset
140
4784
e43362910a5e Documented getprotobyname() interface.
Guido van Rossum <guido@python.org>
parents: 4576
diff changeset
141 \begin{funcdesc}{getprotobyname}{protocolname}
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
142 Translate an Internet protocol name (e.g.\ \code{'icmp'}) to a constant
4784
e43362910a5e Documented getprotobyname() interface.
Guido van Rossum <guido@python.org>
parents: 4576
diff changeset
143 suitable for passing as the (optional) third argument to the
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
144 \function{socket()} function. This is usually only needed for sockets
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
145 opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
146 modes, the correct protocol is chosen automatically if the protocol is
4784
e43362910a5e Documented getprotobyname() interface.
Guido van Rossum <guido@python.org>
parents: 4576
diff changeset
147 omitted or zero.
e43362910a5e Documented getprotobyname() interface.
Guido van Rossum <guido@python.org>
parents: 4576
diff changeset
148 \end{funcdesc}
e43362910a5e Documented getprotobyname() interface.
Guido van Rossum <guido@python.org>
parents: 4576
diff changeset
149
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
150 \begin{funcdesc}{getservbyname}{servicename, protocolname}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 Translate an Internet service name and protocol name to a port number
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 for that service. The protocol name should be \code{'tcp'} or
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 \code{'udp'}.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
156 \begin{funcdesc}{socket}{family, type\optional{, proto}}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 Create a new socket using the given address family, socket type and
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
158 protocol number. The address family should be \constant{AF_INET} or
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
159 \constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM},
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
160 \constant{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 The protocol number is usually zero and may be omitted in that case.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
164 \begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 Build a socket object from an existing file descriptor (an integer as
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
166 returned by a file object's \method{fileno()} method). Address family,
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
167 socket type and protocol number are as for the \function{socket()} function
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 above. The file descriptor should refer to a socket, but this is not
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 checked --- subsequent operations on the object may fail if the file
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 descriptor is invalid. This function is rarely needed, but can be
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 used to get or set socket options on a socket passed to a program as
2527
815dfaa6a778 small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
172 standard input or output (e.g.\ a server started by the \UNIX{} inet
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 daemon).
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 \end{funcdesc}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
4576
c3b0895c90fb Added [nh]to[hn][sl]().
Guido van Rossum <guido@python.org>
parents: 2576
diff changeset
176 \begin{funcdesc}{ntohl}{x}
6783
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
177 Convert 32-bit integers from network to host byte order. On machines
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
178 where the host byte order is the same as network byte order, this is a
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
179 no-op; otherwise, it performs a 4-byte swap operation.
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
180 \end{funcdesc}
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
181
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
182 \begin{funcdesc}{ntohs}{x}
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
183 Convert 16-bit integers from network to host byte order. On machines
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
184 where the host byte order is the same as network byte order, this is a
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
185 no-op; otherwise, it performs a 2-byte swap operation.
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
186 \end{funcdesc}
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
187
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
188 \begin{funcdesc}{htonl}{x}
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
189 Convert 32-bit integers from host to network byte order. On machines
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
190 where the host byte order is the same as network byte order, this is a
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
191 no-op; otherwise, it performs a 4-byte swap operation.
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
192 \end{funcdesc}
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
193
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
194 \begin{funcdesc}{htons}{x}
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
195 Convert 16-bit integers from host to network byte order. On machines
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
196 where the host byte order is the same as network byte order, this is a
c18501770895 Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents: 6587
diff changeset
197 no-op; otherwise, it performs a 2-byte swap operation.
4576
c3b0895c90fb Added [nh]to[hn][sl]().
Guido van Rossum <guido@python.org>
parents: 2576
diff changeset
198 \end{funcdesc}
c3b0895c90fb Added [nh]to[hn][sl]().
Guido van Rossum <guido@python.org>
parents: 2576
diff changeset
199
11291
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
200 \begin{funcdesc}{inet_aton}{ip_string}
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
201 Convert an IP address from dotted-quad string format
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
202 (e.g.\ '123.45.67.89') to 32-bit packed binary format, as a string four
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
203 characters in length.
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
204
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
205 Useful when conversing with a program that uses the standard C library
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
206 and needs objects of type \ctype{struct in_addr}, which is the C type
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
207 for the 32-bit packed binary this function returns.
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
208
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
209 If the IP address string passed to this function is invalid,
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
210 \exception{socket.error} will be raised. Note that exactly what is
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
211 valid depends on the underlying C implementation of
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
212 \cfunction{inet_aton()}.
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
213 \end{funcdesc}
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
214
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
215 \begin{funcdesc}{inet_ntoa}{packed_ip}
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
216 Convert a 32-bit packed IP address (a string four characters in
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
217 length) to its standard dotted-quad string representation
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
218 (e.g. '123.45.67.89').
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
219
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
220 Useful when conversing with a program that uses the standard C library
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
221 and needs objects of type \ctype{struct in_addr}, which is the C type
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
222 for the 32-bit packed binary this function takes as an argument.
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
223
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
224 If the string passed to this function is not exactly 4 bytes in
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
225 length, \exception{socket.error} will be raised.
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
226 \end{funcdesc}
d970af0cece9 Added documentation for inet_aton() and inet_ntoa(), from Ben
Fred Drake <fdrake@acm.org>
parents: 11178
diff changeset
227
6482
d311b68754c1 socket_type ==> SocketType
Fred Drake <fdrake@acm.org>
parents: 5818
diff changeset
228 \begin{datadesc}{SocketType}
5574
84d7cc5e63cd Added description of socket_type.
Guido van Rossum <guido@python.org>
parents: 5461
diff changeset
229 This is a Python type object that represents the socket object type.
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
230 It is the same as \code{type(socket(...))}.
5574
84d7cc5e63cd Added description of socket_type.
Guido van Rossum <guido@python.org>
parents: 5461
diff changeset
231 \end{datadesc}
84d7cc5e63cd Added description of socket_type.
Guido van Rossum <guido@python.org>
parents: 5461
diff changeset
232
11178
245d2b605036 Add index entry "I/O control, buffering" for makefile().
Fred Drake <fdrake@acm.org>
parents: 10836
diff changeset
233 \subsection{Socket Objects \label{socket-objects}}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 Socket objects have the following methods. Except for
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
236 \method{makefile()} these correspond to \UNIX{} system calls
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
237 applicable to sockets.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
239 \begin{methoddesc}[socket]{accept}{}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 Accept a connection.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 The socket must be bound to an address and listening for connections.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 The return value is a pair \code{(\var{conn}, \var{address})}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 where \var{conn} is a \emph{new} socket object usable to send and
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 receive data on the connection, and \var{address} is the address bound
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 to the socket on the other end of the connection.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
246 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
248 \begin{methoddesc}[socket]{bind}{address}
1682
2bcddc905da0 clarify the meaning of address for those methods that or return addresses
Guido van Rossum <guido@python.org>
parents: 1567
diff changeset
249 Bind the socket to \var{address}. The socket must not already be bound.
12230
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
250 (The format of \var{address} depends on the address family --- see
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
251 above.) \strong{Note:} This method has historically accepted a pair
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
252 of parameters for \constant{AF_INET} addresses instead of only a
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
253 tuple. This was never intentional and will no longer be available in
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
254 Python 1.7.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
255 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
256
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
257 \begin{methoddesc}[socket]{close}{}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 Close the socket. All future operations on the socket object will fail.
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 The remote end will receive no more data (after queued data is flushed).
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 Sockets are automatically closed when they are garbage-collected.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
261 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
262
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
263 \begin{methoddesc}[socket]{connect}{address}
1682
2bcddc905da0 clarify the meaning of address for those methods that or return addresses
Guido van Rossum <guido@python.org>
parents: 1567
diff changeset
264 Connect to a remote socket at \var{address}.
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
265 (The format of \var{address} depends on the address family --- see
12230
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
266 above.) \strong{Note:} This method has historically accepted a pair
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
267 of parameters for \constant{AF_INET} addresses instead of only a
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
268 tuple. This was never intentional and will no longer be available in
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
269 Python 1.7.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
270 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
272 \begin{methoddesc}[socket]{connect_ex}{address}
6804
e224c41bdac6 Fix a little mess (Likecode{...} -> Like \code{...}) in recently added text.
Guido van Rossum <guido@python.org>
parents: 6783
diff changeset
273 Like \code{connect(\var{address})}, but return an error indicator
10836
efd1573a4b03 Updated connect_ex() documentation based on comments in the newgroup
Fred Drake <fdrake@acm.org>
parents: 10655
diff changeset
274 instead of raising an exception for errors returned by the C-level
efd1573a4b03 Updated connect_ex() documentation based on comments in the newgroup
Fred Drake <fdrake@acm.org>
parents: 10655
diff changeset
275 \cfunction{connect()} call (other problems, such as ``host not found,''
efd1573a4b03 Updated connect_ex() documentation based on comments in the newgroup
Fred Drake <fdrake@acm.org>
parents: 10655
diff changeset
276 can still raise exceptions). The error indicator is \code{0} if the
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
277 operation succeeded, otherwise the value of the \cdata{errno}
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
278 variable. This is useful, e.g., for asynchronous connects.
12230
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
279 \strong{Note:} This method has historically accepted a pair of
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
280 parameters for \constant{AF_INET} addresses instead of only a tuple.
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
281 This was never intentional and will no longer be available in Python
7bf8b10ce2ab Added notes about the eventual disappearance of two-parameter bind(),
Fred Drake <fdrake@acm.org>
parents: 12223
diff changeset
282 1.7.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
283 \end{methoddesc}
6587
e0e667bbc971 Document s.connect_ex(), which does not raise an exception on error.
Guido van Rossum <guido@python.org>
parents: 6482
diff changeset
284
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
285 \begin{methoddesc}[socket]{fileno}{}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 Return the socket's file descriptor (a small integer). This is useful
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
287 with \function{select.select()}.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
288 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
289
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
290 \begin{methoddesc}[socket]{getpeername}{}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 Return the remote address to which the socket is connected. This is
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 useful to find out the port number of a remote IP socket, for instance.
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2447
diff changeset
293 (The format of the address returned depends on the address family ---
1759
4d421f9e8373 * Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents: 1750
diff changeset
294 see above.) On some systems this function is not supported.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
295 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
297 \begin{methoddesc}[socket]{getsockname}{}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 Return the socket's own address. This is useful to find out the port
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 number of an IP socket, for instance.
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2447
diff changeset
300 (The format of the address returned depends on the address family ---
1682
2bcddc905da0 clarify the meaning of address for those methods that or return addresses
Guido van Rossum <guido@python.org>
parents: 1567
diff changeset
301 see above.)
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
302 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
303
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
304 \begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 Return the value of the given socket option (see the \UNIX{} man page
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
306 \manpage{getsockopt}{2}). The needed symbolic constants
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
307 (\constant{SO_*} etc.) are defined in this module. If \var{buflen}
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
308 is absent, an integer option is assumed and its integer value
2447
73499a667e78 changes refs to SO_* symbols
Guido van Rossum <guido@python.org>
parents: 2354
diff changeset
309 is returned by the function. If \var{buflen} is present, it specifies
73499a667e78 changes refs to SO_* symbols
Guido van Rossum <guido@python.org>
parents: 2354
diff changeset
310 the maximum length of the buffer used to receive the option in, and
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
311 this buffer is returned as a string. It is up to the caller to decode
2447
73499a667e78 changes refs to SO_* symbols
Guido van Rossum <guido@python.org>
parents: 2354
diff changeset
312 the contents of the buffer (see the optional built-in module
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
313 \refmodule{struct} for a way to decode C structures encoded as strings).
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
314 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
315
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
316 \begin{methoddesc}[socket]{listen}{backlog}
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
317 Listen for connections made to the socket. The \var{backlog} argument
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
318 specifies the maximum number of queued connections and should be at
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
319 least 1; the maximum value is system-dependent (usually 5).
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
320 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
321
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
322 \begin{methoddesc}[socket]{makefile}{\optional{mode\optional{, bufsize}}}
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
323 Return a \dfn{file object} associated with the socket. (File objects
11178
245d2b605036 Add index entry "I/O control, buffering" for makefile().
Fred Drake <fdrake@acm.org>
parents: 10836
diff changeset
324 are described in \ref{bltin-file-objects}, ``File Objects.'')
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
325 The file object references a \cfunction{dup()}ped version of the
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
326 socket file descriptor, so the file object and socket object may be
11178
245d2b605036 Add index entry "I/O control, buffering" for makefile().
Fred Drake <fdrake@acm.org>
parents: 10836
diff changeset
327 closed or garbage-collected independently.
245d2b605036 Add index entry "I/O control, buffering" for makefile().
Fred Drake <fdrake@acm.org>
parents: 10836
diff changeset
328 \index{I/O control!buffering}The optional \var{mode}
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
329 and \var{bufsize} arguments are interpreted the same way as by the
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
330 built-in \function{open()} function.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
331 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
333 \begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 Receive data from the socket. The return value is a string representing
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 the data received. The maximum amount of data to be received
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 at once is specified by \var{bufsize}. See the \UNIX{} manual page
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
337 \manpage{recv}{2} for the meaning of the optional argument
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
338 \var{flags}; it defaults to zero.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
339 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
340
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
341 \begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 Receive data from the socket. The return value is a pair
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 \code{(\var{string}, \var{address})} where \var{string} is a string
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 representing the data received and \var{address} is the address of the
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
345 socket sending the data. The optional \var{flags} argument has the
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
346 same meaning as for \method{recv()} above.
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2447
diff changeset
347 (The format of \var{address} depends on the address family --- see above.)
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
348 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
350 \begin{methoddesc}[socket]{send}{string\optional{, flags}}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 Send data to the socket. The socket must be connected to a remote
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
352 socket. The optional \var{flags} argument has the same meaning as for
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
353 \method{recv()} above. Returns the number of bytes sent.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
354 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
355
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
356 \begin{methoddesc}[socket]{sendto}{string\optional{, flags}, address}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 Send data to the socket. The socket should not be connected to a
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 remote socket, since the destination socket is specified by
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
359 \var{address}. The optional \var{flags} argument has the same
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
360 meaning as for \method{recv()} above. Return the number of bytes sent.
2453
cdd3a721b40e a few typographical changes (e.g. -- => ---) and lots of new stuff in the WWW chapter
Guido van Rossum <guido@python.org>
parents: 2447
diff changeset
361 (The format of \var{address} depends on the address family --- see above.)
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
362 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
363
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
364 \begin{methoddesc}[socket]{setblocking}{flag}
1871
10a12473ee93 added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents: 1767
diff changeset
365 Set blocking or non-blocking mode of the socket: if \var{flag} is 0,
10a12473ee93 added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents: 1767
diff changeset
366 the socket is set to non-blocking, else to blocking mode. Initially
10a12473ee93 added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents: 1767
diff changeset
367 all sockets are in blocking mode. In non-blocking mode, if a
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
368 \method{recv()} call doesn't find any data, or if a
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
369 \method{send()} call can't immediately dispose of the data, a
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
370 \exception{error} exception is raised; in blocking mode, the calls
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
371 block until they can proceed.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
372 \end{methoddesc}
1871
10a12473ee93 added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents: 1767
diff changeset
373
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
374 \begin{methoddesc}[socket]{setsockopt}{level, optname, value}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 Set the value of the given socket option (see the \UNIX{} man page
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
376 \manpage{setsockopt}{2}). The needed symbolic constants are defined in
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
377 the \module{socket} module (\code{SO_*} etc.). The value can be an
2447
73499a667e78 changes refs to SO_* symbols
Guido van Rossum <guido@python.org>
parents: 2354
diff changeset
378 integer or a string representing a buffer. In the latter case it is
73499a667e78 changes refs to SO_* symbols
Guido van Rossum <guido@python.org>
parents: 2354
diff changeset
379 up to the caller to ensure that the string contains the proper bits
73499a667e78 changes refs to SO_* symbols
Guido van Rossum <guido@python.org>
parents: 2354
diff changeset
380 (see the optional built-in module
10655
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
381 \refmodule{struct}\refbimodindex{struct} for a way to encode C
299bf53e6861 Logical markup nits.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
382 structures as strings).
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
383 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
384
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
385 \begin{methoddesc}[socket]{shutdown}{how}
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
386 Shut down one or both halves of the connection. If \var{how} is
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
387 \code{0}, further receives are disallowed. If \var{how} is \code{1},
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
388 further sends are disallowed. If \var{how} is \code{2}, further sends
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
389 and receives are disallowed.
7823
0575d5a17218 Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents: 7712
diff changeset
390 \end{methoddesc}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
391
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
392 Note that there are no methods \method{read()} or \method{write()};
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
393 use \method{recv()} and \method{send()} without \var{flags} argument
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
394 instead.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 \subsection{Example}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 \nodename{Socket Example}
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
398
2527
815dfaa6a778 small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents: 2453
diff changeset
399 Here are two minimal example programs using the TCP/IP protocol:\ a
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 server that echoes all data that it receives back (servicing only one
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 client), and a client using it. Note that a server must perform the
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
402 sequence \function{socket()}, \method{bind()}, \method{listen()},
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
403 \method{accept()} (possibly repeating the \method{accept()} to service
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
404 more than one client), while a client only needs the sequence
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
405 \function{socket()}, \method{connect()}. Also note that the server
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
406 does not \method{send()}/\method{recv()} on the
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 socket it is listening on but on the new socket returned by
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
408 \method{accept()}.
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
409
7277
bf4e790bd61a Remove all \bcode / \ecode cruft; this is no longer needed. See previous
Fred Drake <fdrake@acm.org>
parents: 7200
diff changeset
410 \begin{verbatim}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 # Echo server program
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 from socket import *
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 HOST = '' # Symbolic name meaning the local host
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 PORT = 50007 # Arbitrary non-privileged server
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 s = socket(AF_INET, SOCK_STREAM)
12571
8bcd14313c11 Someone found the examples of poor practice on socket addresses!
Fred Drake <fdrake@acm.org>
parents: 12230
diff changeset
416 s.bind((HOST, PORT))
1567
dddcba2038bc adapt to min value of 1 for listen() backlog argument
Guido van Rossum <guido@python.org>
parents: 1483
diff changeset
417 s.listen(1)
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 conn, addr = s.accept()
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 print 'Connected by', addr
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 while 1:
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 data = conn.recv(1024)
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 if not data: break
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 conn.send(data)
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 conn.close()
7277
bf4e790bd61a Remove all \bcode / \ecode cruft; this is no longer needed. See previous
Fred Drake <fdrake@acm.org>
parents: 7200
diff changeset
425 \end{verbatim}
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
426
7277
bf4e790bd61a Remove all \bcode / \ecode cruft; this is no longer needed. See previous
Fred Drake <fdrake@acm.org>
parents: 7200
diff changeset
427 \begin{verbatim}
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 # Echo client program
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 from socket import *
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 HOST = 'daring.cwi.nl' # The remote host
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 PORT = 50007 # The same port as used by the server
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 s = socket(AF_INET, SOCK_STREAM)
12571
8bcd14313c11 Someone found the examples of poor practice on socket addresses!
Fred Drake <fdrake@acm.org>
parents: 12230
diff changeset
433 s.connect((HOST, PORT))
1471
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 s.send('Hello, world')
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 data = s.recv(1024)
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 s.close()
2ec96140a36b Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 print 'Received', `data`
7277
bf4e790bd61a Remove all \bcode / \ecode cruft; this is no longer needed. See previous
Fred Drake <fdrake@acm.org>
parents: 7200
diff changeset
438 \end{verbatim}
7632
d22cf666fa2c Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
439
5818
3468ea8645ca AMK's megapatch:
Guido van Rossum <guido@python.org>
parents: 5574
diff changeset
440 \begin{seealso}
12571
8bcd14313c11 Someone found the examples of poor practice on socket addresses!
Fred Drake <fdrake@acm.org>
parents: 12230
diff changeset
441 \seemodule{SocketServer}{classes that simplify writing network servers}
5818
3468ea8645ca AMK's megapatch:
Guido van Rossum <guido@python.org>
parents: 5574
diff changeset
442 \end{seealso}