annotate Lib/socket.py @ 83983:ccffce2dde49

Issue #18084: Use sys.byteorder in wave.py. Original patch by Hideaki Takahashi.
author Serhiy Storchaka <storchaka@gmail.com>
date Wed, 29 May 2013 23:38:00 +0300
parents 1410b7790de6
children 8d28d44f3a9a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 [*]
76150
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
15 fromshare() -- create a socket object from data received from socket.share() [*]
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 gethostname() -- return the current hostname
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 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
18 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
19 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
20 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
21 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
22 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
23 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
24 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
25 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
26 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
27 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
28 optional source address.
14007
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 [*] not available on all platforms!
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 Special objects:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 SocketType -- type object for socket objects
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 error -- exception raised for I/O errors
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 28443
diff changeset
36 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
37
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 Integer constants:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 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
41 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
42
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43 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
44 the setsockopt() and getsockopt() methods.
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
45 """
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46
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 import _socket
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 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
49
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
50 import os, sys, io
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51
28890
39f4b97ce828 Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents: 28888
diff changeset
52 try:
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
53 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
54 except ImportError:
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
55 errno = None
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
56 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
57 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
58 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
59
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
60 __all__ = ["getfqdn", "create_connection"]
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15872
diff changeset
61 __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
62
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42974
diff changeset
63
4a07a288f5c4 Merged revisions 57620-57771 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42974
diff changeset
64 _realsocket = socket
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15872
diff changeset
65
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 # WSA error codes
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 if sys.platform.lower().startswith("win"):
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 errorTab = {}
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69 errorTab[10004] = "The operation was interrupted."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 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
71 errorTab[10013] = "Permission denied."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72 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
73 errorTab[10022] = "An invalid operation was attempted."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74 errorTab[10035] = "The socket operation would block"
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
75 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
76 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
77 errorTab[10054] = "The connection has been reset."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78 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
79 errorTab[10060] = "The operation timed out."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80 errorTab[10061] = "Connection refused."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 errorTab[10063] = "The name is too long."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82 errorTab[10064] = "The host is down."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83 errorTab[10065] = "The host is unreachable."
17071
b45ae8c467aa add errorTab to __all__ on win*
Skip Montanaro <skip@pobox.com>
parents: 16949
diff changeset
84 __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
85
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86
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 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
88
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
89 """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
90
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
91 __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
92
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
93 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
94 _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
95 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
96 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
97
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
98 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
99 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
100
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 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
102 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
103 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
104
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
105 def __repr__(self):
83230
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
106 """Wrap __repr__() to reveal the real class name and socket
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
107 address(es).
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
108 """
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
109 closed = getattr(self, '_closed', False)
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
110 s = "<%s.%s%s fd=%i, family=%i, type=%i, proto=%i" \
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
111 % (self.__class__.__module__,
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
112 self.__class__.__name__,
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
113 " [closed]" if closed else "",
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
114 self.fileno(),
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
115 self.family,
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
116 self.type,
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
117 self.proto)
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
118 if not closed:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
119 try:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
120 laddr = self.getsockname()
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
121 if laddr:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
122 s += ", laddr=%s" % str(laddr)
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
123 except error:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
124 pass
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
125 try:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
126 raddr = self.getpeername()
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
127 if raddr:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
128 s += ", raddr=%s" % str(raddr)
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
129 except error:
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
130 pass
1410b7790de6 Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Giampaolo Rodola' <g.rodola@gmail.com>
parents: 81039
diff changeset
131 s += '>'
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
132 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
133
68774
5e13e5e6df30 Issue #11127: Raise a TypeError when trying to pickle a socket object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
134 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
135 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
136
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
137 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
138 """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
139
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
140 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
141 """
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
142 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
143 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
144 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
145 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
146
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
147 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
148 """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
149
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
150 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
151 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
152 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
153 """
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
154 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
155 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
156 # 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
157 # 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
158 # 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
159 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
160 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
161 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
162
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
163 def makefile(self, mode="r", buffering=None, *,
65259
3619c1214007 Merged revisions 85420 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64766
diff changeset
164 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
165 """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
166
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
167 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
168 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
169 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
170 """
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
171 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
172 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
173 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
174 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
175 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
176 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
177 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
178 rawmode = ""
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
179 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
180 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
181 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
182 rawmode += "w"
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
183 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
184 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
185 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
186 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
187 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
188 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
189 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
190 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
191 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
192 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
193 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
194 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
195 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
196 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
197 else:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
198 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
199 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
200 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
201 return buffer
65259
3619c1214007 Merged revisions 85420 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64766
diff changeset
202 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
203 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
204 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
205
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
206 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
207 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
208 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
209 if self._closed:
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
210 self.close()
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
211
64332
62930cb05ed8 Issue #808164: Fixed socket.close to avoid references to globals, to
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 62903
diff changeset
212 def _real_close(self, _ss=_socket.socket):
64333
03268bf9a256 reformat comment
Benjamin Peterson <benjamin@python.org>
parents: 64332
diff changeset
213 # 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
214 _ss.close(self)
44305
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 43881
diff changeset
215
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
216 def close(self):
64333
03268bf9a256 reformat comment
Benjamin Peterson <benjamin@python.org>
parents: 64332
diff changeset
217 # 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
218 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
219 if self._io_refs <= 0:
44305
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 43881
diff changeset
220 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
221
76033
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
222 def detach(self):
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
223 """detach() -> file descriptor
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
224
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
225 Close the socket object without closing the underlying file descriptor.
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
226 The object cannot be used after this call, but the file descriptor
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
227 can be reused for other purposes. The file descriptor is returned.
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
228 """
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
229 self._closed = True
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
230 return super().detach()
3a220feafa15 Issue #13872: socket.detach() now marks the socket closed (as mirrored in the socket repr()).
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
231
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
232 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
233 """ 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
234
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
235 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
236 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
237 """
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
238 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
239 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
240
76150
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
241 if hasattr(_socket.socket, "share"):
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
242 def fromshare(info):
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
243 """ fromshare(info) -> socket object
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
244
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
245 Create a socket object from a the bytes object returned by
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
246 socket.share(pid).
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
247 """
51b4bddd0e92 Issue #14310: inter-process socket duplication for windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 76034
diff changeset
248 return socket(0, 0, 0, info)
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
249
64747
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
250 if hasattr(_socket, "socketpair"):
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
251
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
252 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
253 """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
254
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
255 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
256 socketpair() function.
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
257 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
258 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
259 """
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
260 if family is None:
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
261 try:
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
262 family = AF_UNIX
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
263 except NameError:
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
264 family = AF_INET
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
265 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
266 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
267 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
268 return a, b
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
269
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64574
diff changeset
270
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
271 _blocking_errnos = { EAGAIN, EWOULDBLOCK }
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
272
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
273 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
274
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
275 """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
276
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
277 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
278 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
279 """
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
280
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
281 # 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
282 # 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
283 # - 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
284 # write() on a socket handle)
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
285 # - 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
286 # 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
287
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
288 # XXX More docs
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
289
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
290 def __init__(self, sock, mode):
49800
6aef28ebbd6c make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents: 49782
diff changeset
291 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
292 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
293 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
294 self._sock = sock
49800
6aef28ebbd6c make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents: 49782
diff changeset
295 if "b" not in mode:
6aef28ebbd6c make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents: 49782
diff changeset
296 mode += "b"
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
297 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
298 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
299 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
300 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
301
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
302 def readinto(self, b):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
303 """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
304 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
305 are available, None is returned.
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
306
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
307 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
308 was shutdown at the other end.
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
309 """
42904
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
310 self._checkClosed()
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
311 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
312 if self._timeout_occurred:
81039
7d69d04522e3 Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79027
diff changeset
313 raise OSError("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
314 while True:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
315 try:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
316 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
317 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
318 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
319 raise
73085
90f81045613a Use InterruptedError instead of checking for EINTR
Antoine Pitrou <solipsis@pitrou.net>
parents: 68774
diff changeset
320 except InterruptedError:
90f81045613a Use InterruptedError instead of checking for EINTR
Antoine Pitrou <solipsis@pitrou.net>
parents: 68774
diff changeset
321 continue
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
322 except error as e:
73085
90f81045613a Use InterruptedError instead of checking for EINTR
Antoine Pitrou <solipsis@pitrou.net>
parents: 68774
diff changeset
323 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
324 return None
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
325 raise
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
326
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
327 def write(self, b):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
328 """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
329 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
330 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
331 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
332 """
42904
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
333 self._checkClosed()
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
334 self._checkWritable()
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
335 try:
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
336 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
337 except error as e:
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
338 # XXX what about EINTR?
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
339 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
340 return None
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64764
diff changeset
341 raise
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
342
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
343 def readable(self):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
344 """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
345 """
79026
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
346 if self.closed:
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
347 raise ValueError("I/O operation on closed socket.")
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
348 return self._reading
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
349
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
350 def writable(self):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
351 """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
352 """
79026
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
353 if self.closed:
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
354 raise ValueError("I/O operation on closed socket.")
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
355 return self._writing
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
356
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
357 def seekable(self):
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
358 """True if the SocketIO is open for seeking.
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
359 """
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
360 if self.closed:
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
361 raise ValueError("I/O operation on closed socket.")
fad797916266 Issue #15842: the SocketIO.{readable,writable,seekable} methods now raise ValueError when the file-like object is closed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76033
diff changeset
362 return super().seekable()
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
363
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
364 def fileno(self):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
365 """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
366 """
50927
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
367 self._checkClosed()
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
368 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
369
49782
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
370 @property
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
371 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
372 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
373 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
374 else:
f4a808f5a66d Issue #10819: SocketIO.name property returns -1 when its closed, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65707
diff changeset
375 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
376
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
377 @property
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
378 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
379 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
380
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
381 def close(self):
64766
a97d12e5465f Merged revisions 84825-84826,84830 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64536
diff changeset
382 """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
383 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
384 """
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
385 if self.closed:
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
386 return
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
387 io.RawIOBase.close(self)
50927
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
388 self._sock._decref_socketios()
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
389 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
390
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
391
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
392 def getfqdn(name=''):
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
393 """Get fully qualified domain name from name.
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
394
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
395 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
396
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
397 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
398 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
399 from gethostname() is returned.
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
400 """
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
401 name = name.strip()
14014
3d68d5bccd40 updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 14007
diff changeset
402 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
403 name = gethostname()
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
404 try:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
405 hostname, aliases, ipaddrs = gethostbyaddr(name)
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
406 except error:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
407 pass
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
408 else:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
409 aliases.insert(0, hostname)
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
410 for name in aliases:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
411 if '.' in name:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
412 break
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
413 else:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
414 name = hostname
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
415 return name
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
416
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
417
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
418 _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
419
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
420 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
421 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
422 """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
423
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
424 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
425 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
426 *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
427 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
428 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
429 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
430 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
431 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
432 """
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
433
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
434 host, port = address
64536
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
435 err = None
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
436 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
437 af, socktype, proto, canonname, sa = res
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
438 sock = None
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
439 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
440 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
441 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
442 sock.settimeout(timeout)
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
443 if source_address:
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
444 sock.bind(source_address)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
445 sock.connect(sa)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
446 return sock
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
447
64536
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
448 except error as _:
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
449 err = _
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
450 if sock is not None:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
451 sock.close()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
452
64536
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
453 if err is not None:
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
454 raise err
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
455 else:
2400f55ea312 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54514
diff changeset
456 raise error("getaddrinfo returns an empty list")