本文整理汇总了Python中twisted.internet.abstract.isIPAddress函数的典型用法代码示例。如果您正苦于以下问题:Python isIPAddress函数的具体用法?Python isIPAddress怎么用?Python isIPAddress使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isIPAddress函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_decimalDotted
def test_decimalDotted(self):
"""
L{isIPAddress} should return C{True} for any decimal dotted
representation of an IPv4 address.
"""
self.assertTrue(isIPAddress('0.1.2.3'))
self.assertTrue(isIPAddress('252.253.254.255'))
示例2: test_invalidLetters
def test_invalidLetters(self):
"""
L{isIPAddress} should return C{False} for any non-decimal dotted
representation including letters.
"""
self.assertFalse(isIPAddress('a.2.3.4'))
self.assertFalse(isIPAddress('1.b.3.4'))
示例3: test_nonASCII
def test_nonASCII(self):
"""
All IP addresses must be encodable as ASCII; non-ASCII should result in
a L{False} result.
"""
self.assertFalse(isIPAddress(b'\xff.notascii'))
self.assertFalse(isIPAddress(u'\u4321.notascii'))
示例4: test_unicodeAndBytes
def test_unicodeAndBytes(self):
"""
L{isIPAddress} evaluates ASCII-encoded bytes as well as text.
"""
self.assertFalse(isIPAddress(b'256.0.0.0'))
self.assertFalse(isIPAddress(u'256.0.0.0'))
self.assertTrue(isIPAddress(b'252.253.254.255'))
self.assertTrue(isIPAddress(u'252.253.254.255'))
示例5: test_invalidNegative
def test_invalidNegative(self):
"""
L{isIPAddress} should return C{False} for negative decimal values.
"""
self.assertFalse(isIPAddress('-1'))
self.assertFalse(isIPAddress('1.-2'))
self.assertFalse(isIPAddress('1.2.-3'))
self.assertFalse(isIPAddress('1.2.-3.4'))
示例6: write
def write(self, datagram, addr=None):
"""
Write a datagram.
@type datagram: L{bytes}
@param datagram: The datagram to be sent.
@type addr: L{tuple} containing L{str} as first element and L{int} as
second element, or L{None}
@param addr: A tuple of (I{stringified IPv4 or IPv6 address},
I{integer port number}); can be L{None} in connected mode.
"""
if self._connectedAddr:
assert addr in (None, self._connectedAddr)
try:
return self.socket.send(datagram)
except socket.error as se:
no = se.args[0]
if no == EINTR:
return self.write(datagram)
elif no == EMSGSIZE:
raise error.MessageLengthError("message too long")
elif no == ECONNREFUSED:
self.protocol.connectionRefused()
else:
raise
else:
assert addr != None
if (not abstract.isIPAddress(addr[0])
and not abstract.isIPv6Address(addr[0])
and addr[0] != "<broadcast>"):
raise error.InvalidAddressError(
addr[0],
"write() only accepts IP addresses, not hostnames")
if ((abstract.isIPAddress(addr[0]) or addr[0] == "<broadcast>")
and self.addressFamily == socket.AF_INET6):
raise error.InvalidAddressError(
addr[0],
"IPv6 port write() called with IPv4 or broadcast address")
if (abstract.isIPv6Address(addr[0])
and self.addressFamily == socket.AF_INET):
raise error.InvalidAddressError(
addr[0], "IPv4 port write() called with IPv6 address")
try:
return self.socket.sendto(datagram, addr)
except socket.error as se:
no = se.args[0]
if no == EINTR:
return self.write(datagram, addr)
elif no == EMSGSIZE:
raise error.MessageLengthError("message too long")
elif no == ECONNREFUSED:
# in non-connected UDP ECONNREFUSED is platform dependent, I
# think and the info is not necessarily useful. Nevertheless
# maybe we should call connectionRefused? XXX
return
else:
raise
示例7: test_shortDecimalDotted
def test_shortDecimalDotted(self):
"""
L{isIPAddress} should return C{False} for a dotted decimal
representation with fewer or more than four octets.
"""
self.assertFalse(isIPAddress('0'))
self.assertFalse(isIPAddress('0.1'))
self.assertFalse(isIPAddress('0.1.2'))
self.assertFalse(isIPAddress('0.1.2.3.4'))
示例8: test_invalidPunctuation
def test_invalidPunctuation(self):
"""
L{isIPAddress} should return C{False} for a string containing
strange punctuation.
"""
self.assertFalse(isIPAddress(','))
self.assertFalse(isIPAddress('1,2'))
self.assertFalse(isIPAddress('1,2,3'))
self.assertFalse(isIPAddress('1.,.3,4'))
示例9: test_invalidPositive
def test_invalidPositive(self):
"""
L{isIPAddress} should return C{False} for a string containing
positive decimal values greater than 255.
"""
self.assertFalse(isIPAddress('256.0.0.0'))
self.assertFalse(isIPAddress('0.256.0.0'))
self.assertFalse(isIPAddress('0.0.256.0'))
self.assertFalse(isIPAddress('0.0.0.256'))
self.assertFalse(isIPAddress('256.256.256.256'))
示例10: write
def write(self, datagram, addr=None):
"""
Write a datagram.
@param addr: should be a tuple (ip, port), can be None in connected
mode.
"""
if self._connectedAddr:
assert addr in (None, self._connectedAddr)
try:
return self.socket.send(datagram)
except socket.error as se:
no = se.args[0]
if no == errno.WSAEINTR:
return self.write(datagram)
elif no == errno.WSAEMSGSIZE:
raise error.MessageLengthError("message too long")
elif no in (errno.WSAECONNREFUSED, errno.WSAECONNRESET,
ERROR_CONNECTION_REFUSED, ERROR_PORT_UNREACHABLE):
self.protocol.connectionRefused()
else:
raise
else:
assert addr != None
if (not isIPAddress(addr[0]) and not isIPv6Address(addr[0])
and addr[0] != "<broadcast>"):
raise error.InvalidAddressError(
addr[0],
"write() only accepts IP addresses, not hostnames")
if isIPAddress(addr[0]) and self.addressFamily == socket.AF_INET6:
raise error.InvalidAddressError(
addr[0], "IPv6 port write() called with IPv4 address")
if isIPv6Address(addr[0]) and self.addressFamily == socket.AF_INET:
raise error.InvalidAddressError(
addr[0], "IPv4 port write() called with IPv6 address")
try:
return self.socket.sendto(datagram, addr)
except socket.error as se:
no = se.args[0]
if no == errno.WSAEINTR:
return self.write(datagram, addr)
elif no == errno.WSAEMSGSIZE:
raise error.MessageLengthError("message too long")
elif no in (errno.WSAECONNREFUSED, errno.WSAECONNRESET,
ERROR_CONNECTION_REFUSED, ERROR_PORT_UNREACHABLE):
# in non-connected UDP ECONNREFUSED is platform dependent,
# I think and the info is not necessarily useful.
# Nevertheless maybe we should call connectionRefused? XXX
return
else:
raise
示例11: render_GET
def render_GET(self, request):
"""Renders the response to peers"""
args = request.args
peer_id = args.get('peer_id', [None])[0]
if peer_id is None:
return self._encode_failure('Peer ID not given')
if len(peer_id) != 20:
return self._encode_failure('Invalid peer id')
# TODO: eliminate ip option
ip_address = args.get('ip', [request.getClientIP()])[0]
if not isIPAddress(ip_address):
return self._encode_failure('Invalid IP Address')
port = args.get('port', [None])[0]
if port is None:
return self._encode_failure('Port number not given')
try:
port = int(port)
except ValueError:
return self._encode_failure('Invalid port number')
self._peer_manager.update_peer(peer_id, ip_address, port)
return self._encode_peers()
示例12: get_value
def get_value(self):
val = self.entry.get_text()
if val == "":
return val
if not isIPAddress(val):
raise Exception("Must be a valid IPv4 address.")
return val
示例13: __init__
def __init__(self, host, port, connect_callback=None,
disconnect_callback=None):
"""Avoid using this initializer directly; Instead, use the create()
static method, otherwise the messages won't be really delivered.
If you still need to use this directly and want to resolve the host
yourself, remember to call host_resolved() as soon as it's resolved.
@param host: The StatsD server host.
@param port: The StatsD server port.
@param connect_callback: The callback to invoke on connection.
@param disconnect_callback: The callback to invoke on disconnection.
"""
from twisted.internet import reactor
self.reactor = reactor
self.host = host
self.port = port
self.connect_callback = connect_callback
self.disconnect_callback = disconnect_callback
self.data_queue = DataQueue()
self.transport = None
self.transport_gateway = None
if abstract.isIPAddress(host):
self.host_resolved(host)
示例14: will_exit_to
def will_exit_to(self, host, port, protocol="TCP"):
"""Figure out whether this relay will allow any exit traffic to host:port.
If host is None, return True if there is any possible host that exit traffic
to that port would be allowed (and vice versa). Behavior when host AND port
are None is undefined (raises exception)"""
if not self.desc:
return False
#protocol is allowed to be DHT or TCP
if protocol == "DHT":
return self.desc.allowsDHT
if protocol != "TCP":
raise ValueError("Bad value for protocol: %s" % (protocol))
#for the vacuous case that we dont actually care
if not host and not port:
return True
#make sure that this host is an int:
intHost = None
if host and type(host) != types.IntType and isIPAddress(str(host)):
intHost = struct.unpack(">I", socket.inet_aton(host))[0]
port = int(port)
#if we're filtering based on both host and port:
if intHost and port:
#check the descriptor's exit policy:
return self.desc.will_exit_to(intHost, port)
#if we're just filtering based on host:
elif intHost:
return self.will_exit_to_host(intHost)
#if we're just filtering based on port:
elif port:
return self.will_exit_to_port(port)
#should be unreachable...
return False
示例15: _send_remote_peer_request
def _send_remote_peer_request(self, infohash, callback):
#make sure we have a circuit to send it out on:
if self.circ and self.circ.is_done():
self.circ = None
if not self.circ:
self.circ = self.app.find_or_build_best_circuit(force=True, protocol="DHT")
if self.circ == None:
log_msg("Could not build circuit for DHT remote peer request", 0, "dht")
return
#generate the message: (version, infohash, peerList)
msg = ""
#header:
msg += Basic.write_byte(Node.VERSION)
#infohash:
msg += infohash
#peers:
for host, port in self.knownNodes:
#is this an IP address?
if isIPAddress(host):
msg += Basic.write_byte(0)
msg += struct.pack("!4sH", socket.inet_aton(host), port)
#otherwise, it's a URL that has to be resolved remotely
else:
msg += Basic.write_byte(1)
msg += Basic.write_lenstr(host)
msg += Basic.write_short(port)
self.circ.send_dht_request(msg, self.make_callback_wrapper(callback))