1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Address objects for network connections.
10 from zope.interface import implements
12 from twisted.internet.interfaces import IAddress
13 from twisted.python import util
16 class _IPAddress(object, util.FancyEqMixin):
18 An L{_IPAddress} represents the address of an IP socket endpoint, providing
19 common behavior for IPv4 and IPv6.
21 @ivar type: A string describing the type of transport, either 'TCP' or
24 @ivar host: A string containing the presentation format of the IP address;
25 for example, "127.0.0.1" or "::1".
28 @ivar port: An integer representing the port number.
34 compareAttributes = ('type', 'host', 'port')
36 def __init__(self, type, host, port):
37 assert type in ('TCP', 'UDP')
44 return '%s(%s, %r, %d)' % (
45 self.__class__.__name__, self.type, self.host, self.port)
49 return hash((self.type, self.host, self.port))
53 class IPv4Address(_IPAddress):
55 An L{IPv4Address} represents the address of an IPv4 socket endpoint.
57 @ivar host: A string containing a dotted-quad IPv4 address; for example,
62 def __init__(self, type, host, port, _bwHack=None):
63 _IPAddress.__init__(self, type, host, port)
64 if _bwHack is not None:
65 warnings.warn("twisted.internet.address.IPv4Address._bwHack "
66 "is deprecated since Twisted 11.0",
67 DeprecationWarning, stacklevel=2)
71 class IPv6Address(_IPAddress):
73 An L{IPv6Address} represents the address of an IPv6 socket endpoint.
75 @ivar host: A string containing a colon-separated, hexadecimal formatted
76 IPv6 address; for example, "::1".
82 class UNIXAddress(object, util.FancyEqMixin):
84 Object representing a UNIX socket endpoint.
86 @ivar name: The filename associated with this socket.
92 compareAttributes = ('name', )
94 def __init__(self, name, _bwHack = None):
96 if _bwHack is not None:
97 warnings.warn("twisted.internet.address.UNIXAddress._bwHack is deprecated since Twisted 11.0",
98 DeprecationWarning, stacklevel=2)
101 if getattr(os.path, 'samefile', None) is not None:
102 def __eq__(self, other):
104 overriding L{util.FancyEqMixin} to ensure the os level samefile
105 check is done if the name attributes do not match.
107 res = super(UNIXAddress, self).__eq__(other)
108 if not res and self.name and other.name:
110 return os.path.samefile(self.name, other.name)
117 return 'UNIXAddress(%r)' % (self.name,)
121 if self.name is None:
122 return hash((self.__class__, None))
124 s1 = os.stat(self.name)
125 return hash((s1.st_ino, s1.st_dev))
127 return hash(self.name)
131 # These are for buildFactory backwards compatability due to
132 # stupidity-induced inconsistency.
134 class _ServerFactoryIPv4Address(IPv4Address):
135 """Backwards compatability hack. Just like IPv4Address in practice."""
137 def __eq__(self, other):
138 if isinstance(other, tuple):
139 warnings.warn("IPv4Address.__getitem__ is deprecated. Use attributes instead.",
140 category=DeprecationWarning, stacklevel=2)
141 return (self.host, self.port) == other
142 elif isinstance(other, IPv4Address):
143 a = (self.type, self.host, self.port)
144 b = (other.type, other.host, other.port)