Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libsocket.tex @ 8831:00a6bb65ff29 legacy-trunk
Getting rid of this ancient stuff.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 05 Aug 1998 21:10:09 +0000 |
| parents | 760dcf197048 |
| children | a9eb265d2f57 |
| rev | line source |
|---|---|
|
7877
8e5223680a9d
Remove all uses of \sectcode; we can now use logical markup everywhere.
Fred Drake <fdrake@acm.org>
parents:
7823
diff
changeset
|
1 \section{Built-in Module \module{socket}} |
|
8716
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
7877
diff
changeset
|
2 \declaremodule{builtin}{socket} |
|
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
7877
diff
changeset
|
3 |
|
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
7877
diff
changeset
|
4 \modulesynopsis{Low-level networking interface.} |
|
760dcf197048
Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents:
7877
diff
changeset
|
5 |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
7096
5bb884527446
Consistency: "{\bf ...}" ==> "\strong{...}"
Fred Drake <fdrake@acm.org>
parents:
6804
diff
changeset
|
7 This module provides access to the BSD \emph{socket} interface. |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 It is available on \UNIX{} systems that support this interface. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 For an introduction to socket programming (in C), see the following |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 papers: \emph{An Introductory 4.3BSD Interprocess Communication |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 Tutorial}, by Stuart Sechrest and \emph{An Advanced 4.3BSD Interprocess |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 Communication Tutorial}, by Samuel J. Leffler et al, both in the |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 \UNIX{} Programmer's Manual, Supplementary Documents 1 (sections PS1:7 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 and PS1:8). The \UNIX{} manual pages for the various socket-related |
|
2527
815dfaa6a778
small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents:
2453
diff
changeset
|
16 system calls are also a valuable source of information on the details of |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 socket semantics. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 The Python interface is a straightforward transliteration of the |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 \UNIX{} system call and library interface for sockets to Python's |
| 7632 | 21 object-oriented style: the \function{socket()} function returns a |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 \dfn{socket object} whose methods implement the various socket system |
| 4882 | 23 calls. Parameter types are somewhat higher-level than in the C |
| 7632 | 24 interface: as with \method{read()} and \method{write()} operations on |
| 25 Python files, buffer allocation on receive operations is automatic, | |
| 26 and buffer length is implicit on send operations. | |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 Socket addresses are represented as a single string for the |
| 7632 | 29 \constant{AF_UNIX} address family and as a pair |
| 30 \code{(\var{host}, \var{port})} for the \constant{AF_INET} address | |
| 31 family, where \var{host} is a string representing | |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 either a hostname in Internet domain notation like |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 \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
|
34 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
|
35 currently not supported. The address format required by a particular |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 socket object is automatically selected based on the address family |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 specified when the socket object was created. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 |
|
5461
0ded63bcf5de
Document "" and "<broadcast>".
Guido van Rossum <guido@python.org>
parents:
4929
diff
changeset
|
39 For IP addresses, two special forms are accepted instead of a host |
| 7632 | 40 address: the empty string represents \constant{INADDR_ANY}, and the string |
| 41 \code{"<broadcast>"} represents \constant{INADDR_BROADCAST}. | |
|
5461
0ded63bcf5de
Document "" and "<broadcast>".
Guido van Rossum <guido@python.org>
parents:
4929
diff
changeset
|
42 |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 All errors raise exceptions. The normal exceptions for invalid |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 argument types and out-of-memory conditions can be raised; errors |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 related to socket or address semantics raise the error \code{socket.error}. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
47 Non-blocking mode is supported through the \code{setblocking()} |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
48 method. |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 |
| 7632 | 50 The module \module{socket} exports the following constants and functions: |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
| 7632 | 52 |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 \begin{excdesc}{error} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 This exception is raised for socket- or address-related errors. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 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
|
56 pair \code{(\var{errno}, \var{string})} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 representing an error returned by a system |
|
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
|
58 call, similar to the value accompanying \code{os.error}. |
|
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
|
59 See the module \module{errno}\refbimodindex{errno}, which contains |
|
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
|
60 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
|
61 \end{excdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 \begin{datadesc}{AF_UNIX} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 \dataline{AF_INET} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 These constants represent the address (and protocol) families, |
| 7632 | 66 used for the first argument to \function{socket()}. If the |
| 67 \constant{AF_UNIX} constant is not defined then this protocol is | |
| 68 unsupported. | |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 \end{datadesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 \begin{datadesc}{SOCK_STREAM} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 \dataline{SOCK_DGRAM} |
|
1759
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
73 \dataline{SOCK_RAW} |
|
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
74 \dataline{SOCK_RDM} |
|
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
75 \dataline{SOCK_SEQPACKET} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 These constants represent the socket types, |
| 7632 | 77 used for the second argument to \function{socket()}. |
| 78 (Only \constant{SOCK_STREAM} and | |
| 79 \constant{SOCK_DGRAM} appear to be generally useful.) | |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 \end{datadesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 |
|
2354
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
82 \begin{datadesc}{SO_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
83 \dataline{SOMAXCONN} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
84 \dataline{MSG_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
85 \dataline{SOL_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
86 \dataline{IPPROTO_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
87 \dataline{IPPORT_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
88 \dataline{INADDR_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
89 \dataline{IP_*} |
|
2527
815dfaa6a778
small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents:
2453
diff
changeset
|
90 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
|
91 sockets and/or the IP protocol, are also defined in the socket module. |
| 7632 | 92 They are generally used in arguments to the \method{setsockopt()} and |
| 93 \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
|
94 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
|
95 for a few symbols, default values are provided. |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
96 \end{datadesc} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
97 |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 \begin{funcdesc}{gethostbyname}{hostname} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 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
|
100 returned as a string, e.g., \code{'100.50.200.5'}. If the host name |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 is an IP address itself it is returned unchanged. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 |
|
1759
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
104 \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
|
105 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
|
106 the Python interpreter is currently executing. If you want to know the |
| 7632 | 107 current machine's IP address, use \code{gethostbyname(gethostname())}. |
| 108 Note: \function{gethostname()} doesn't always return the fully qualified | |
| 109 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
|
110 (see below). |
|
2064
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
111 \end{funcdesc} |
|
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
112 |
|
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
113 \begin{funcdesc}{gethostbyaddr}{ip_address} |
| 7632 | 114 Return a triple \code{(\var{hostname}, \var{aliaslist}, |
| 115 \var{ipaddrlist})} where \var{hostname} is the primary host name | |
| 116 responding to the given \var{ip_address}, \var{aliaslist} is a | |
| 117 (possibly empty) list of alternative host names for the same address, | |
| 118 and \var{ipaddrlist} is a list of IP addresses for the same interface | |
| 119 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
|
120 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
|
121 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
|
122 \end{funcdesc} |
|
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
123 |
|
4784
e43362910a5e
Documented getprotobyname() interface.
Guido van Rossum <guido@python.org>
parents:
4576
diff
changeset
|
124 \begin{funcdesc}{getprotobyname}{protocolname} |
|
e43362910a5e
Documented getprotobyname() interface.
Guido van Rossum <guido@python.org>
parents:
4576
diff
changeset
|
125 Translate an Internet protocol name (e.g. \code{'icmp'}) to a constant |
|
e43362910a5e
Documented getprotobyname() interface.
Guido van Rossum <guido@python.org>
parents:
4576
diff
changeset
|
126 suitable for passing as the (optional) third argument to the |
| 7632 | 127 \function{socket()} function. This is usually only needed for sockets |
| 128 opened in ``raw'' mode (\constant{SOCK_RAW}); for the normal socket | |
| 129 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
|
130 omitted or zero. |
|
e43362910a5e
Documented getprotobyname() interface.
Guido van Rossum <guido@python.org>
parents:
4576
diff
changeset
|
131 \end{funcdesc} |
|
e43362910a5e
Documented getprotobyname() interface.
Guido van Rossum <guido@python.org>
parents:
4576
diff
changeset
|
132 |
| 7632 | 133 \begin{funcdesc}{getservbyname}{servicename, protocolname} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 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
|
135 for that service. The protocol name should be \code{'tcp'} or |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 \code{'udp'}. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 |
| 7632 | 139 \begin{funcdesc}{socket}{family, type\optional{, proto}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 Create a new socket using the given address family, socket type and |
| 7632 | 141 protocol number. The address family should be \constant{AF_INET} or |
| 142 \constant{AF_UNIX}. The socket type should be \constant{SOCK_STREAM}, | |
| 143 \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
|
144 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
|
145 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 |
| 7632 | 147 \begin{funcdesc}{fromfd}{fd, family, type\optional{, proto}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 Build a socket object from an existing file descriptor (an integer as |
| 7632 | 149 returned by a file object's \method{fileno()} method). Address family, |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 socket type and protocol number are as for the \code{socket} function |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 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
|
152 checked --- subsequent operations on the object may fail if the file |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 descriptor is invalid. This function is rarely needed, but can be |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 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
|
155 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
|
156 daemon). |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 |
|
4576
c3b0895c90fb
Added [nh]to[hn][sl]().
Guido van Rossum <guido@python.org>
parents:
2576
diff
changeset
|
159 \begin{funcdesc}{ntohl}{x} |
|
6783
c18501770895
Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents:
6587
diff
changeset
|
160 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
|
161 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
|
162 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
|
163 \end{funcdesc} |
|
c18501770895
Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents:
6587
diff
changeset
|
164 |
|
c18501770895
Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents:
6587
diff
changeset
|
165 \begin{funcdesc}{ntohs}{x} |
|
c18501770895
Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents:
6587
diff
changeset
|
166 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
|
167 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
|
168 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
|
169 \end{funcdesc} |
|
c18501770895
Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents:
6587
diff
changeset
|
170 |
|
c18501770895
Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents:
6587
diff
changeset
|
171 \begin{funcdesc}{htonl}{x} |
|
c18501770895
Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents:
6587
diff
changeset
|
172 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
|
173 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
|
174 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
|
175 \end{funcdesc} |
|
c18501770895
Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents:
6587
diff
changeset
|
176 |
|
c18501770895
Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents:
6587
diff
changeset
|
177 \begin{funcdesc}{htons}{x} |
|
c18501770895
Separated the definitions of ntohs(), ntohl(), htons(), htonl(). Separate
Fred Drake <fdrake@acm.org>
parents:
6587
diff
changeset
|
178 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
|
179 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
|
180 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
|
181 \end{funcdesc} |
|
c3b0895c90fb
Added [nh]to[hn][sl]().
Guido van Rossum <guido@python.org>
parents:
2576
diff
changeset
|
182 |
| 6482 | 183 \begin{datadesc}{SocketType} |
|
5574
84d7cc5e63cd
Added description of socket_type.
Guido van Rossum <guido@python.org>
parents:
5461
diff
changeset
|
184 This is a Python type object that represents the socket object type. |
| 7632 | 185 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
|
186 \end{datadesc} |
|
84d7cc5e63cd
Added description of socket_type.
Guido van Rossum <guido@python.org>
parents:
5461
diff
changeset
|
187 |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
188 \subsection{Socket Objects} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 Socket objects have the following methods. Except for |
| 7632 | 191 \method{makefile()} these correspond to \UNIX{} system calls |
| 192 applicable to sockets. | |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
194 \begin{methoddesc}[socket]{accept}{} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 Accept a connection. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 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
|
197 The return value is a pair \code{(\var{conn}, \var{address})} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 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
|
199 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
|
200 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
|
201 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
203 \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
|
204 Bind the socket to \var{address}. The socket must not already be bound. |
|
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
|
205 (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
|
206 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
208 \begin{methoddesc}[socket]{close}{} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 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
|
210 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
|
211 Sockets are automatically closed when they are garbage-collected. |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
212 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
214 \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
|
215 Connect to a remote socket at \var{address}. |
| 7632 | 216 (The format of \var{address} depends on the address family --- see |
| 217 above.) | |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
218 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
220 \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
|
221 Like \code{connect(\var{address})}, but return an error indicator |
|
6587
e0e667bbc971
Document s.connect_ex(), which does not raise an exception on error.
Guido van Rossum <guido@python.org>
parents:
6482
diff
changeset
|
222 instead of raising an exception. The error indicator is 0 if the |
| 7632 | 223 operation succeeded, otherwise the value of the \cdata{errno} |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
224 variable. This is useful, e.g., for asynchronous connects. |
|
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
225 \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
|
226 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
227 \begin{methoddesc}[socket]{fileno}{} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 Return the socket's file descriptor (a small integer). This is useful |
| 7632 | 229 with \function{select.select()}. |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
230 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
232 \begin{methoddesc}[socket]{getpeername}{} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 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
|
234 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
|
235 (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
|
236 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
|
237 \end{methoddesc} |
|
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]{getsockname}{} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 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
|
241 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
|
242 (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
|
243 see above.) |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
244 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
246 \begin{methoddesc}[socket]{getsockopt}{level, optname\optional{, buflen}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 Return the value of the given socket option (see the \UNIX{} man page |
| 7632 | 248 \manpage{getsockopt}{2}). The needed symbolic constants |
| 249 (\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
|
250 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
|
251 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
|
252 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
|
253 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
|
254 the contents of the buffer (see the optional built-in module |
| 7632 | 255 \module{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
|
256 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
258 \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
|
259 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
|
260 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
|
261 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
|
262 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
264 \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
|
265 Return a \dfn{file object} associated with the socket. (File objects |
| 7632 | 266 were described earlier in \ref{bltin-file-objects}, ``File Objects.'') |
| 267 The file object references a \cfunction{dup()}ped version of the | |
| 268 socket file descriptor, so the file object and socket object may be | |
| 269 closed or garbage-collected independently. The optional \var{mode} | |
| 270 and \var{bufsize} arguments are interpreted the same way as by the | |
| 271 built-in \function{open()} function. | |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
272 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
274 \begin{methoddesc}[socket]{recv}{bufsize\optional{, flags}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 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
|
276 the data received. The maximum amount of data to be received |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 at once is specified by \var{bufsize}. See the \UNIX{} manual page |
| 7632 | 278 \manpage{recv}{2} for the meaning of the optional argument |
| 279 \var{flags}; it defaults to zero. | |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
280 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
282 \begin{methoddesc}[socket]{recvfrom}{bufsize\optional{, flags}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 Receive data from the socket. The return value is a pair |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 \code{(\var{string}, \var{address})} where \var{string} is a string |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 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
|
286 socket sending the data. The optional \var{flags} argument has the |
| 7632 | 287 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
|
288 (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
|
289 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
291 \begin{methoddesc}[socket]{send}{string\optional{, flags}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 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
|
293 socket. The optional \var{flags} argument has the same meaning as for |
| 7632 | 294 \method{recv()} above. Returns the number of bytes sent. |
|
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]{sendto}{string\optional{, flags}, address} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 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
|
299 remote socket, since the destination socket is specified by |
| 7632 | 300 \var{address}. The optional \var{flags} argument has the same |
| 301 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
|
302 (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
|
303 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
305 \begin{methoddesc}[socket]{setblocking}{flag} |
|
1871
10a12473ee93
added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents:
1767
diff
changeset
|
306 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
|
307 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
|
308 all sockets are in blocking mode. In non-blocking mode, if a |
| 7632 | 309 \method{recv()} call doesn't find any data, or if a \code{send} call can't |
| 310 immediately dispose of the data, a \exception{error} exception is | |
|
1871
10a12473ee93
added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents:
1767
diff
changeset
|
311 raised; in blocking mode, the calls block until they can proceed. |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
312 \end{methoddesc} |
|
1871
10a12473ee93
added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents:
1767
diff
changeset
|
313 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
314 \begin{methoddesc}[socket]{setsockopt}{level, optname, value} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 Set the value of the given socket option (see the \UNIX{} man page |
| 7632 | 316 \manpage{setsockopt}{2}). The needed symbolic constants are defined in |
| 317 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
|
318 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
|
319 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
|
320 (see the optional built-in module |
| 7632 | 321 \module{struct}\refbimodindex{struct} for a way to encode C structures |
| 322 as strings). | |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
323 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
325 \begin{methoddesc}[socket]{shutdown}{how} |
| 7632 | 326 Shut down one or both halves of the connection. If \var{how} is |
| 327 \code{0}, further receives are disallowed. If \var{how} is \code{1}, | |
| 328 further sends are disallowed. If \var{how} is \code{2}, further sends | |
| 329 and receives are disallowed. | |
|
7823
0575d5a17218
Use {methoddesc} as appropriate.
Fred Drake <fdrake@acm.org>
parents:
7712
diff
changeset
|
330 \end{methoddesc} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 |
| 7632 | 332 Note that there are no methods \method{read()} or \method{write()}; |
| 333 use \method{recv()} and \method{send()} without \var{flags} argument | |
| 334 instead. | |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 \subsection{Example} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 \nodename{Socket Example} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 |
|
2527
815dfaa6a778
small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents:
2453
diff
changeset
|
339 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
|
340 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
|
341 client), and a client using it. Note that a server must perform the |
| 7632 | 342 sequence \function{socket()}, \method{bind()}, \method{listen()}, |
| 343 \method{accept()} (possibly repeating the \method{accept()} to service | |
| 344 more than one client), while a client only needs the sequence | |
| 345 \function{socket()}, \method{connect()}. Also note that the server | |
| 346 does not \method{send()}/\method{recv()} on the | |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 socket it is listening on but on the new socket returned by |
| 7632 | 348 \method{accept()}. |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 |
|
7277
bf4e790bd61a
Remove all \bcode / \ecode cruft; this is no longer needed. See previous
Fred Drake <fdrake@acm.org>
parents:
7200
diff
changeset
|
350 \begin{verbatim} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 # Echo server program |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 from socket import * |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 HOST = '' # Symbolic name meaning the local host |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 PORT = 50007 # Arbitrary non-privileged server |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 s = socket(AF_INET, SOCK_STREAM) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 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
|
357 s.listen(1) |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 conn, addr = s.accept() |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 print 'Connected by', addr |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 while 1: |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 data = conn.recv(1024) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 if not data: break |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 conn.send(data) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 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
|
365 \end{verbatim} |
| 7632 | 366 |
|
7277
bf4e790bd61a
Remove all \bcode / \ecode cruft; this is no longer needed. See previous
Fred Drake <fdrake@acm.org>
parents:
7200
diff
changeset
|
367 \begin{verbatim} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 # Echo client program |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 from socket import * |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 HOST = 'daring.cwi.nl' # The remote host |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 PORT = 50007 # The same port as used by the server |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 s = socket(AF_INET, SOCK_STREAM) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 s.connect(HOST, PORT) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 s.send('Hello, world') |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 data = s.recv(1024) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 s.close() |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 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
|
378 \end{verbatim} |
| 7632 | 379 |
| 5818 | 380 \begin{seealso} |
| 381 \seemodule{SocketServer}{classes that simplify writing network servers} | |
| 382 \end{seealso} |
