Mercurial > cpython-lru_cache-weakref
annotate Lib/socket.py @ 67467:abe201dfd3dc 3.1
Merged revisions 87861,87863 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r87861 | antoine.pitrou | 2011-01-08 11:23:29 +0100 (sam., 08 janv. 2011) | 3 lines
Fix test_ssl after r87849
........
r87863 | antoine.pitrou | 2011-01-08 11:28:11 +0100 (sam., 08 janv. 2011) | 3 lines
Add EHOSTUNREACH ('No route to host') to the errnos trapped by transient_internet().
........
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Sat, 08 Jan 2011 10:31:09 +0000 |
| parents | 3619c1214007 |
| children | 3c9232a684ae |
| 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 |
|
33063
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32160
diff
changeset
|
13 socketpair() -- create a pair of new socket objects [*] |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 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
|
15 gethostname() -- return the current hostname |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 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
|
17 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
|
18 getservbyname() -- map a service name and a protocol name to a port number |
|
54514
7234a83a8b19
Merged revisions 73138 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
53072
diff
changeset
|
19 getprotobyname() -- map a protocol name (e.g. 'tcp') to a number |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 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
|
21 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
|
22 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
|
23 inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89) |
|
24293
640370f08e2a
Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents:
23844
diff
changeset
|
24 socket.getdefaulttimeout() -- get the default timeout value |
|
640370f08e2a
Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents:
23844
diff
changeset
|
25 socket.setdefaulttimeout() -- set the default timeout value |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
26 create_connection() -- connects to an address, with an optional timeout |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
28 [*] not available on all platforms! |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
29 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 Special objects: |
|
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 SocketType -- type object for socket objects |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
33 error -- exception raised for I/O errors |
|
28818
57e61588571b
Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents:
28443
diff
changeset
|
34 has_ipv6 -- boolean value indicating if IPv6 is supported |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
35 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
36 Integer constants: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
37 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
38 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
|
39 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
|
40 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
41 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
|
42 the setsockopt() and getsockopt() methods. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
43 """ |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 |
|
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
|
45 import _socket |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
46 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
|
47 |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
48 import os, sys, io |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
49 |
|
28890
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
50 try: |
|
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
51 from errno import EBADF |
|
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
52 except ImportError: |
|
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
53 EBADF = 9 |
|
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
54 |
|
53072
7e978ca42930
Merged revisions 70980,71059,71225,71234,71241,71243,71249,71251,71255,71266,71299,71329,71397-71398,71486 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50927
diff
changeset
|
55 __all__ = ["getfqdn", "create_connection"] |
| 16613 | 56 __all__.extend(os._get_exports_list(_socket)) |
|
43095
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
57 |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
58 |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
59 _realsocket = socket |
| 16613 | 60 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
61 # WSA error codes |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
62 if sys.platform.lower().startswith("win"): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 errorTab = {} |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
64 errorTab[10004] = "The operation was interrupted." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 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
|
66 errorTab[10013] = "Permission denied." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 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
|
68 errorTab[10022] = "An invalid operation was attempted." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 errorTab[10035] = "The socket operation would block" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 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
|
71 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
|
72 errorTab[10054] = "The connection has been reset." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
73 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
|
74 errorTab[10060] = "The operation timed out." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
75 errorTab[10061] = "Connection refused." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
76 errorTab[10063] = "The name is too long." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
77 errorTab[10064] = "The host is down." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
78 errorTab[10065] = "The host is unreachable." |
|
17071
b45ae8c467aa
add errorTab to __all__ on win*
Skip Montanaro <skip@pobox.com>
parents:
16949
diff
changeset
|
79 __all__.append("errorTab") |
|
24663
b265e76bab88
Extend __all__ with the exports list of the _ssl module.
Guido van Rossum <guido@python.org>
parents:
24662
diff
changeset
|
80 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
81 |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
82 class socket(_socket.socket): |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
83 |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
84 """A subclass of _socket.socket adding the makefile() method.""" |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
85 |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
86 __slots__ = ["__weakref__", "_io_refs", "_closed"] |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
87 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
88 def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None): |
|
43881
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
89 _socket.socket.__init__(self, family, type, proto, fileno) |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
90 self._io_refs = 0 |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
91 self._closed = False |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
92 |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
93 def __repr__(self): |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
94 """Wrap __repr__() to reveal the real class name.""" |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
95 s = _socket.socket.__repr__(self) |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
96 if s.startswith("<socket object"): |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
97 s = "<%s.%s%s%s" % (self.__class__.__module__, |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
98 self.__class__.__name__, |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
99 (self._closed and " [closed] ") or "", |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
100 s[7:]) |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
101 return s |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
102 |
|
43881
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
103 def dup(self): |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
104 """dup() -> socket object |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
105 |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
106 Return a new socket object connected to the same system resource. |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
107 """ |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
108 fd = dup(self.fileno()) |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
109 sock = self.__class__(self.family, self.type, self.proto, fileno=fd) |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
110 sock.settimeout(self.gettimeout()) |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
111 return sock |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
112 |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
113 def accept(self): |
|
43881
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
114 """accept() -> (socket object, address info) |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
115 |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
116 Wait for an incoming connection. Return a new socket |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
117 representing the connection, and the address of the client. |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
118 For IP sockets, the address info is a pair (hostaddr, port). |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
119 """ |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
120 fd, addr = self._accept() |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
121 return socket(self.family, self.type, self.proto, fileno=fd), addr |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
122 |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
123 def makefile(self, mode="r", buffering=None, *, |
|
65259
3619c1214007
Merged revisions 85420 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64766
diff
changeset
|
124 encoding=None, errors=None, newline=None): |
|
43881
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
125 """makefile(...) -> an I/O stream connected to the socket |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
126 |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
127 The arguments are as for io.open() after the filename, |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
128 except the only mode characters supported are 'r', 'w' and 'b'. |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
129 The semantics are similar too. (XXX refactor to share code?) |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
130 """ |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
131 for c in mode: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
132 if c not in {"r", "w", "b"}: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
133 raise ValueError("invalid mode %r (only r, w, b allowed)") |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
134 writing = "w" in mode |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
135 reading = "r" in mode or not writing |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
136 assert reading or writing |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
137 binary = "b" in mode |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
138 rawmode = "" |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
139 if reading: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
140 rawmode += "r" |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
141 if writing: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
142 rawmode += "w" |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
143 raw = SocketIO(self, rawmode) |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
144 self._io_refs += 1 |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
145 if buffering is None: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
146 buffering = -1 |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
147 if buffering < 0: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
148 buffering = io.DEFAULT_BUFFER_SIZE |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
149 if buffering == 0: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
150 if not binary: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
151 raise ValueError("unbuffered streams must be binary") |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
152 return raw |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
153 if reading and writing: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
154 buffer = io.BufferedRWPair(raw, raw, buffering) |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
155 elif reading: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
156 buffer = io.BufferedReader(raw, buffering) |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
157 else: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
158 assert writing |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
159 buffer = io.BufferedWriter(raw, buffering) |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
160 if binary: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
161 return buffer |
|
65259
3619c1214007
Merged revisions 85420 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64766
diff
changeset
|
162 text = io.TextIOWrapper(buffer, encoding, errors, newline) |
|
42301
f940e470e550
(1) SF patch# 1755214 by Amaury Forgeot d'Arc which makes some tests
Guido van Rossum <guido@python.org>
parents:
41809
diff
changeset
|
163 text.mode = mode |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
164 return text |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
165 |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
166 def _decref_socketios(self): |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
167 if self._io_refs > 0: |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
168 self._io_refs -= 1 |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
169 if self._closed: |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
170 self.close() |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
171 |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
43881
diff
changeset
|
172 def _real_close(self): |
|
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
43881
diff
changeset
|
173 _socket.socket.close(self) |
|
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
43881
diff
changeset
|
174 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
175 def close(self): |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
176 self._closed = True |
|
43881
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
177 if self._io_refs <= 0: |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
43881
diff
changeset
|
178 self._real_close() |
|
43881
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
179 |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
180 def fromfd(fd, family, type, proto=0): |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
181 """ fromfd(fd, family, type[, proto]) -> socket object |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
182 |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
183 Create a socket object from a duplicate of the given file |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
184 descriptor. The remaining arguments are the same as for socket(). |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
185 """ |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
186 nfd = dup(fd) |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
187 return socket(family, type, proto, nfd) |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
188 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
189 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
190 class SocketIO(io.RawIOBase): |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
191 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
192 """Raw I/O implementation for stream sockets. |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
193 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
194 This class supports the makefile() method on sockets. It provides |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
195 the raw I/O interface on top of a socket object. |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
196 """ |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
197 |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
198 # One might wonder why not let FileIO do the job instead. There are two |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
199 # main reasons why FileIO is not adapted: |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
200 # - it wouldn't work under Windows (where you can't used read() and |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
201 # write() on a socket handle) |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
202 # - it wouldn't work with socket timeouts (FileIO would ignore the |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
203 # timeout and consider the socket non-blocking) |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
204 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
205 # XXX More docs |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
206 |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
207 def __init__(self, sock, mode): |
|
49800
6aef28ebbd6c
make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents:
49782
diff
changeset
|
208 if mode not in ("r", "w", "rw", "rb", "wb", "rwb"): |
|
42904
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
209 raise ValueError("invalid mode: %r" % mode) |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
210 io.RawIOBase.__init__(self) |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
211 self._sock = sock |
|
49800
6aef28ebbd6c
make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents:
49782
diff
changeset
|
212 if "b" not in mode: |
|
6aef28ebbd6c
make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents:
49782
diff
changeset
|
213 mode += "b" |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
214 self._mode = mode |
|
42904
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
215 self._reading = "r" in mode |
|
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
216 self._writing = "w" in mode |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
217 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
218 def readinto(self, b): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
219 """Read up to len(b) bytes into the writable buffer *b* and return |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
220 the number of bytes read. If the socket is non-blocking and no bytes |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
221 are available, None is returned. |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
222 |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
223 If *b* is non-empty, a 0 return value indicates that the connection |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
224 was shutdown at the other end. |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
225 """ |
|
42904
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
226 self._checkClosed() |
|
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
227 self._checkReadable() |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
228 return self._sock.recv_into(b) |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
229 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
230 def write(self, b): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
231 """Write the given bytes or bytearray object *b* to the socket |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
232 and return the number of bytes written. This can be less than |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
233 len(b) if not all data could be written. If the socket is |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
234 non-blocking and no bytes could be written None is returned. |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
235 """ |
|
42904
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
236 self._checkClosed() |
|
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
237 self._checkWritable() |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
238 return self._sock.send(b) |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
239 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
240 def readable(self): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
241 """True if the SocketIO is open for reading. |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
242 """ |
|
42904
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
243 return self._reading and not self.closed |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
244 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
245 def writable(self): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
246 """True if the SocketIO is open for writing. |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
247 """ |
|
42904
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
248 return self._writing and not self.closed |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
249 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
250 def fileno(self): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
251 """Return the file descriptor of the underlying socket. |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
252 """ |
|
50927
5c3bf31d04fd
Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
253 self._checkClosed() |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
254 return self._sock.fileno() |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
255 |
|
49782
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
256 @property |
|
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
257 def name(self): |
|
50927
5c3bf31d04fd
Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
258 return self.fileno() |
|
49782
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
259 |
|
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
260 @property |
|
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
261 def mode(self): |
|
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
262 return self._mode |
|
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
263 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
264 def close(self): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
265 """Close the SocketIO object. This doesn't close the underlying |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
266 socket, except if all references to it have disappeared. |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
267 """ |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
268 if self.closed: |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
269 return |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
270 io.RawIOBase.close(self) |
|
50927
5c3bf31d04fd
Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
271 self._sock._decref_socketios() |
|
5c3bf31d04fd
Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
272 self._sock = None |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
273 |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
274 def __del__(self): |
|
50927
5c3bf31d04fd
Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
275 if not self.closed: |
|
5c3bf31d04fd
Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
276 self._sock._decref_socketios() |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
277 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
278 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
279 def getfqdn(name=''): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
280 """Get fully qualified domain name from name. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
281 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
282 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
|
283 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
284 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
|
285 possibly existing aliases. In case no FQDN is available, hostname |
|
34733
e83309d43a82
Fix test for socket.getfqdn() to also include the name returned by
Brett Cannon <bcannon@gmail.com>
parents:
33063
diff
changeset
|
286 from gethostname() is returned. |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
287 """ |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
288 name = name.strip() |
|
14014
3d68d5bccd40
updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
14007
diff
changeset
|
289 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
|
290 name = gethostname() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
291 try: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
292 hostname, aliases, ipaddrs = gethostbyaddr(name) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
293 except error: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
294 pass |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
295 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
296 aliases.insert(0, hostname) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
297 for name in aliases: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
298 if '.' in name: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
299 break |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
300 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
301 name = hostname |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
302 return name |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
303 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
304 |
|
47559
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
305 _GLOBAL_DEFAULT_TIMEOUT = object() |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
306 |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
307 def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT): |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
308 """Connect to *address* and return the socket object. |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
309 |
|
47559
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
310 Convenience function. Connect to *address* (a 2-tuple ``(host, |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
311 port)``) and return the socket object. Passing the optional |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
312 *timeout* parameter will set the timeout on the socket instance |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
313 before attempting to connect. If no *timeout* is supplied, the |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
314 global default timeout setting returned by :func:`getdefaulttimeout` |
|
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
315 is used. |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
316 """ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
317 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
318 host, port = address |
|
64536
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
319 err = None |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
320 for res in getaddrinfo(host, port, 0, SOCK_STREAM): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
321 af, socktype, proto, canonname, sa = res |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
322 sock = None |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
323 try: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
324 sock = socket(af, socktype, proto) |
|
47559
2925b568aacc
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents:
44305
diff
changeset
|
325 if timeout is not _GLOBAL_DEFAULT_TIMEOUT: |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
326 sock.settimeout(timeout) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
327 sock.connect(sa) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
328 return sock |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
329 |
|
64536
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
330 except error as _: |
|
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
331 err = _ |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
332 if sock is not None: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
333 sock.close() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
334 |
|
64536
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
335 if err is not None: |
|
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
336 raise err |
|
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
337 else: |
|
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
338 raise error("getaddrinfo returns an empty list") |
