annotate Lib/socket.py @ 62799:dcc2e8bf64cf

Fix: Issue9091 Minor documentation clarification.
author Senthil Kumaran <orsenthil@gmail.com>
date Mon, 05 Jul 2010 11:41:42 +0000
parents efbac7d3a368
children 4eff9c35c54a
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 [*]
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)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
56 EINTR = getattr(errno, 'EINTR', 4)
28890
39f4b97ce828 Extract the errno value to use from the errno module if possible.
Fred Drake <fdrake@acm.org>
parents: 28888
diff changeset
57
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
58 __all__ = ["getfqdn", "create_connection"]
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15872
diff changeset
59 __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
60
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 _realsocket = socket
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15872
diff changeset
63
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 # WSA error codes
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 if sys.platform.lower().startswith("win"):
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 errorTab = {}
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 errorTab[10004] = "The operation was interrupted."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 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
69 errorTab[10013] = "Permission denied."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 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
71 errorTab[10022] = "An invalid operation was attempted."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72 errorTab[10035] = "The socket operation would block"
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73 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
74 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
75 errorTab[10054] = "The connection has been reset."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76 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
77 errorTab[10060] = "The operation timed out."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78 errorTab[10061] = "Connection refused."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
79 errorTab[10063] = "The name is too long."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80 errorTab[10064] = "The host is down."
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81 errorTab[10065] = "The host is unreachable."
17071
b45ae8c467aa add errorTab to __all__ on win*
Skip Montanaro <skip@pobox.com>
parents: 16949
diff changeset
82 __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
83
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
85 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
86
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
87 """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
88
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
89 __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
90
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
91 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
92 _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
93 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
94 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
95
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
96 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
97 """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
98 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
99 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
100 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
101 self.__class__.__name__,
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
102 (self._closed and " [closed] ") or "",
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
103 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
104 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
105
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
106 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
107 """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
108
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
109 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
110 """
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
111 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
112 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
113 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
114 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
115
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
116 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
117 """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
118
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 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
120 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
121 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
122 """
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 fd, addr = self._accept()
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
124 return socket(self.family, self.type, self.proto, fileno=fd), addr
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
125
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
126 def makefile(self, mode="r", buffering=None, *,
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
127 encoding=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
128 """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
129
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
130 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
131 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
132 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
133 """
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
134 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
135 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
136 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
137 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
138 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
139 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
140 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
141 rawmode = ""
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
142 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
143 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
144 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
145 rawmode += "w"
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
146 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
147 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
148 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
149 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
150 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
151 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
152 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
153 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
154 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
155 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
156 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
157 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
158 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
159 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
160 else:
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
161 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
162 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
163 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
164 return buffer
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
165 text = io.TextIOWrapper(buffer, encoding, 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
166 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
167 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
168
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
169 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
170 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
171 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
172 if self._closed:
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
173 self.close()
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
174
44305
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 43881
diff changeset
175 def _real_close(self):
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 43881
diff changeset
176 _socket.socket.close(self)
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 43881
diff changeset
177
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
178 def close(self):
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
179 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
180 if self._io_refs <= 0:
44305
19f894ae864a update to fix leak in SSL code
Bill Janssen <janssen@parc.com>
parents: 43881
diff changeset
181 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
182
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
183 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
184 """ 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
185
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
186 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
187 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
188 """
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43851
diff changeset
189 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
190 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
191
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
192
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
193 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
194
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
195 """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
196
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
197 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
198 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
199 """
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
200
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
201 # XXX More docs
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
202
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
203 def __init__(self, sock, mode):
49800
6aef28ebbd6c make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents: 49782
diff changeset
204 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
205 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
206 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
207 self._sock = sock
49800
6aef28ebbd6c make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents: 49782
diff changeset
208 if "b" not in mode:
6aef28ebbd6c make FileIO.mode always include 'b'
Benjamin Peterson <benjamin@python.org>
parents: 49782
diff changeset
209 mode += "b"
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
210 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
211 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
212 self._writing = "w" in mode
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
213
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
214 def readinto(self, b):
42904
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
215 self._checkClosed()
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
216 self._checkReadable()
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
217 while True:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
218 try:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
219 return self._sock.recv_into(b)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
220 except error as e:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
221 if e.args[0] == EINTR:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
222 continue
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
223 raise
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
224
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
225 def write(self, b):
42904
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
226 self._checkClosed()
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
227 self._checkWritable()
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
228 return self._sock.send(b)
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
229
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
230 def readable(self):
42904
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
231 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
232
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
233 def writable(self):
42904
cd235027e64c Changes to io.py and socket.py by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
234 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
235
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
236 def fileno(self):
50927
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
237 self._checkClosed()
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
238 return self._sock.fileno()
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
239
49782
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
240 @property
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
241 def name(self):
50927
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
242 return self.fileno()
49782
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
243
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
244 @property
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47559
diff changeset
245 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
246 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
247
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
248 def close(self):
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
249 if self.closed:
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
250 return
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42301
diff changeset
251 io.RawIOBase.close(self)
50927
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
252 self._sock._decref_socketios()
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
253 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
254
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
255 def __del__(self):
50927
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
256 if not self.closed:
5c3bf31d04fd Backport r68539, Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
257 self._sock._decref_socketios()
43851
9a1281391c46 Patch 1439 by Bill Janssen. I think this will work.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
258
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
259
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
260 def getfqdn(name=''):
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
261 """Get fully qualified domain name from name.
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
262
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
263 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
264
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
265 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
266 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
267 from gethostname() is returned.
14007
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
268 """
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
269 name = name.strip()
14014
3d68d5bccd40 updated occurences of fqdn algorithm (closes patch #101197)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 14007
diff changeset
270 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
271 name = gethostname()
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
272 try:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
273 hostname, aliases, ipaddrs = gethostbyaddr(name)
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
274 except error:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
275 pass
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
276 else:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
277 aliases.insert(0, hostname)
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
278 for name in aliases:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
279 if '.' in name:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
280 break
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
281 else:
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
282 name = hostname
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
283 return name
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
284
eda09f824a9f Revise the wrapper structure for the socket module:
Fred Drake <fdrake@acm.org>
parents:
diff changeset
285
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
286 _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
287
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
288 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
289 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
290 """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
291
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
292 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
293 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
294 *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
295 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
296 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
297 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
298 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
299 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
300 """
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
301
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
302 msg = "getaddrinfo returns an empty list"
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
303 host, port = address
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
304 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
305 af, socktype, proto, canonname, sa = res
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
306 sock = None
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
307 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
308 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
309 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
310 sock.settimeout(timeout)
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
311 if source_address:
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 54514
diff changeset
312 sock.bind(source_address)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
313 sock.connect(sa)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
314 return sock
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
315
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
316 except error as err:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
317 msg = err
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
318 if sock is not None:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
319 sock.close()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
320
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
321 raise error(msg)