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, 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 addr[0].replace(".", "").isdigit():
warnings.warn("Please only pass IPs to write(), not hostnames", DeprecationWarning, stacklevel=2)
try:
return self.socket.sendto(datagram, addr)
except socket.error, 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
评论列表
文章目录