Mercurial > cpython-lru_cache-weakref
annotate Lib/socket.py @ 74006:88198b93ff2f
Issue #13560: Add PyUnicode_EncodeLocale()
* Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not
available
* Document my last changes in Misc/NEWS
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Sat, 17 Dec 2011 04:13:41 +0100 |
| parents | 90f81045613a |
| children | d2f0c3eb1eed |
| 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 |
|
58114
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
54514
diff
changeset
|
26 create_connection() -- connects to an address, with an optional timeout and |
|
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
54514
diff
changeset
|
27 optional source address. |
|
14007
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 [*] not available on all platforms! |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
31 Special objects: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
32 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
33 SocketType -- type object for socket objects |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
34 error -- exception raised for I/O errors |
|
28818
57e61588571b
Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents:
28443
diff
changeset
|
35 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
|
36 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
37 Integer constants: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
38 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
39 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
|
40 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
|
41 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
42 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
|
43 the setsockopt() and getsockopt() methods. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 """ |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
45 |
|
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 _socket |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
47 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
|
48 |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
49 import os, sys, io |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
50 |
|
28890
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
51 try: |
|
58146
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58114
diff
changeset
|
52 import errno |
|
28890
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
53 except ImportError: |
|
58146
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58114
diff
changeset
|
54 errno = None |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58114
diff
changeset
|
55 EBADF = getattr(errno, 'EBADF', 9) |
|
64817
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
56 EAGAIN = getattr(errno, 'EAGAIN', 11) |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
57 EWOULDBLOCK = getattr(errno, 'EWOULDBLOCK', 11) |
|
28890
39f4b97ce828
Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents:
28888
diff
changeset
|
58 |
|
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
|
59 __all__ = ["getfqdn", "create_connection"] |
| 16613 | 60 __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
|
61 |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
62 |
|
4a07a288f5c4
Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
42974
diff
changeset
|
63 _realsocket = socket |
| 16613 | 64 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 # WSA error codes |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
66 if sys.platform.lower().startswith("win"): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 errorTab = {} |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
68 errorTab[10004] = "The operation was interrupted." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 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
|
70 errorTab[10013] = "Permission denied." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
71 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
|
72 errorTab[10022] = "An invalid operation was attempted." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
73 errorTab[10035] = "The socket operation would block" |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
74 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
|
75 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
|
76 errorTab[10054] = "The connection has been reset." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
77 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
|
78 errorTab[10060] = "The operation timed out." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
79 errorTab[10061] = "Connection refused." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
80 errorTab[10063] = "The name is too long." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
81 errorTab[10064] = "The host is down." |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 errorTab[10065] = "The host is unreachable." |
|
17071
b45ae8c467aa
add errorTab to __all__ on win*
Skip Montanaro <skip@pobox.com>
parents:
16949
diff
changeset
|
83 __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
|
84 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
86 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
|
87 |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
88 """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
|
89 |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
90 __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
|
91 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
92 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
|
93 _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
|
94 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
|
95 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
|
96 |
|
64574
caf0b4b0569e
Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64534
diff
changeset
|
97 def __enter__(self): |
|
caf0b4b0569e
Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64534
diff
changeset
|
98 return self |
|
caf0b4b0569e
Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64534
diff
changeset
|
99 |
|
caf0b4b0569e
Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64534
diff
changeset
|
100 def __exit__(self, *args): |
|
caf0b4b0569e
Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64534
diff
changeset
|
101 if not self._closed: |
|
caf0b4b0569e
Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64534
diff
changeset
|
102 self.close() |
|
caf0b4b0569e
Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64534
diff
changeset
|
103 |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
104 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
|
105 """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
|
106 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
|
107 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
|
108 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
|
109 self.__class__.__name__, |
|
65707
cff78ffb932a
Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
110 getattr(self, '_closed', False) and " [closed] " or "", |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
111 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
|
112 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
|
113 |
|
68774
5e13e5e6df30
Issue #11127: Raise a TypeError when trying to pickle a socket object.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
114 def __getstate__(self): |
|
5e13e5e6df30
Issue #11127: Raise a TypeError when trying to pickle a socket object.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
115 raise TypeError("Cannot serialize socket object") |
|
5e13e5e6df30
Issue #11127: Raise a TypeError when trying to pickle a socket object.
Antoine Pitrou <solipsis@pitrou.net>
parents:
68257
diff
changeset
|
116 |
|
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
|
117 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
|
118 """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
|
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 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
|
121 """ |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
122 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
|
123 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
|
124 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
|
125 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
|
126 |
|
41809
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
127 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
|
128 """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
|
129 |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
130 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
|
131 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
|
132 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
|
133 """ |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
134 fd, addr = self._accept() |
|
67382
ed0259230611
Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents:
67344
diff
changeset
|
135 sock = socket(self.family, self.type, self.proto, fileno=fd) |
|
ed0259230611
Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents:
67344
diff
changeset
|
136 # Issue #7995: if no default timeout is set and the listening |
|
ed0259230611
Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents:
67344
diff
changeset
|
137 # socket had a (non-zero) timeout, force the new socket in blocking |
|
ed0259230611
Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents:
67344
diff
changeset
|
138 # mode to override platform-specific socket flags inheritance. |
|
ed0259230611
Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents:
67344
diff
changeset
|
139 if getdefaulttimeout() is None and self.gettimeout(): |
|
ed0259230611
Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents:
67344
diff
changeset
|
140 sock.setblocking(True) |
|
ed0259230611
Issue #7995: When calling accept() on a socket with a timeout, the returned
Antoine Pitrou <solipsis@pitrou.net>
parents:
67344
diff
changeset
|
141 return sock, 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
|
142 |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
143 def makefile(self, mode="r", buffering=None, *, |
|
65259
3619c1214007
Merged revisions 85420 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64766
diff
changeset
|
144 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
|
145 """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
|
146 |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
147 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
|
148 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
|
149 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
|
150 """ |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 rawmode = "" |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 rawmode += "w" |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 else: |
|
8e062e572ea4
Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
178 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
|
179 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
|
180 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
|
181 return buffer |
|
65259
3619c1214007
Merged revisions 85420 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64766
diff
changeset
|
182 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
|
183 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
|
184 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
|
185 |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
186 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
|
187 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
|
188 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
|
189 if self._closed: |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
190 self.close() |
|
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
191 |
|
64332
62930cb05ed8
Issue #808164: Fixed socket.close to avoid references to globals, to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
62903
diff
changeset
|
192 def _real_close(self, _ss=_socket.socket): |
| 64333 | 193 # This function should not reference any globals. See issue #808164. |
|
64332
62930cb05ed8
Issue #808164: Fixed socket.close to avoid references to globals, to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents:
62903
diff
changeset
|
194 _ss.close(self) |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
43881
diff
changeset
|
195 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
196 def close(self): |
| 64333 | 197 # This function should not reference any globals. See issue #808164. |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
198 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
|
199 if self._io_refs <= 0: |
|
44305
19f894ae864a
update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents:
43881
diff
changeset
|
200 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
|
201 |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
202 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
|
203 """ 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
|
204 |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
205 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
|
206 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
|
207 """ |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43851
diff
changeset
|
208 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
|
209 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
|
210 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
211 |
|
64747
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
212 if hasattr(_socket, "socketpair"): |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
213 |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
214 def socketpair(family=None, type=SOCK_STREAM, proto=0): |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
215 """socketpair([family[, type[, proto]]]) -> (socket object, socket object) |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
216 |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
217 Create a pair of socket objects from the sockets returned by the platform |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
218 socketpair() function. |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
219 The arguments are the same as for socket() except the default family is |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
220 AF_UNIX if defined on the platform; otherwise, the default is AF_INET. |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
221 """ |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
222 if family is None: |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
223 try: |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
224 family = AF_UNIX |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
225 except NameError: |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
226 family = AF_INET |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
227 a, b = _socket.socketpair(family, type, proto) |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
228 a = socket(family, type, proto, a.detach()) |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
229 b = socket(family, type, proto, b.detach()) |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
230 return a, b |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
231 |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64574
diff
changeset
|
232 |
|
64817
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
233 _blocking_errnos = { EAGAIN, EWOULDBLOCK } |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
234 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
235 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
|
236 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
237 """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
|
238 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
239 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
|
240 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
|
241 """ |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
242 |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
243 # 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
|
244 # 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
|
245 # - 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
|
246 # write() on a socket handle) |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
247 # - 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
|
248 # 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
|
249 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
250 # XXX More docs |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
251 |
|
43851
9a1281391c46
Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents:
43400
diff
changeset
|
252 def __init__(self, sock, mode): |
|
49800
6aef28ebbd6c
make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents:
49782
diff
changeset
|
253 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
|
254 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
|
255 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
|
256 self._sock = sock |
|
49800
6aef28ebbd6c
make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents:
49782
diff
changeset
|
257 if "b" not in mode: |
|
6aef28ebbd6c
make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents:
49782
diff
changeset
|
258 mode += "b" |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
259 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
|
260 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
|
261 self._writing = "w" in mode |
|
68159
17d1c6cede7e
Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents:
67382
diff
changeset
|
262 self._timeout_occurred = False |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
263 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
264 def readinto(self, b): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
265 """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
|
266 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
|
267 are available, None is returned. |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
268 |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
269 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
|
270 was shutdown at the other end. |
|
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
271 """ |
|
42904
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
272 self._checkClosed() |
|
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
273 self._checkReadable() |
|
68159
17d1c6cede7e
Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents:
67382
diff
changeset
|
274 if self._timeout_occurred: |
|
17d1c6cede7e
Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents:
67382
diff
changeset
|
275 raise IOError("cannot read from timed out object") |
|
58146
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58114
diff
changeset
|
276 while True: |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58114
diff
changeset
|
277 try: |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58114
diff
changeset
|
278 return self._sock.recv_into(b) |
|
68159
17d1c6cede7e
Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents:
67382
diff
changeset
|
279 except timeout: |
|
17d1c6cede7e
Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents:
67382
diff
changeset
|
280 self._timeout_occurred = True |
|
17d1c6cede7e
Issue #7322: Trying to read from a socket's file-like object after a timeout
Antoine Pitrou <solipsis@pitrou.net>
parents:
67382
diff
changeset
|
281 raise |
|
73085
90f81045613a
Use InterruptedError instead of checking for EINTR
Antoine Pitrou <solipsis@pitrou.net>
parents:
68774
diff
changeset
|
282 except InterruptedError: |
|
90f81045613a
Use InterruptedError instead of checking for EINTR
Antoine Pitrou <solipsis@pitrou.net>
parents:
68774
diff
changeset
|
283 continue |
|
58146
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58114
diff
changeset
|
284 except error as e: |
|
73085
90f81045613a
Use InterruptedError instead of checking for EINTR
Antoine Pitrou <solipsis@pitrou.net>
parents:
68774
diff
changeset
|
285 if e.args[0] in _blocking_errnos: |
|
64817
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
286 return None |
|
58146
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58114
diff
changeset
|
287 raise |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
288 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
289 def write(self, b): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
290 """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
|
291 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
|
292 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
|
293 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
|
294 """ |
|
42904
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
295 self._checkClosed() |
|
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
296 self._checkWritable() |
|
64817
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
297 try: |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
298 return self._sock.send(b) |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
299 except error as e: |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
300 # XXX what about EINTR? |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
301 if e.args[0] in _blocking_errnos: |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
302 return None |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64764
diff
changeset
|
303 raise |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
304 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
305 def readable(self): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
306 """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
|
307 """ |
|
42904
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
308 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
|
309 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
310 def writable(self): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
311 """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
|
312 """ |
|
42904
cd235027e64c
Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
42434
diff
changeset
|
313 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
|
314 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
315 def fileno(self): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
316 """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
|
317 """ |
|
50927
5c3bf31d04fd
Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
318 self._checkClosed() |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
319 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
|
320 |
|
49782
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
321 @property |
|
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
322 def name(self): |
|
67344
f4a808f5a66d
Issue #10819: SocketIO.name property returns -1 when its closed, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65707
diff
changeset
|
323 if not self.closed: |
|
f4a808f5a66d
Issue #10819: SocketIO.name property returns -1 when its closed, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65707
diff
changeset
|
324 return self.fileno() |
|
f4a808f5a66d
Issue #10819: SocketIO.name property returns -1 when its closed, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65707
diff
changeset
|
325 else: |
|
f4a808f5a66d
Issue #10819: SocketIO.name property returns -1 when its closed, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65707
diff
changeset
|
326 return -1 |
|
49782
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
327 |
|
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
328 @property |
|
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47559
diff
changeset
|
329 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
|
330 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
|
331 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
332 def close(self): |
|
64766
a97d12e5465f
Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64536
diff
changeset
|
333 """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
|
334 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
|
335 """ |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
336 if self.closed: |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
337 return |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42301
diff
changeset
|
338 io.RawIOBase.close(self) |
|
50927
5c3bf31d04fd
Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
339 self._sock._decref_socketios() |
|
5c3bf31d04fd
Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
340 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
|
341 |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
342 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
343 def getfqdn(name=''): |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
344 """Get fully qualified domain name from name. |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
345 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
346 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
|
347 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
348 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
|
349 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
|
350 from gethostname() is returned. |
|
14007
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
351 """ |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
352 name = name.strip() |
|
14014
3d68d5bccd40
updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
14007
diff
changeset
|
353 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
|
354 name = gethostname() |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
355 try: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
356 hostname, aliases, ipaddrs = gethostbyaddr(name) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
357 except error: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
358 pass |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
359 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
360 aliases.insert(0, hostname) |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
361 for name in aliases: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
362 if '.' in name: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
363 break |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
364 else: |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
365 name = hostname |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
366 return name |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
367 |
|
eda09f824a9f
Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
368 |
|
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
|
369 _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
|
370 |
|
58114
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
54514
diff
changeset
|
371 def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT, |
|
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
54514
diff
changeset
|
372 source_address=None): |
|
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
|
373 """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
|
374 |
|
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
|
375 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
|
376 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
|
377 *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
|
378 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
|
379 global default timeout setting returned by :func:`getdefaulttimeout` |
|
58114
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
54514
diff
changeset
|
380 is used. If *source_address* is set it must be a tuple of (host, port) |
|
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
54514
diff
changeset
|
381 for the socket to bind as a source address before making the connection. |
|
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
54514
diff
changeset
|
382 An host of '' or port 0 tells the OS to use the default. |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
383 """ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
384 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
385 host, port = address |
|
64536
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
386 err = None |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
387 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
|
388 af, socktype, proto, canonname, sa = res |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
389 sock = None |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
390 try: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
391 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
|
392 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
|
393 sock.settimeout(timeout) |
|
58114
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
54514
diff
changeset
|
394 if source_address: |
|
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
54514
diff
changeset
|
395 sock.bind(source_address) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
396 sock.connect(sa) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
397 return sock |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
398 |
|
64536
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
399 except error as _: |
|
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
400 err = _ |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
401 if sock is not None: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
402 sock.close() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
403 |
|
64536
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
404 if err is not None: |
|
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
405 raise err |
|
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
406 else: |
|
2400f55ea312
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
54514
diff
changeset
|
407 raise error("getaddrinfo returns an empty list") |
