1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
"""Address objects for network connections."""
8
from zope.interface import implements
9
from twisted.internet.interfaces import IAddress
12
class IPv4Address(object):
14
Object representing an IPv4 socket endpoint.
16
@ivar type: A string describing the type of transport, either 'TCP' or 'UDP'.
17
@ivar host: A string containing the dotted-quad IP address.
18
@ivar port: An integer representing the port number.
21
# _bwHack is given to old users who think we are a tuple. They expected
22
# addr[0] to define the socket type rather than the address family, so
23
# the value comes from a different namespace than the new .type value:
26
# map = { 'SSL': 'TCP', 'INET': 'TCP', 'INET_UDP': 'UDP' }
30
def __init__(self, type, host, port, _bwHack = None):
31
assert type in ('TCP', 'UDP')
35
self._bwHack = _bwHack
37
def __getitem__(self, index):
38
warnings.warn("IPv4Address.__getitem__ is deprecated. Use attributes instead.",
39
category=DeprecationWarning, stacklevel=2)
40
return (self._bwHack or self.type, self.host, self.port).__getitem__(index)
42
def __getslice__(self, start, stop):
43
warnings.warn("IPv4Address.__getitem__ is deprecated. Use attributes instead.",
44
category=DeprecationWarning, stacklevel=2)
45
return (self._bwHack or self.type, self.host, self.port)[start:stop]
47
def __eq__(self, other):
48
if isinstance(other, tuple):
49
return tuple(self) == other
50
elif isinstance(other, IPv4Address):
51
a = (self.type, self.host, self.port)
52
b = (other.type, other.host, other.port)
57
return 'IPv4Address(%s, %r, %d)' % (self.type, self.host, self.port)
60
class UNIXAddress(object):
62
Object representing a UNIX socket endpoint.
64
@ivar name: The filename associated with this socket.
70
def __init__(self, name, _bwHack='UNIX'):
72
self._bwHack = _bwHack
74
def __getitem__(self, index):
75
warnings.warn("UNIXAddress.__getitem__ is deprecated. Use attributes instead.",
76
category=DeprecationWarning, stacklevel=2)
77
return (self._bwHack, self.name).__getitem__(index)
79
def __getslice__(self, start, stop):
80
warnings.warn("UNIXAddress.__getitem__ is deprecated. Use attributes instead.",
81
category=DeprecationWarning, stacklevel=2)
82
return (self._bwHack, self.name)[start:stop]
84
def __eq__(self, other):
85
if isinstance(other, tuple):
86
return tuple(self) == other
87
elif isinstance(other, UNIXAddress):
89
return os.path.samefile(self.name, other.name)
95
return 'UNIXSocket(%r)' % (self.name,)
98
# These are for buildFactory backwards compatability due to
99
# stupidity-induced inconsistency.
101
class _ServerFactoryIPv4Address(IPv4Address):
102
"""Backwards compatability hack. Just like IPv4Address in practice."""
104
def __eq__(self, other):
105
if isinstance(other, tuple):
106
warnings.warn("IPv4Address.__getitem__ is deprecated. Use attributes instead.",
107
category=DeprecationWarning, stacklevel=2)
108
return (self.host, self.port) == other
109
elif isinstance(other, IPv4Address):
110
a = (self.type, self.host, self.port)
111
b = (other.type, other.host, other.port)