Mercurial > cpython-lru_cache-weakref
annotate Lib/socket.py @ 24042:63fbbed10714 legacy-trunk
Simplify HTTPSConnection constructor.
See discussion in SF bug 458463.
| author | Jeremy Hylton <jeremy@alum.mit.edu> |
|---|---|
| date | Fri, 28 Jun 2002 23:38:14 +0000 |
| parents | bd37eb4daf3b |
| children | 640370f08e2a |
| 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 |
|
21971
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
41 import _socket |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
42 from _socket import * |
|
21971
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
43 |
|
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
44 SSL_EXISTS = 1 |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
21473
diff
changeset
|
45 try: |
|
21971
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
46 import _ssl |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
21473
diff
changeset
|
47 from _ssl import * |
|
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
21473
diff
changeset
|
48 except ImportError: |
|
21971
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
49 SSL_EXISTS = 0 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
50 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 import os, sys |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
52 |
| 16613 | 53 __all__ = ["getfqdn"] |
| 54 __all__.extend(os._get_exports_list(_socket)) | |
|
21971
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
55 # XXX shouldn't there be something similar to the above for _ssl exports? |
| 16613 | 56 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 if (sys.platform.lower().startswith("win") |
|
16949
09ba15ee1961
RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents:
16613
diff
changeset
|
58 or (hasattr(os, 'uname') and os.uname()[0] == "BeOS") |
|
21971
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
59 or sys.platform=="riscos"): |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
60 |
|
17156
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
61 _realsocketcall = _socket.socket |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
62 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 def socket(family, type, proto=0): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 return _socketobject(_realsocketcall(family, type, proto)) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 |
|
21971
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
66 if SSL_EXISTS: |
|
21959
3957d155f8c5
Break SSL support out of _socket module and place it into a new
Marc-André Lemburg <mal@egenix.com>
parents:
21473
diff
changeset
|
67 _realsslcall = _ssl.ssl |
|
17156
1be0d1c57834
Add a wrapper function for ssl() on Windows. Inspired by SF patch
Guido van Rossum <guido@python.org>
parents:
17071
diff
changeset
|
68 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
|
69 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
|
70 sock = sock._sock |
|
17245
0377f927cafa
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17156
diff
changeset
|
71 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
|
72 |
|
21971
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
73 del _socket |
|
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
74 if SSL_EXISTS: |
|
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
75 del _ssl |
|
fce7278d46b0
Repair so that importing socket doesn't blow up on platforms that lack
Tim Peters <tim.peters@gmail.com>
parents:
21962
diff
changeset
|
76 del SSL_EXISTS |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
77 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
78 # WSA error codes |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
79 if sys.platform.lower().startswith("win"): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
80 errorTab = {} |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
81 errorTab[10004] = "The operation was interrupted." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 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
|
83 errorTab[10013] = "Permission denied." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
84 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
|
85 errorTab[10022] = "An invalid operation was attempted." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
86 errorTab[10035] = "The socket operation would block" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
87 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
|
88 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
|
89 errorTab[10054] = "The connection has been reset." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
90 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
|
91 errorTab[10060] = "The operation timed out." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
92 errorTab[10061] = "Connection refused." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
93 errorTab[10063] = "The name is too long." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
94 errorTab[10064] = "The host is down." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
95 errorTab[10065] = "The host is unreachable." |
|
17071
b45ae8c467aa
add errorTab to __all__ on win*
Skip Montanaro <skip@pobox.com>
parents:
16949
diff
changeset
|
96 __all__.append("errorTab") |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
97 del os, sys |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
98 |
|
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 def getfqdn(name=''): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
101 """Get fully qualified domain name from name. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
102 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
103 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
|
104 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
105 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
|
106 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
|
107 is returned. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
108 """ |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
109 name = name.strip() |
|
14014
3d68d5bccd40
updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
14007
diff
changeset
|
110 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
|
111 name = gethostname() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
112 try: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
113 hostname, aliases, ipaddrs = gethostbyaddr(name) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
114 except error: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
115 pass |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
116 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 aliases.insert(0, hostname) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
118 for name in aliases: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
119 if '.' in name: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
120 break |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
121 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
122 name = hostname |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
123 return name |
|
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 |
|
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 # 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
|
128 # 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
|
129 # 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
|
130 # |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
131 # 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
|
132 # |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
133 |
|
19364
b738428238f1
When the socket is closed, don't just assign 0 to self._sock.
Guido van Rossum <guido@python.org>
parents:
17245
diff
changeset
|
134 _socketmethods = ( |
|
b738428238f1
When the socket is closed, don't just assign 0 to self._sock.
Guido van Rossum <guido@python.org>
parents:
17245
diff
changeset
|
135 'bind', 'connect', 'connect_ex', 'fileno', 'listen', |
|
b738428238f1
When the socket is closed, don't just assign 0 to self._sock.
Guido van Rossum <guido@python.org>
parents:
17245
diff
changeset
|
136 'getpeername', 'getsockname', 'getsockopt', 'setsockopt', |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
137 'recv', 'recvfrom', 'send', 'sendall', 'sendto', 'setblocking', |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
138 'settimeout', 'gettimeout', 'shutdown') |
|
19364
b738428238f1
When the socket is closed, don't just assign 0 to self._sock.
Guido van Rossum <guido@python.org>
parents:
17245
diff
changeset
|
139 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
140 class _socketobject: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
141 |
|
21473
e1f3fe5677da
Move the helper class _closedsocket *into* the _socketobject class.
Guido van Rossum <guido@python.org>
parents:
20732
diff
changeset
|
142 class _closedsocket: |
|
e1f3fe5677da
Move the helper class _closedsocket *into* the _socketobject class.
Guido van Rossum <guido@python.org>
parents:
20732
diff
changeset
|
143 def __getattr__(self, name): |
|
e1f3fe5677da
Move the helper class _closedsocket *into* the _socketobject class.
Guido van Rossum <guido@python.org>
parents:
20732
diff
changeset
|
144 raise error(9, 'Bad file descriptor') |
|
e1f3fe5677da
Move the helper class _closedsocket *into* the _socketobject class.
Guido van Rossum <guido@python.org>
parents:
20732
diff
changeset
|
145 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
146 def __init__(self, sock): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
147 self._sock = sock |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
148 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
149 def close(self): |
|
21473
e1f3fe5677da
Move the helper class _closedsocket *into* the _socketobject class.
Guido van Rossum <guido@python.org>
parents:
20732
diff
changeset
|
150 # Avoid referencing globals here |
|
e1f3fe5677da
Move the helper class _closedsocket *into* the _socketobject class.
Guido van Rossum <guido@python.org>
parents:
20732
diff
changeset
|
151 self._sock = self.__class__._closedsocket() |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
152 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
153 def __del__(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
154 self.close() |
|
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 def accept(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
157 sock, addr = self._sock.accept() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
158 return _socketobject(sock), addr |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
159 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
160 def dup(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
161 return _socketobject(self._sock) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
162 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
163 def makefile(self, mode='r', bufsize=-1): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
164 return _fileobject(self._sock, mode, bufsize) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
165 |
|
19364
b738428238f1
When the socket is closed, don't just assign 0 to self._sock.
Guido van Rossum <guido@python.org>
parents:
17245
diff
changeset
|
166 _s = "def %s(self, *args): return self._sock.%s(*args)\n\n" |
|
b738428238f1
When the socket is closed, don't just assign 0 to self._sock.
Guido van Rossum <guido@python.org>
parents:
17245
diff
changeset
|
167 for _m in _socketmethods: |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
168 exec _s % (_m, _m) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
169 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
170 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
171 class _fileobject: |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
172 """Implements a file object on top of a regular socket object.""" |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
173 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
174 def __init__(self, sock, mode='rb', bufsize=8192): |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
175 self._sock = sock |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
176 self._mode = mode |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
177 if bufsize <= 0: |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
178 bufsize = 512 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
179 self._rbufsize = bufsize |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
180 self._wbufsize = bufsize |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
181 self._rbuf = [ ] |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
182 self._wbuf = [ ] |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
183 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
184 def close(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
185 try: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
186 if self._sock: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
187 self.flush() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
188 finally: |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
189 self._sock = None |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
190 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
191 def __del__(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
192 self.close() |
|
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 flush(self): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
195 if self._wbuf: |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
196 buffer = ''.join(self._wbuf) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
197 self._sock.sendall(buffer) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
198 self._wbuf = [ ] |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
199 |
| 23844 | 200 def fileno(self): |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
201 return self._sock.fileno() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
202 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
203 def write(self, data): |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
204 self._wbuf.append (data) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
205 # A _wbufsize of 1 means we're doing unbuffered IO. |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
206 # Flush accordingly. |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
207 if self._wbufsize == 1: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
208 if '\n' in data: |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
209 self.flush () |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
210 elif self.__get_wbuf_len() >= self._wbufsize: |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
211 self.flush() |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
212 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
213 def writelines(self, list): |
|
21962
d3b1f2c0ebcf
The Grande 'sendall()' patch, copied from release21-maint. Fixes #516715.
Martin v. Löwis <martin@v.loewis.de>
parents:
21959
diff
changeset
|
214 filter(self._sock.sendall, list) |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
215 self.flush() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
216 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
217 def __get_wbuf_len (self): |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
218 buf_len = 0 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
219 for i in [len(x) for x in self._wbuf]: |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
220 buf_len += i |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
221 return buf_len |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
222 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
223 def __get_rbuf_len(self): |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
224 buf_len = 0 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
225 for i in [len(x) for x in self._rbuf]: |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
226 buf_len += i |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
227 return buf_len |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
228 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
229 def read(self, size=-1): |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
230 buf_len = self.__get_rbuf_len() |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
231 while size < 0 or buf_len < size: |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
232 recv_size = max(self._rbufsize, size - buf_len) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
233 data = self._sock.recv(recv_size) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
234 if not data: |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
235 break |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
236 buf_len += len(data) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
237 self._rbuf.append(data) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
238 # Clear the rbuf at the end so we're not affected by |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
239 # an exception during a recv |
| 23844 | 240 data = ''.join(self._rbuf) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
241 self._rbuf = [ ] |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
242 if buf_len > size and size >= 0: |
| 23844 | 243 self._rbuf.append(data[size:]) |
| 244 data = data[:size] | |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
245 return data |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
246 |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
247 def readline(self, size=-1): |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
248 index = -1 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
249 buf_len = self.__get_rbuf_len() |
| 23844 | 250 if self._rbuf: |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
251 index = min([x.find('\n') for x in self._rbuf]) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
252 while index < 0 and (size < 0 or buf_len < size): |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
253 recv_size = max(self._rbufsize, size - buf_len) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
254 data = self._sock.recv(recv_size) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
255 if not data: |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
256 break |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
257 buf_len += len(data) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
258 self._rbuf.append(data) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
259 index = data.find('\n') |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
260 data = ''.join(self._rbuf) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
261 self._rbuf = [ ] |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
262 index = data.find('\n') |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
263 if index >= 0: |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
264 index += 1 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
265 elif buf_len > size: |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
266 index = size |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
267 else: |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
268 index = buf_len |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
269 self._rbuf.append(data[index:]) |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
270 data = data[:index] |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
271 return data |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
272 |
|
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21971
diff
changeset
|
273 def readlines(self, sizehint=0): |
|
14682
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
274 total = 0 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
275 list = [] |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
276 while 1: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
277 line = self.readline() |
| 23844 | 278 if not line: |
| 279 break | |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
280 list.append(line) |
|
14682
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
281 total += len(line) |
|
295126433856
Support sizehint in _fileobject.readlines, as documented.
Martin v. Löwis <martin@v.loewis.de>
parents:
14592
diff
changeset
|
282 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
|
283 break |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
284 return list |
