Mercurial > cpython-lru_cache-weakref
annotate Doc/libsocket.tex @ 3044:7e7bb00a0a2e legacy-trunk
Added functionality: locate CW projects, copy them, empty them and
binhex them.
| author | Jack Jansen <jack.jansen@cwi.nl> |
|---|---|
| date | Thu, 31 Aug 1995 13:47:14 +0000 |
| parents | f47bc5554418 |
| children | c3b0895c90fb |
| rev | line source |
|---|---|
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 \section{Built-in Module \sectcode{socket}} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 \bimodindex{socket} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 This module provides access to the BSD {\em socket} interface. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 It is available on \UNIX{} systems that support this interface. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 For an introduction to socket programming (in C), see the following |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 papers: \emph{An Introductory 4.3BSD Interprocess Communication |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 Tutorial}, by Stuart Sechrest and \emph{An Advanced 4.3BSD Interprocess |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 Communication Tutorial}, by Samuel J. Leffler et al, both in the |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 \UNIX{} Programmer's Manual, Supplementary Documents 1 (sections PS1:7 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 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
|
13 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
|
14 socket semantics. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 The Python interface is a straightforward transliteration of the |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 \UNIX{} system call and library interface for sockets to Python's |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 object-oriented style: the \code{socket()} function returns a |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 \dfn{socket object} whose methods implement the various socket system |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 calls. Parameter types are somewhat higer-level than in the C |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 interface: as with \code{read()} and \code{write()} operations on Python |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 files, buffer allocation on receive operations is automatic, and |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 buffer length is implicit on send operations. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 Socket addresses are represented as a single string for the |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 \code{AF_UNIX} address family and as a pair |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 \code{(\var{host}, \var{port})} for the \code{AF_INET} address family, |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 where \var{host} is a string representing |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 either a hostname in Internet domain notation like |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 \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
|
31 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
|
32 currently not supported. The address format required by a particular |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 socket object is automatically selected based on the address family |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 specified when the socket object was created. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 All errors raise exceptions. The normal exceptions for invalid |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 argument types and out-of-memory conditions can be raised; errors |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 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
|
39 |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
40 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
|
41 method. |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 The module \code{socket} exports the following constants and functions: |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 \renewcommand{\indexsubitem}{(in module socket)} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 \begin{excdesc}{error} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 This exception is raised for socket- or address-related errors. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 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
|
49 pair \code{(\var{errno}, \var{string})} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 representing an error returned by a system |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 call, similar to the value accompanying \code{posix.error}. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 \end{excdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 \begin{datadesc}{AF_UNIX} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 \dataline{AF_INET} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 These constants represent the address (and protocol) families, |
|
1759
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
57 used for the first argument to \code{socket()}. If the \code{AF_UNIX} |
|
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
58 constant is not defined then this protocol is unsupported. |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 \end{datadesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 \begin{datadesc}{SOCK_STREAM} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 \dataline{SOCK_DGRAM} |
|
1759
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
63 \dataline{SOCK_RAW} |
|
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
64 \dataline{SOCK_RDM} |
|
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
65 \dataline{SOCK_SEQPACKET} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 These constants represent the socket types, |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 used for the second argument to \code{socket()}. |
|
1759
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
68 (Only \code{SOCK_STREAM} and |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 \code{SOCK_DGRAM} appear to be generally useful.) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 \end{datadesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
|
2354
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
72 \begin{datadesc}{SO_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
73 \dataline{SOMAXCONN} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
74 \dataline{MSG_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
75 \dataline{SOL_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
76 \dataline{IPPROTO_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
77 \dataline{IPPORT_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
78 \dataline{INADDR_*} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
79 \dataline{IP_*} |
|
2527
815dfaa6a778
small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents:
2453
diff
changeset
|
80 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
|
81 sockets and/or the IP protocol, are also defined in the socket module. |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
82 They are generally used in arguments to the \code{setsockopt} and |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
83 \code{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
|
84 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
|
85 for a few symbols, default values are provided. |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
86 \end{datadesc} |
|
fbaf54169711
added lots of constants
Guido van Rossum <guido@python.org>
parents:
2064
diff
changeset
|
87 |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 \begin{funcdesc}{gethostbyname}{hostname} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 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
|
90 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
|
91 is an IP address itself it is returned unchanged. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 |
|
1759
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
94 \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
|
95 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
|
96 the Python interpreter is currently executing. If you want to know the |
|
907605320a5f
Lots of small corrections by Andrew Kuchling (plus all new rotor docs)
Guido van Rossum <guido@python.org>
parents:
1759
diff
changeset
|
97 current machine's IP address, use |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
98 \code{socket.gethostbyname(socket.gethostname())}. |
|
2064
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
99 \end{funcdesc} |
|
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
100 |
|
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
101 \begin{funcdesc}{gethostbyaddr}{ip_address} |
|
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
102 Return a triple \code{(hostname, aliaslist, ipaddrlist)} where |
|
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
103 \code{hostname} is the primary host name responding to the given |
|
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
104 \var{ip_address}, \code{aliaslist} is a (possibly empty) list of |
|
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
105 alternative host names for the same address, and \code{ipaddrlist} is |
|
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
106 a list of IP addresses for the same interface on the same |
|
280f31e6c653
copyright.tex: Add 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1871
diff
changeset
|
107 host (most likely containing only a single address). |
|
1759
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
108 \end{funcdesc} |
|
4d421f9e8373
* Doc/libsocket.tex (subsection{Socket Object Methods}):
Guido van Rossum <guido@python.org>
parents:
1750
diff
changeset
|
109 |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 \begin{funcdesc}{getservbyname}{servicename\, protocolname} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 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
|
112 for that service. The protocol name should be \code{'tcp'} or |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 \code{'udp'}. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
2527
815dfaa6a778
small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents:
2453
diff
changeset
|
116 \begin{funcdesc}{socket}{family\, type\optional{\, proto}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 Create a new socket using the given address family, socket type and |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 protocol number. The address family should be \code{AF_INET} or |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 \code{AF_UNIX}. The socket type should be \code{SOCK_STREAM}, |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 \code{SOCK_DGRAM} or perhaps one of the other \samp{SOCK_} constants. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 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
|
122 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 |
|
2527
815dfaa6a778
small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents:
2453
diff
changeset
|
124 \begin{funcdesc}{fromfd}{fd\, family\, type\optional{\, proto}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 Build a socket object from an existing file descriptor (an integer as |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 returned by a file object's \code{fileno} method). Address family, |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 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
|
128 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
|
129 checked --- subsequent operations on the object may fail if the file |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 descriptor is invalid. This function is rarely needed, but can be |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 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
|
132 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
|
133 daemon). |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
136 \subsection{Socket Objects} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 \noindent |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 Socket objects have the following methods. Except for |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 \code{makefile()} these correspond to \UNIX{} system calls applicable to |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 sockets. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 \renewcommand{\indexsubitem}{(socket method)} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 \begin{funcdesc}{accept}{} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 Accept a connection. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 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
|
147 The return value is a pair \code{(\var{conn}, \var{address})} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 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
|
149 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
|
150 to the socket on the other end of the connection. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 \begin{funcdesc}{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
|
154 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
|
155 (The format of \var{address} depends on the address family --- see above.) |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 \begin{funcdesc}{close}{} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 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
|
160 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
|
161 Sockets are automatically closed when they are garbage-collected. |
|
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 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 \begin{funcdesc}{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
|
165 Connect to a remote socket at \var{address}. |
|
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
|
166 (The format of \var{address} depends on the address family --- see above.) |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 \begin{funcdesc}{fileno}{} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 Return the socket's file descriptor (a small integer). This is useful |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 with \code{select}. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 \begin{funcdesc}{getpeername}{} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 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
|
176 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
|
177 (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
|
178 see above.) On some systems this function is not supported. |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 \begin{funcdesc}{getsockname}{} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 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
|
183 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
|
184 (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
|
185 see above.) |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 |
|
2527
815dfaa6a778
small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents:
2453
diff
changeset
|
188 \begin{funcdesc}{getsockopt}{level\, optname\optional{\, buflen}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 Return the value of the given socket option (see the \UNIX{} man page |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
190 {\it getsockopt}(2)). The needed symbolic constants (\code{SO_*} etc.) |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
191 are defined in this module. If \var{buflen} |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
192 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
|
193 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
|
194 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
|
195 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
|
196 the contents of the buffer (see the optional built-in module |
|
73499a667e78
changes refs to SO_* symbols
Guido van Rossum <guido@python.org>
parents:
2354
diff
changeset
|
197 \code{struct} for a way to decode C structures encoded as strings). |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 \begin{funcdesc}{listen}{backlog} |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
201 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
|
202 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
|
203 least 1; the maximum value is system-dependent (usually 5). |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
206 \begin{funcdesc}{makefile}{\optional{mode\optional{\, bufsize}}} |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
207 Return a \dfn{file object} associated with the socket. (File objects |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
208 were described earlier under Built-in Types.) The file object |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
209 references a \code{dup()}ped version of the socket file descriptor, so |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
210 the file object and socket object may be closed or garbage-collected |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
211 independently. The optional \var{mode} and \var{bufsize} arguments |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
212 are interpreted the same way as by the built-in |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
213 \code{open()} function. |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 |
|
2527
815dfaa6a778
small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents:
2453
diff
changeset
|
216 \begin{funcdesc}{recv}{bufsize\optional{\, flags}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 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
|
218 the data received. The maximum amount of data to be received |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 at once is specified by \var{bufsize}. See the \UNIX{} manual page |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 for the meaning of the optional argument \var{flags}; it defaults to |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 zero. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
224 \begin{funcdesc}{recvfrom}{bufsize\optional{\, flags}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 Receive data from the socket. The return value is a pair |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 \code{(\var{string}, \var{address})} where \var{string} is a string |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 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
|
228 socket sending the data. The optional \var{flags} argument has the |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
229 same meaning as for \code{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
|
230 (The format of \var{address} depends on the address family --- see above.) |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
233 \begin{funcdesc}{send}{string\optional{\, flags}} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 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
|
235 socket. The optional \var{flags} argument has the same meaning as for |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
236 \code{recv()} above. Return the number of bytes sent. |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
239 \begin{funcdesc}{sendto}{string\optional{\, flags}\, address} |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 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
|
241 remote socket, since the destination socket is specified by |
|
2576
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
242 \code{address}. The optional \var{flags} argument has the same |
|
f47bc5554418
mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents:
2527
diff
changeset
|
243 meaning as for \code{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
|
244 (The format of \var{address} depends on the address family --- see above.) |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 |
|
1871
10a12473ee93
added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents:
1767
diff
changeset
|
247 \begin{funcdesc}{setblocking}{flag} |
|
10a12473ee93
added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents:
1767
diff
changeset
|
248 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
|
249 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
|
250 all sockets are in blocking mode. In non-blocking mode, if a |
|
10a12473ee93
added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents:
1767
diff
changeset
|
251 \code{recv} call doesn't find any data, or if a \code{send} call can't |
|
10a12473ee93
added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents:
1767
diff
changeset
|
252 immediately dispose of the data, a \code{socket.error} exception is |
|
10a12473ee93
added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents:
1767
diff
changeset
|
253 raised; in blocking mode, the calls block until they can proceed. |
|
10a12473ee93
added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents:
1767
diff
changeset
|
254 \end{funcdesc} |
|
10a12473ee93
added Tommy Burnette's setblocking() method
Guido van Rossum <guido@python.org>
parents:
1767
diff
changeset
|
255 |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 \begin{funcdesc}{setsockopt}{level\, optname\, value} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 Set the value of the given socket option (see the \UNIX{} man page |
|
2447
73499a667e78
changes refs to SO_* symbols
Guido van Rossum <guido@python.org>
parents:
2354
diff
changeset
|
258 {\it setsockopt}(2)). The needed symbolic constants are defined in |
|
73499a667e78
changes refs to SO_* symbols
Guido van Rossum <guido@python.org>
parents:
2354
diff
changeset
|
259 the \code{socket} module (\code{SO_*} etc.). The value can be an |
|
73499a667e78
changes refs to SO_* symbols
Guido van Rossum <guido@python.org>
parents:
2354
diff
changeset
|
260 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
|
261 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
|
262 (see the optional built-in module |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 \code{struct} for a way to encode C structures as strings). |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 \begin{funcdesc}{shutdown}{how} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 Shut down one or both halves of the connection. If \var{how} is \code{0}, |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 further receives are disallowed. If \var{how} is \code{1}, further sends are |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 disallowed. If \var{how} is \code{2}, further sends and receives are |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 disallowed. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 \end{funcdesc} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 Note that there are no methods \code{read()} or \code{write()}; use |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 \code{recv()} and \code{send()} without \var{flags} argument instead. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 \subsection{Example} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 \nodename{Socket Example} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 |
|
2527
815dfaa6a778
small changes by Soren Larsen
Guido van Rossum <guido@python.org>
parents:
2453
diff
changeset
|
279 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
|
280 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
|
281 client), and a client using it. Note that a server must perform the |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 sequence \code{socket}, \code{bind}, \code{listen}, \code{accept} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 (possibly repeating the \code{accept} to service more than one client), |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 while a client only needs the sequence \code{socket}, \code{connect}. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 Also note that the server does not \code{send}/\code{receive} on the |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 socket it is listening on but on the new socket returned by |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 \code{accept}. |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 \bcode\begin{verbatim} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 # Echo server program |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 from socket import * |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 HOST = '' # Symbolic name meaning the local host |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 PORT = 50007 # Arbitrary non-privileged server |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 s = socket(AF_INET, SOCK_STREAM) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 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
|
296 s.listen(1) |
|
1471
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 conn, addr = s.accept() |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 print 'Connected by', addr |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 while 1: |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 data = conn.recv(1024) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 if not data: break |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 conn.send(data) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 conn.close() |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 \end{verbatim}\ecode |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 \bcode\begin{verbatim} |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 # Echo client program |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 from socket import * |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 HOST = 'daring.cwi.nl' # The remote host |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 PORT = 50007 # The same port as used by the server |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 s = socket(AF_INET, SOCK_STREAM) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 s.connect(HOST, PORT) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 s.send('Hello, world') |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 data = s.recv(1024) |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 s.close() |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 print 'Received', `data` |
|
2ec96140a36b
Restructured library documentation
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 \end{verbatim}\ecode |
