def connect(self, address, _hostname_hint=None):
"""connect(address)
Connect the socket to a remote address. For IP sockets, the address
is a pair (host, port).
"""
if not self._created:
if self.gettimeout() is None:
self._CreateSocket(address=address,
address_hostname_hint=_hostname_hint)
return
else:
self._CreateSocket()
if not self._socket_descriptor:
raise error(errno.EBADF, os.strerror(errno.EBADF))
if self._connected:
raise error(errno.EISCONN, os.strerror(errno.EISCONN))
request = remote_socket_service_pb.ConnectRequest()
request.set_socket_descriptor(self._socket_descriptor)
self._SetProtoFromAddr(request.mutable_remote_ip(), address, _hostname_hint)
if self.gettimeout() is not None:
request.set_timeout_seconds(self.gettimeout())
reply = remote_socket_service_pb.ConnectReply()
try:
apiproxy_stub_map.MakeSyncCall('remote_socket', 'Connect', request, reply)
except apiproxy_errors.ApplicationError, e:
translated_e = _SystemExceptionFromAppError(e)
if translated_e.errno == errno.EISCONN:
self._bound = True
self._connected = True
elif translated_e.errno == errno.EINPROGRESS:
self._connect_in_progress = True
raise translated_e
self._bound = True
self._connected = True
python类EISCONN的实例源码
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
def doConnect(self):
"""
Initiate the outgoing connection attempt.
@note: Applications do not need to call this method; it will be invoked
internally as part of L{IReactorTCP.connectTCP}.
"""
self.doWrite = self.doConnect
self.doRead = self.doConnect
if not hasattr(self, "connector"):
# this happens when connection failed but doConnect
# was scheduled via a callLater in self._finishInit
return
err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
if err:
self.failIfNotConnected(error.getConnectError((err, strerror(err))))
return
# doConnect gets called twice. The first time we actually need to
# start the connection attempt. The second time we don't really
# want to (SO_ERROR above will have taken care of any errors, and if
# it reported none, the mere fact that doConnect was called again is
# sufficient to indicate that the connection has succeeded), but it
# is not /particularly/ detrimental to do so. This should get
# cleaned up some day, though.
try:
connectResult = self.socket.connect_ex(self.realAddress)
except socket.error as se:
connectResult = se.args[0]
if connectResult:
if connectResult == EISCONN:
pass
# on Windows EINVAL means sometimes that we should keep trying:
# http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/connect_2.asp
elif ((connectResult in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or
(connectResult == EINVAL and platformType == "win32")):
self.startReading()
self.startWriting()
return
else:
self.failIfNotConnected(error.getConnectError((connectResult, strerror(connectResult))))
return
# If I have reached this point without raising or returning, that means
# that the socket is connected.
del self.doWrite
del self.doRead
# we first stop and then start, to reset any references to the old doRead
self.stopReading()
self.stopWriting()
self._connectDone()