Mercurial > cpython-lru_cache-weakref
annotate Lib/socket.py @ 17581:c1937faabb23 2.1
Added support for optional explanation parameter to the \versionadded
macro.
Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most
of the work in a helper function, with the do_cmd_*() wrappers just supplying
a portion of the replacement text.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Wed, 18 Apr 2001 17:29:14 +0000 |
| parents | 0377f927cafa |
| children | 22d821673e52 b738428238f1 |
| rev | line source |
|---|---|
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 # Wrapper module for _socket, providing some additional facilities |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
2 # implemented in Python. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
3 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
4 """\ |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 This module provides socket operations and some related functions. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
6 On Unix, it supports IP (Internet Protocol) and Unix domain sockets. |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14935
diff
changeset
|
7 On other systems, it only supports IP. Functions specific for a |
|
14935
13488539f02b
Mention in the module's doc string that other functions of the socket API
Martin v. Löwis <martin@v.loewis.de>
parents:
14682
diff
changeset
|
8 socket are available as methods of the socket object. |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
10 Functions: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
11 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
12 socket() -- create a new socket object |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 fromfd() -- create a socket object from an open file descriptor [*] |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 gethostname() -- return the current hostname |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 gethostbyname() -- map a hostname to its IP number |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 gethostbyaddr() -- map an IP number or hostname to DNS info |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 getservbyname() -- map a service name and a protocol name to a port number |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
18 getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 htons(), htonl() -- convert 16, 32 bit int from host to network byte order |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
22 inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
23 ssl() -- secure socket layer support (only available if configured) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
24 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
25 [*] not available on all platforms! |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
26 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 Special objects: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
28 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
29 SocketType -- type object for socket objects |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 error -- exception raised for I/O errors |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
31 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
32 Integer constants: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
33 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
34 AF_INET, AF_UNIX -- socket domains (first argument to socket() call) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
35 SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
36 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
37 Many other constants may be defined; these may be used in calls to |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
38 the setsockopt() and getsockopt() methods. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
39 """ |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
40 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
41 from _socket import * |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
42 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
43 import os, sys |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 |
| 16613 | 45 __all__ = ["getfqdn"] |
| 46 import _socket | |
| 47 __all__.extend(os._get_exports_list(_socket)) | |
| 48 | |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
49 if (sys.platform.lower().startswith("win") |
|
16949
09ba15ee1961
RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents:
16613
diff
changeset
|
50 or (hasattr(os, 'uname') and os.uname()[0] == "BeOS") |
|
09ba15ee1961
RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents:
16613
diff
changeset
|
51 or (sys.platform=="RISCOS")): |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
52 |
|
17156
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
53 _realsocketcall = _socket.socket |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
54 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
55 def socket(family, type, proto=0): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
56 return _socketobject(_realsocketcall(family, type, proto)) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 |
|
17156
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
58 try: |
|
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
59 _realsslcall = _socket.ssl |
|
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
60 except AttributeError: |
|
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
61 pass # No ssl |
|
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
62 else: |
|
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
63 def ssl(sock, keyfile=None, certfile=None): |
|
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
64 if hasattr(sock, "_sock"): |
|
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
65 sock = sock._sock |
|
17245
0377f927cafa
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17156
diff
changeset
|
66 return _realsslcall(sock, keyfile, certfile) |
|
17156
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
67 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
68 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 # WSA error codes |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 if sys.platform.lower().startswith("win"): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
71 errorTab = {} |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
72 errorTab[10004] = "The operation was interrupted." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
73 errorTab[10009] = "A bad file handle was passed." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
74 errorTab[10013] = "Permission denied." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
75 errorTab[10014] = "A fault occurred on the network??" # WSAEFAULT |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
76 errorTab[10022] = "An invalid operation was attempted." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
77 errorTab[10035] = "The socket operation would block" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
78 errorTab[10036] = "A blocking operation is already in progress." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
79 errorTab[10048] = "The network address is in use." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
80 errorTab[10054] = "The connection has been reset." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
81 errorTab[10058] = "The network has been shut down." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 errorTab[10060] = "The operation timed out." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 errorTab[10061] = "Connection refused." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
84 errorTab[10063] = "The name is too long." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 errorTab[10064] = "The host is down." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
86 errorTab[10065] = "The host is unreachable." |
|
17071
b45ae8c467aa
add errorTab to __all__ on win*
Skip Montanaro <skip@pobox.com>
parents:
16949
diff
changeset
|
87 __all__.append("errorTab") |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
88 del os, sys |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
89 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
90 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
91 def getfqdn(name=''): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
92 """Get fully qualified domain name from name. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
93 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
94 An empty argument is interpreted as meaning the local host. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
95 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
96 First the hostname returned by gethostbyaddr() is checked, then |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
97 possibly existing aliases. In case no FQDN is available, hostname |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
98 is returned. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
99 """ |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
100 name = name.strip() |
|
14014
3d68d5bccd40
updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
14007
diff
changeset
|
101 if not name or name == '0.0.0.0': |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
102 name = gethostname() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
103 try: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
104 hostname, aliases, ipaddrs = gethostbyaddr(name) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
105 except error: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
106 pass |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
107 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
108 aliases.insert(0, hostname) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
109 for name in aliases: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
110 if '.' in name: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
111 break |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
112 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
113 name = hostname |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
114 return name |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
115 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
116 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 # |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
118 # These classes are used by the socket() defined on Windows and BeOS |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
119 # platforms to provide a best-effort implementation of the cleanup |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
120 # semantics needed when sockets can't be dup()ed. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
121 # |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
122 # These are not actually used on other platforms. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
123 # |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
124 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
125 class _socketobject: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
126 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
127 def __init__(self, sock): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
128 self._sock = sock |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
129 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
130 def close(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
131 self._sock = 0 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
132 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
133 def __del__(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
134 self.close() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
135 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
136 def accept(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
137 sock, addr = self._sock.accept() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
138 return _socketobject(sock), addr |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
139 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
140 def dup(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
141 return _socketobject(self._sock) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
142 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
143 def makefile(self, mode='r', bufsize=-1): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
144 return _fileobject(self._sock, mode, bufsize) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
145 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
146 _s = "def %s(self, *args): return apply(self._sock.%s, args)\n\n" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
147 for _m in ('bind', 'connect', 'connect_ex', 'fileno', 'listen', |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
148 'getpeername', 'getsockname', |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
149 'getsockopt', 'setsockopt', |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
150 'recv', 'recvfrom', 'send', 'sendto', |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
151 'setblocking', |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
152 'shutdown'): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
153 exec _s % (_m, _m) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
154 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
155 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
156 class _fileobject: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
157 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
158 def __init__(self, sock, mode, bufsize): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
159 self._sock = sock |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
160 self._mode = mode |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
161 if bufsize < 0: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
162 bufsize = 512 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
163 self._rbufsize = max(1, bufsize) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
164 self._wbufsize = bufsize |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
165 self._wbuf = self._rbuf = "" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
166 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
167 def close(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
168 try: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
169 if self._sock: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
170 self.flush() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
171 finally: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
172 self._sock = 0 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
173 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
174 def __del__(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
175 self.close() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
176 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
177 def flush(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
178 if self._wbuf: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
179 self._sock.send(self._wbuf) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
180 self._wbuf = "" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
181 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
182 def fileno(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
183 return self._sock.fileno() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
184 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
185 def write(self, data): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
186 self._wbuf = self._wbuf + data |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
187 if self._wbufsize == 1: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
188 if '\n' in data: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
189 self.flush() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
190 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
191 if len(self._wbuf) >= self._wbufsize: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
192 self.flush() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
193 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
194 def writelines(self, list): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
195 filter(self._sock.send, list) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
196 self.flush() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
197 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
198 def read(self, n=-1): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
199 if n >= 0: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
200 k = len(self._rbuf) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
201 if n <= k: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
202 data = self._rbuf[:n] |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
203 self._rbuf = self._rbuf[n:] |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
204 return data |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
205 n = n - k |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
206 L = [self._rbuf] |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
207 self._rbuf = "" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
208 while n > 0: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
209 new = self._sock.recv(max(n, self._rbufsize)) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
210 if not new: break |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
211 k = len(new) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
212 if k > n: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
213 L.append(new[:n]) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
214 self._rbuf = new[n:] |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
215 break |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
216 L.append(new) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
217 n = n - k |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
218 return "".join(L) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
219 k = max(512, self._rbufsize) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
220 L = [self._rbuf] |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
221 self._rbuf = "" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
222 while 1: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
223 new = self._sock.recv(k) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
224 if not new: break |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
225 L.append(new) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
226 k = min(k*2, 1024**2) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
227 return "".join(L) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
228 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
229 def readline(self, limit=-1): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
230 data = "" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
231 i = self._rbuf.find('\n') |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
232 while i < 0 and not (0 < limit <= len(self._rbuf)): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
233 new = self._sock.recv(self._rbufsize) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
234 if not new: break |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
235 i = new.find('\n') |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
236 if i >= 0: i = i + len(self._rbuf) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
237 self._rbuf = self._rbuf + new |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
238 if i < 0: i = len(self._rbuf) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
239 else: i = i+1 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
240 if 0 <= limit < len(self._rbuf): i = limit |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
241 data, self._rbuf = self._rbuf[:i], self._rbuf[i:] |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
242 return data |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
243 |
|
14682
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
244 def readlines(self, sizehint = 0): |
|
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
245 total = 0 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
246 list = [] |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
247 while 1: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
248 line = self.readline() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
249 if not line: break |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
250 list.append(line) |
|
14682
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
251 total += len(line) |
|
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
252 if sizehint and total >= sizehint: |
|
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
253 break |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
254 return list |
