def sendto(self, data, *args):
"""sendto(data[, flags], address) -> count
Like send(data, flags) but allows specifying the destination address.
For IP sockets, the address is a pair (hostaddr, port).
"""
if len(args) == 1:
flags, address = 0, args[0]
elif len(args) == 2:
flags, address = args
if not self._created:
self._CreateSocket()
if not self._socket_descriptor:
raise error(errno.EBADF, os.strerror(errno.EBADF))
if self._shutdown_write:
raise error(errno.EPIPE, os.strerror(errno.EPIPE))
request = remote_socket_service_pb.SendRequest()
request.set_socket_descriptor(self._socket_descriptor)
if len(data) > 512*1024:
request.set_data(data[:512*1024])
else:
request.set_data(data)
request.set_flags(flags)
request.set_stream_offset(self._stream_offset)
if address:
if self._connected:
raise error(errno.EISCONN, os.strerror(errno.EISCONN))
if self.type != SOCK_DGRAM:
raise error(errno.ENOTCONN, os.strerror(errno.ENOTCONN))
self._SetProtoFromAddr(request.mutable_send_to(), address)
else:
if not (self._connected or self._connect_in_progress):
raise error(errno.ENOTCONN, os.strerror(errno.ENOTCONN))
if self.gettimeout() is not None:
request.set_timeout_seconds(self.gettimeout())
reply = remote_socket_service_pb.SendReply()
try:
apiproxy_stub_map.MakeSyncCall('remote_socket', 'Send', request, reply)
except apiproxy_errors.ApplicationError, e:
raise _SystemExceptionFromAppError(e)
if self._connect_in_progress:
self._connect_in_progress = False
self._connected = True
nbytes = reply.data_sent()
assert nbytes >= 0
if self.type == SOCK_STREAM:
self._stream_offset += nbytes
return nbytes
评论列表
文章目录