def sendall(fd, data): total = len(data) offset = 0 while offset < total: try: n = fd.send(data[offset:]) except socket.error as e: raise NetworkError('Failed to write to socket: %s' % e) if n <= 0: raise NetworkError('Failed to write to socket') else: offset += n
def connect_daemon(self): self._connfd = socket.socket() self._connfd.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) try: self._connfd.connect(('127.0.0.1', self.port)) except: raise NetworkError("Can't connect to daemon")
def recvall(fd, total): remain = total data = '' while remain > 0: try: new = fd.recv(remain) except socket.error as e: raise NetworkError('Failed to read from socket: %s' % e) n = len(new) if n <= 0: raise NetworkError("Failed to read from socket") else: data += new remain -= n return data
def connect_daemon_with_socket(self): self._connfd = socket.socket() self._connfd.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) try: self._connfd.connect(('127.0.0.1', self.port)) except: raise NetworkError("Can't connect to daemon") make_socket_closeonexec(self._connfd.fileno())
def _event_cb(self, bev, what, cb_data): dummy = bev, cb_data logging.warning('libevent error: what = %s' % what) if what & libevent.BEV_EVENT_EOF or \ what & libevent.BEV_EVENT_ERROR or \ what & libevent.BEV_EVENT_READING or \ what & libevent.BEV_EVENT_WRITING: if self._bev is not None: self._bev = None raise NetworkError('libevent error: what = %s' % what)
def connect_daemon_with_pipe(self): self._connfd = socket.socket(socket.AF_UNIX) if not self.un_path: pipe_name = os.path.join(self.config_dir, CCNET_PIPE_NAME) else: pipe_name = self.un_path try: self._connfd.connect(pipe_name) except: raise NetworkError("Can't connect to daemon") make_socket_closeonexec(self._connfd.fileno())