Пример #1
0
 def recv_into(self, buffer, nbytes=None, flags=0):
     if buffer and (nbytes is None):
         nbytes = len(buffer)
     elif nbytes is None:
         nbytes = 1024
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to recv_into() on %s"
                 % self.__class__)
         while True:
             try:
                 tmp_buffer = self.read(nbytes)
                 v = len(tmp_buffer)
                 buffer[:v] = tmp_buffer
                 return v
             except SSLError, x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     sys.exc_clear()
                     if self.timeout == 0.0:
                         raise timeout(str(x))
                     wait_read(self.fileno(), timeout=self.timeout)
                     continue
                 else:
                     raise
Пример #2
0
        def connect_stdin():
            fcntl.fcntl(sys.stdin, fcntl.F_SETFL, os.O_NONBLOCK)
            while True:
                wait_read(sys.stdin.fileno())
                buf = sys.stdin.read(4096)
                if len(buf) == 0:
                    break
                binary = self.should_send_binary_frame(buf)
                #                print("common opts: ",self.opts)
                #                if self.opts.verbosity >= 3:
                #                    mode_msg = 'binary' if binary else 'text'
                #                    print >> sys.stderr, "[sending payload of length %d as %s]" % (len(buf), mode_msg)
                websocket.send(buf, binary)

#            if self is not None:
#                sys.stderr.write("self: "+str(self.keys()))

#            if self.opts.verbosity >= 2:
#                sys.stderr.write('[EOF on stdin, shutting down input]')

# If -q was passed, shutdown the program after EOF and the
# specified delay.  Otherwise, keep the socket open even with no
# more input flowing (consistent with netcat's behaviour).
            if self.opts.quit_on_eof is not None:
                if self.opts.quit_on_eof > 0:
                    gevent.sleep(self.opts.quit_on_eof)
                self.shutdown_cond.set()
Пример #3
0
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 v = self._sslobj.write(data)
             except SSLError, x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     if self.timeout == 0.0:
                         raise timeout(str(x))
                     sys.exc_clear()
                     wait_read(self.fileno(), timeout=timeout)
                 elif x.args[0] == SSL_ERROR_WANT_WRITE:
                     if self.timeout == 0.0:
                         raise timeout(str(x))
                     sys.exc_clear()
                     wait_write(self.fileno(), timeout=timeout)
                 else:
                     raise
             else:
                 return v
Пример #4
0
def handle(socket, address):
    print('new connection!')
    fp = socket.makefile()
    try:
        _prev_message = ""
        while True:
            wait_read(socket.fileno(), timeout=5, timeout_exc=TooLong)
            line = fp.readline()

            if line:
                print("[wait]", time.time())

                # 前回受信分を含めた応答文字列を生成
                s = _prev_message + line
                print("==========")
                print(len(s))
                # 送信
                # socket.send(s.encode('utf-8', 'ignore'))
                # fp.flush()
                _prev_message = line
    except TooLong:
        print('timeout')

    # Timeoutが発生したら切断する
    socket.shutdown(py_socket.SHUT_RDWR)
    socket.close()
Пример #5
0
 def reader_fn(self):
     while True:
         wait_read(self.fd)
         self.run_single_read()
         self._check()
         if self.dead:
             return
Пример #6
0
def consumer():
    for index in range(0, 10):
        print("Consumer gevent.socket.wait_read")
        wait_read(s1.recv_fd)
        print("Consumer gevent.socket.wait_read finished")
        req = s1.recv(flags=DONTWAIT)
        s1.send("{} pong {}".format(req, index))
Пример #7
0
 def _forward_outbound(self, channel):
     """ Forward outbound traffic (ssh -> websockets) """
     try:
         data = ''
         while True:
             wait_read(channel.fileno())
             recv = channel.recv(self._windowsize)
             if len(recv) <= 0:
                 return
             try:
                 response = recv.decode('utf-8')
                 self._decoder = 'utf-8'
             except UnicodeDecodeError:
                 response = recv.decode('gbk', 'ignore')
                 self._decoder = 'gbk'
             data += self._commandbuffer.handle_response(response)
             try:
                 self._websocket.send(
                     json.dumps({
                         'data': data,
                         'decoder': self._decoder
                     },
                                ensure_ascii=False))
                 data = ''
             except UnicodeEncodeError:
                 pass
     finally:
         self.close()
Пример #8
0
    def run(self):
        sync_interval = 10
        sync_last = time.time()
        while self.sock:
            try:
                wait_read(self.sock.fileno(), timeout=sync_interval)
            except socket.timeout:
                # After initial synching, send periodic sync requests
                now = time.time()
                if sync_last < (now - sync_interval):
                    self._send("sendsync")
                    sync_last = now

            cmd_name = self._recv()
            if not cmd_name:
                break
            cmd_func = getattr(self, '_cmd_' + cmd_name, None)

            if not cmd_func:
                LOG.warning('Peer %r - Unknown CMD: %r' %
                            (self.sockaddr, cmd_name))
                self.close()
                return False

            LOG.debug("Peer %r - Received command: %r", self.sockaddr,
                      cmd_name)
            cmd_func()
        return True
Пример #9
0
Файл: ssl.py Проект: 0rt/XX-Net
 def _sslobj_shutdown(self):
     while True:
         try:
             return self._sslobj.shutdown()
         except SSLError, ex:
             if ex.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
                 return ''
             elif ex.args[0] == SSL_ERROR_WANT_READ:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_read(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorReadTimeout, event=self._read_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
             elif ex.args[0] == SSL_ERROR_WANT_WRITE:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_write(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorWriteTimeout, event=self._write_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
Пример #10
0
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 v = self._sslobj.write(data)
             except SSLError, x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     if self.timeout == 0.0:
                         return 0
                     sys.exc_clear()
                     try:
                         wait_read(self.fileno(), timeout=timeout, event=self._read_event)
                     except socket_error, ex:
                         if ex[0] == EBADF:
                             return 0
                         raise
                 elif x.args[0] == SSL_ERROR_WANT_WRITE:
                     if self.timeout == 0.0:
                         return 0
                     sys.exc_clear()
                     try:
                         wait_write(self.fileno(), timeout=timeout, event=self._write_event)
                     except socket_error, ex:
                         if ex[0] == EBADF:
                             return 0
                         raise
Пример #11
0
 def write(self, data):
     """Write DATA to the underlying SSL channel.  Returns
     number of bytes of DATA actually transmitted."""
     while True:
         try:
             return self._sslobj.write(data)
         except SSLError, ex:
             if ex.args[0] == SSL_ERROR_WANT_READ:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_read(self.fileno(),
                               timeout=self.timeout,
                               timeout_exc=_SSLErrorWriteTimeout,
                               event=self._read_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return 0
                     raise
             elif ex.args[0] == SSL_ERROR_WANT_WRITE:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_write(self.fileno(),
                                timeout=self.timeout,
                                timeout_exc=_SSLErrorWriteTimeout,
                                event=self._write_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return 0
                     raise
Пример #12
0
 def _sslobj_shutdown(self):
     while True:
         try:
             return self._sslobj.shutdown()
         except SSLError, ex:
             if ex.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
                 return ''
             elif ex.args[0] == SSL_ERROR_WANT_READ:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_read(self.fileno(),
                               timeout=self.timeout,
                               timeout_exc=_SSLErrorReadTimeout,
                               event=self._read_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
             elif ex.args[0] == SSL_ERROR_WANT_WRITE:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_write(self.fileno(),
                                timeout=self.timeout,
                                timeout_exc=_SSLErrorWriteTimeout,
                                event=self._write_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
Пример #13
0
def file_to_pipe(file, pipe, chunksize=-1):
    """Copy contents of *file* to *pipe*. *chunksize* is passed on to file.read()
    """
    if file.closed:
        pipe.close()
        return

    fcntl.fcntl(file, fcntl.F_SETFL, os.O_NONBLOCK)

    fno = file.fileno()
    use_wait = True

    while True:
        try:
            chunk = file.read(chunksize)
            if not chunk:
                break
            pipe.put(chunk)

        except IOError, ex:
            if ex[0] != errno.EAGAIN:
                raise

            sys.exc_clear()

        try:
            if use_wait:
                socket.wait_read(fno)
        except IOError, ex:
            if ex[0] != errno.EPERM:
                raise

            sys.exc_clear()
            use_wait = False
Пример #14
0
 def recv_into(self, buffer, nbytes=None, flags=0):
     if buffer and (nbytes is None):
         nbytes = len(buffer)
     elif nbytes is None:
         nbytes = 1024
     if self._sslobj:
         if flags != 0:
             raise ValueError(
               "non-zero flags not allowed in calls to recv_into() on %s" %
               self.__class__)
         while True:
             try:
                 tmp_buffer = self.read(nbytes)
                 v = len(tmp_buffer)
                 buffer[:v] = tmp_buffer
                 return v
             except SSLError, x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     sys.exc_clear()
                     if self.timeout == 0.0:
                         raise timeout(str(x))
                     wait_read(self.fileno(), timeout=self.timeout)
                     continue
                 else:
                     raise
Пример #15
0
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     if self._sslobj:
         if flags != 0:
             raise ValueError(
                 "non-zero flags not allowed in calls to send() on %s" %
                 self.__class__)
         while True:
             try:
                 v = self._sslobj.write(data)
             except SSLError, x:
                 if x.args[0] == SSL_ERROR_WANT_READ:
                     if self.timeout == 0.0:
                         raise timeout(str(x))
                     sys.exc_clear()
                     wait_read(self.fileno(), timeout=timeout)
                 elif x.args[0] == SSL_ERROR_WANT_WRITE:
                     if self.timeout == 0.0:
                         raise timeout(str(x))
                     sys.exc_clear()
                     wait_write(self.fileno(), timeout=timeout)
                 else:
                     raise
             else:
                 return v
Пример #16
0
 def _handler(self, socket, address):
     """Message wait loop for a client"""
     self.stats.update_stats_increment("client", "total_num", 1)
     user_info = None
     msg_parser = message_key_types.Message()
     try:
         while True:
             wait_read(socket.fileno())
             buf = socket.recv(8192)
             if len(buf) == 0:
                 break
             msg_parser.recv(buf)
             while True:
                 msg = msg_parser.parse()
                 if msg is None:
                     break
                 disconnection, new_info = self._process(socket, msg, msg_parser.payload_type)
                 if disconnection:
                     break
                 if new_info is not None:
                     user_info = new_info
     except Exception as e:
         self.logger.info("TCP disconnect: %s" % e)
         traceback.print_exc()
     self.logger.debug("closing socket")
     if user_info is not None:
         self.networking.domains[user_info[0]]['user'].unregister_user(user_info[1], socket)
     try:
         socket.shutdown(py_socket.SHUT_RDWR)
         socket.close()
     except:
         pass
     self.logger.debug("connection closed")
     self.stats.update_stats_decrement("client", "total_num", 1)
Пример #17
0
 def _recv(self):
     buf = ''
     while True:
         if not self.gw_fd or not self._gw_fd_raw:
             gevent.sleep(random.random())
             continue
         try:
             #print('wait for read')
             socket.wait_read(self.gw_fd.fileno())
         except socket.error:
             break
         #print('read')
         buf += self.gw_fd.read()
         if len(buf) == 0:
             continue
         try:
             msg = Packet.UnPack(buf) # Must catch error
         except DataNeededError:
             continue
         else:
             buf = ''
         try:
             self._resp_handler(msg)
         except KeyboardInterrupt:
             break
Пример #18
0
 def _recv(self):
     buf = ''
     while True:
         if not self.gw_fd or not self._gw_fd_raw:
             gevent.sleep(random.random())
             continue
         try:
             #print('wait for read')
             socket.wait_read(self.gw_fd.fileno())
         except socket.error:
             break
         #print('read')
         buf += self.gw_fd.read()
         if len(buf) == 0:
             continue
         try:
             msg = Packet.UnPack(buf)  # Must catch error
         except DataNeededError:
             continue
         else:
             buf = ''
         try:
             self._resp_handler(msg)
         except KeyboardInterrupt:
             break
Пример #19
0
def stdin_handler():
    fcntl.fcntl(stdin, fcntl.F_SETFL, os.O_NONBLOCK)
    while True:
        wait_read(stdin.fileno())
        cmd = stdin.readline().strip().lower()
        if cmd == 'quit':
            exit(0)
Пример #20
0
Файл: ssl.py Проект: 0rt/XX-Net
 def write(self, data):
     """Write DATA to the underlying SSL channel.  Returns
     number of bytes of DATA actually transmitted."""
     while True:
         try:
             return self._sslobj.write(data)
         except SSLError, ex:
             if ex.args[0] == SSL_ERROR_WANT_READ:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_read(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorWriteTimeout, event=self._read_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return 0
                     raise
             elif ex.args[0] == SSL_ERROR_WANT_WRITE:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_write(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorWriteTimeout, event=self._write_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return 0
                     raise
Пример #21
0
def file_to_pipe(file, pipe, chunksize=-1):
    """Copy contents of *file* to *pipe*. *chunksize* is passed on to file.read()
    """
    if file.closed:
        pipe.close()
        return
    
    fcntl.fcntl(file, fcntl.F_SETFL, os.O_NONBLOCK)

    fno = file.fileno()
    use_wait = True
    
    while True:
        try:
            chunk = file.read(chunksize)
            if not chunk:
                break
            pipe.put(chunk)

        except IOError, ex:
            if ex[0] != errno.EAGAIN:
                raise
        
            sys.exc_clear()
       
        try: 
            if use_wait:
                socket.wait_read(fno)
        except IOError, ex:
            if ex[0] != errno.EPERM:
                raise

            sys.exc_clear()
            use_wait = False
Пример #22
0
 def reader_fn(self):
     while True:
         wait_read(self.fd)
         self.run_single_read()
         self._check()
         if self.dead:
             return
Пример #23
0
 def read(self, len=1024):
     """Read up to LEN bytes and return them.
     Return zero-length string on EOF."""
     while True:
         try:
             return self._sslobj.read(len)
         except SSLError, ex:
             if ex.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
                 return ''
             elif ex.args[0] == SSL_ERROR_WANT_READ:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     wait_read(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorReadTimeout, event=self._read_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
             elif ex.args[0] == SSL_ERROR_WANT_WRITE:
                 if self.timeout == 0.0:
                     raise
                 sys.exc_clear()
                 try:
                     # note: using _SSLErrorReadTimeout rather than _SSLErrorWriteTimeout below is intentional
                     wait_write(self.fileno(), timeout=self.timeout, timeout_exc=_SSLErrorReadTimeout, event=self._write_event)
                 except socket_error, ex:
                     if ex[0] == EBADF:
                         return ''
                     raise
Пример #24
0
    def test(self):  # pylint:disable=too-many-locals
        # If this test is broken, there are a few failure modes.
        # - In the original examples, the parent process just hangs, because the
        #   child has raced ahead, spawned the greenlet and read the data. When the
        #   greenlet goes to read in the parent, it blocks, and the hub and loop
        #   wait for it.
        # - Here, our child detects the greenlet ran when it shouldn't and
        #   raises an error, which translates to a non-zero exit status,
        #   which the parent checks for and fails by raising an exception before
        #   returning control to the hub. We can replicate the hang by removing the
        #   assertion in the child.
        from time import sleep as hang

        from gevent import get_hub
        from gevent import spawn
        from gevent.socket import wait_read
        from gevent.os import nb_read
        from gevent.os import nb_write
        from gevent.os import make_nonblocking
        from gevent.os import fork
        from gevent.os import waitpid

        pipe_read_fd, pipe_write_fd = os.pipe()
        make_nonblocking(pipe_read_fd)
        make_nonblocking(pipe_write_fd)

        run = []

        def reader():
            run.append(1)
            return nb_read(pipe_read_fd, 4096)

        # Put data in the pipe
        DATA = b'test'
        nb_write(pipe_write_fd, DATA)
        # Make sure we're ready to read it
        wait_read(pipe_read_fd)

        # Schedule a greenlet to start
        reader = spawn(reader)

        hub = get_hub()
        pid = fork()
        if pid == 0:
            # Child destroys the hub. The reader should not have run.
            hub.destroy(destroy_loop=True)
            self.assertFalse(run)
            os._exit(0)
            return

        # The parent.
        # Briefly prevent us from spinning our event loop.
        hang(0.5)
        wait_child_result = waitpid(pid, 0)
        self.assertEqual(wait_child_result, (pid, 0))
        # We should get the data; the greenlet only runs in the parent.
        data = reader.get()
        self.assertEqual(run, [1])
        self.assertEqual(data, DATA)
Пример #25
0
def input():
    while True:
        wait_read(sys.stdin.fileno())
        input_utf8 = sys.stdin.readline()
        input_unicode = input_utf8.decode('utf-8')
        input_unicode = u'{}\n'.format(input_unicode)
        input_cp1251 = input_unicode.encode('cp1251')
        session.send(input_cp1251)
Пример #26
0
 def _input(self):
     buff = ""
     while True:
         socket.wait_read(sys.stdin.fileno())
         buff += sys.stdin.read()
         while "\n" in buff:
             line, buff = buff.split("\n", 1)
             self.input.put(line)
def wait_for_user_input(state):
  while True:
    gs.wait_read(sys.stdin.fileno())
    ln = sys.stdin.readline()
    if ln.find('P') != -1:
      state.set_state(State.PROCESSING)
    elif ln.find('T') != -1:
      state.set_state(State.TRAINING)
Пример #28
0
    def wait_for_sip_read(self):
        """
        This function waits for events and dispatches event handlers.
        This function should run inside a greenlet.
        """
        while True:
            fileno = self.socket.fileno()
            gsocket.wait_read(fileno)
            message = self.socket.recv(8192)
            header, body = message.split("\r\n\r\n", 1)

            sip_status_line, parse_headers = header.split("\r\n", 1)
            sip_status = sip_status_line.split(" ")

            # FIXME Debug info
            print sip_status_line

            if sip_status[0] != "SIP/2.0":
                print "WARNING: Got:", sip_status_line
                print header
                print body
                continue

            headers = Message(StringIO(parse_headers))

            if sip_status[1] == '100' and headers['cseq'] == "20 INVITE":
                if 'trying' in self.handlers:
                    for fn in self.handlers['trying']:
                        fn(headers, body)

            elif sip_status[1] == '180' and headers['cseq'] == "20 INVITE":
                if 'ringing' in self.handlers:
                    for fn in self.handlers['ringing']:
                        fn(headers, body)

            elif sip_status[1] == '200' and headers['cseq'] == "20 INVITE":
                self.contact = headers['contact'][1:-1]
                self.to_hdr = headers['to']
                self.from_hdr = headers['from']

                if 'invite_ok' in self.handlers:
                    for fn in self.handlers['invite_ok']:
                        fn(headers, body)

            elif sip_status[1] == '404' and headers['cseq'] == "20 INVITE":
                if 'not_found' in self.handlers:
                    for fn in self.handlers['not_found']:
                        fn(headers, body)

            elif sip_status[1] == '415' and headers['cseq'] == "20 INVITE":
                if 'media-error' in self.handlers:
                    for fn in self.handlers['media-error']:
                        fn(headers, body)
            else:
                print header, body
                if 'other' in self.handlers:
                    for fn in self.handlers['other']:
                        fn(headers, body)
Пример #29
0
    def wait_for_sip_read(self):
        """
        This function waits for events and dispatches event handlers.
        This function should run inside a greenlet.
        """
        while True:
            fileno = self.socket.fileno()
            gsocket.wait_read(fileno)
            message = self.socket.recv(8192)
            header, body = message.split("\r\n\r\n", 1)

            sip_status_line, parse_headers = header.split("\r\n", 1)
            sip_status = sip_status_line.split(" ")

            # FIXME Debug info
            print sip_status_line

            if sip_status[0] != "SIP/2.0":
                print "WARNING: Got:", sip_status_line
                print header
                print body
                continue

            headers = Message(StringIO(parse_headers))

            if sip_status[1] == '100' and headers['cseq'] == "20 INVITE":
                if 'trying' in self.handlers:
                    for fn in self.handlers['trying']:
                        fn(headers, body)

            elif sip_status[1] == '180' and headers['cseq'] == "20 INVITE":
                if 'ringing' in self.handlers:
                    for fn in self.handlers['ringing']:
                        fn(headers, body)

            elif sip_status[1] == '200' and headers['cseq'] == "20 INVITE":
                self.contact = headers['contact'][1:-1]
                self.to_hdr = headers['to']
                self.from_hdr = headers['from']

                if 'invite_ok' in self.handlers:
                    for fn in self.handlers['invite_ok']:
                        fn(headers, body)

            elif sip_status[1] == '404' and headers['cseq'] == "20 INVITE":
                if 'not_found' in self.handlers:
                    for fn in self.handlers['not_found']:
                        fn(headers, body)

            elif sip_status[1] == '415' and headers['cseq'] == "20 INVITE":
                if 'media-error' in self.handlers:
                    for fn in self.handlers['media-error']:
                        fn(headers, body)
            else:
                print header, body
                if 'other' in self.handlers:
                    for fn in self.handlers['other']:
                        fn(headers, body)
Пример #30
0
 def _forward_outbound(self, channel):
     """ Forward outbound traffic (ssh -> websockets) """
     try:
         while True:
             wait_read(channel.fileno())
             data = channel.recv(1024)
             self._websocket.send(data)
     except Exception as e:
         print(e)
Пример #31
0
def provider():
    for index in range(0, 10):
        print("Provider send ping...")
        req = s2.send("ping")
        print("Provider gevent.socket.wait_read")
        wait_read(s2.recv_fd)
        print("Provider gevent.socket.wait_read finished")
        rep = s2.recv(flags=DONTWAIT)
        print("Provider receive: {}".format(rep))
Пример #32
0
 def next(self):
     while True:
         try:
             return self.stream.next()
         except IOError, ex:
             if ex[0] != errno.EAGAIN:
                 raise StopIteration
             sys.exc_clear()
         socket.wait_read(self.fileno())
Пример #33
0
 def manager(self):
     gevent.spawn(self.status_thread)
     gevent.spawn(self.player_loop)
     print "Starting Manager"
     while True: 
         wait_read(self.pipe_right.fileno())
         print "Msg Received"
         data = self.pipe_right.recv()
         self.process_data(data)  
Пример #34
0
    def start(self):
        self.w = 80
        self.h = 25
        self.message = []

        print 'New connection:', self.info

        self.conn.send(Codes.IAC + Codes.WILL + Codes.ECHO)
        self.conn.send(Codes.IAC + Codes.WILL + Codes.SUPPRESS_GO_AHEAD)
        self.conn.send(Codes.IAC + Codes.DO + Codes.NAWS)

        self.conn.send('\033[2J')

        while True:
            socket.wait_read(self.conn.fileno())
            c = self.conn.recv(1)

            if not c:
                return

            if c == Codes.IAC:
                print 'IAC'
                c2 = self.conn.recv(1)
                c2_name = Codes.reverse(c2)
                print c2_name
                if c2_name in ('DO', 'DONT', 'WILL', 'WONT'):
                    c3 = self.conn.recv(1)
                    c3_name = Codes.reverse(c3)
                    print c3_name
                elif c2_name == 'SB':
                    c3 = self.conn.recv(1)
                    c3_name = Codes.reverse(c3)
                    if c3_name == 'NAWS':
                        self.conn.recv(1)
                        self.w = ord(self.conn.recv(1))
                        self.conn.recv(1)
                        self.h = ord(self.conn.recv(1))
                    else:
                        pass
                        # while c3 != Codes.IAC:
                        #     self.conn.recv(1)
                    self.conn.recv(1)
                    self.conn.recv(1)
                    print 'Screen resized: {} x {}'.format(self.w, self.h)

                    # return self.bloat()
            else:
                print 'Byte:', ord(c)
                if c in ('\x0A', '\x0D'):
                    return self.bloat()
                elif c in ('\x08', '\x7F'):
                    if len(self.message):
                        self.message = self.message[:-1]
                        self.conn.send('\033[D \033[D')
                else:
                    self.message.append(c)
                    self.conn.send(c)
Пример #35
0
 def receive_loop(self):
     while self.state == AsyncTCPClient.CONNECTED_STATE:
         try:
             socket.wait_read(self.sock.fileno(), timeout=1)
         except _socket.error:
             continue
         msg = self.get_line()
         if self.msg_handler is not None and msg is not None:
             self.msg_handler(msg)
Пример #36
0
 def receive_loop(self):
     while self.state == AsyncTCPClient.CONNECTED_STATE:
         try:
             socket.wait_read(self.sock.fileno(), timeout=1)
         except _socket.error:
             continue
         msg = self.get_line()
         if self.msg_handler is not None and msg is not None:
             self.msg_handler(msg)
Пример #37
0
def run_par2(args):
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    fcntl.fcntl(p.stdout, fcntl.F_SETFL, os.O_NONBLOCK)
    while True:
        gsocket.wait_read(p.stdout.fileno())
        d = p.stdout.read(2048)
        if d == "":
            break
    p.stdout.close()
Пример #38
0
def popenNonblock(args, data='', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=None, logFile=None):
    """Communicate with the process non-blockingly.

    If logFile is defined print the process's output line by line to that file
    """
    if logFile:
        f = open(logFile, 'a')
        stdout = f
        stderr = f
    p = subprocess.Popen(args, stdin=stdin, stdout=stdout, stderr=stderr, cwd=cwd)
    real_stdin = p.stdin if stdin == subprocess.PIPE else stdin
    fcntl.fcntl(real_stdin, fcntl.F_SETFL, os.O_NONBLOCK)  # make the file nonblocking
    real_stdout = p.stdout if stdout == subprocess.PIPE else stdout
    fcntl.fcntl(real_stdout, fcntl.F_SETFL, os.O_NONBLOCK)  # make the file nonblocking

    if data:
        bytes_total = len(data)
        bytes_written = 0
        while bytes_written < bytes_total:
            try:
                # p.stdin.write() doesn't return anything, so use os.write.
                bytes_written += os.write(p.stdin.fileno(), data[bytes_written:])
            except IOError:
                ex = sys.exc_info()[1]
                if ex.args[0] != errno.EAGAIN:
                    raise
                sys.exc_clear()
            socket.wait_write(p.stdin.fileno())
        p.stdin.close()

    chunks = []
    if stdout == subprocess.PIPE:
        while True:
            try:
                chunk = p.stdout.read(4096)
                if not chunk:
                    break
                chunks.append(chunk)
            except IOError:
                ex = sys.exc_info()[1]
                if ex[0] != errno.EAGAIN:
                    raise
                sys.exc_clear()
            socket.wait_read(p.stdout.fileno())
        p.stdout.close()

    output = ''.join(chunks)

    while True:
        returncode = p.poll()
        if returncode is not None:
            break
        else:
            gevent.sleep(1)

    return (returncode, output)
Пример #39
0
 def get_ssh_data(self):
     try:
         while True:
             wait_read(self.channel.fileno())
             data = self.channel.recv(1024).decode('utf-8', 'ignore')
             if not len(data):
                 return
             self.emit('shell_data', data)
     finally:
         self.channel.close()
Пример #40
0
 def get_ssh_data(self):
     try:
         while True:
             wait_read(self.channel.fileno())
             data = self.channel.recv(1024).decode('utf-8','ignore')
             if not len(data):
                 return
             self.emit('shell_data', data)
     finally:
         self.channel.close()
Пример #41
0
 def _forward_bound(self, channel, callback, *args):
     try:
         while True:
             wait_read(channel.fileno())
             data = channel.recv(1024)
             if not len(data):
                 return
             callback(data, *args)
     finally:
         self.close()
Пример #42
0
def read(fromfile, timeout):
    while True:
        try:
            wait_read(fromfile.fileno(), timeout)
            return fromfile.read()
        except:
            ex = exc_info()[1]
            if ex.args[0] == EBADF:
                return ''
            raise
Пример #43
0
 def run(self):
     while self.state == AsyncUDPServer.RUNNING_STATE:
         try:
             socket.wait_read(self.sock.fileno(), timeout=1)
         except _socket.error:
             continue
         msg = self.sock.recv(4096)
         if self.msg_handler is not None:
             self.msg_handler(msg)
     print "udp ending"
Пример #44
0
 def __iowait(self, io_func, *args, **kwargs):
     fd = self._sock.fileno()
     timeout = self._sock.gettimeout()
     while True:
         try:
             return io_func(*args, **kwargs)
         except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantX509LookupError):
             wait_read(fd, timeout=timeout)
         except OpenSSL.SSL.WantWriteError:
             wait_write(fd, timeout=timeout)
Пример #45
0
 def run(self):
     while self.state == AsyncUDPServer.RUNNING_STATE:
         try:
             socket.wait_read(self.sock.fileno(), timeout=1)
         except _socket.error:
             continue
         msg = self.sock.recv(4096)
         if self.msg_handler is not None:
             self.msg_handler(msg)
     print "udp ending"
Пример #46
0
def read(fromfile, timeout):
    while True:
        try:
            wait_read(fromfile.fileno(), timeout)
            return fromfile.read()
        except:
            ex = exc_info()[1]
            if ex.args[0] == EBADF:
                return ''
            raise
Пример #47
0
    def _resolve_loop(self):
        while True:
            iface, service, regtype, replydomain = self._to_resolve.get()

            ref = pybonjour.DNSServiceResolve(
                0, iface, service, regtype, replydomain, self._resolve_callback)
            try:
                wait_read(ref.fileno())
                pybonjour.DNSServiceProcessResult(ref)
            finally:
                ref.close()
Пример #48
0
 def do_handshake(self):
     while True:
         try:
             _Connection.do_handshake(self)
             break
         except WantReadError:
             exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
Пример #49
0
 def connect(self, *args, **kwargs):
     while True:
         try:
             _Connection.connect(self, *args, **kwargs)
             break
         except WantReadError:
             exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
Пример #50
0
 def connect(self, *args, **kwargs):
     while True:
         try:
             self._connection.connect(*args, **kwargs)
             break
         except (WantReadError, WantX509LookupError):
             sys.exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             sys.exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
 def forward_outbound(self, channel):
     """ Forward outbound traffic (ssh -> websockets) """
     try:
         while True:
             wait_read(channel.fileno())
             data = channel.recv(1024)
             if not len(data):
                 return
             self.websocket.send(json.dumps({'data': data}))
     finally:
         self.close()
Пример #52
0
 def connect(self, *args, **kwargs):
     while True:
         try:
             _Connection.connect(self, *args, **kwargs)
             break
         except WantReadError:
             exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
Пример #53
0
 def do_handshake(self):
     while True:
         try:
             _Connection.do_handshake(self)
             break
         except WantReadError:
             exc_clear()
             wait_read(self._sock.fileno(), timeout=self._timeout)
         except WantWriteError:
             exc_clear()
             wait_write(self._sock.fileno(), timeout=self._timeout)
Пример #54
0
    def reader_fn(self):
        """
        Infinite reader of the file descriptor associated with the shell.
        """

        while True:
            wait_read(self.fd)
            self.run_single_read()
            self._check()
            if self.dead:
                return
Пример #55
0
 def __iowait(self, io_func, *args, **kwargs):
     fd = self._sock.fileno()
     timeout = self._sock.gettimeout()
     while True:
         try:
             return io_func(*args, **kwargs)
         except (OpenSSL.SSL.WantReadError,
                 OpenSSL.SSL.WantX509LookupError):
             wait_read(fd, timeout=timeout)
         except OpenSSL.SSL.WantWriteError:
             wait_write(fd, timeout=timeout)
Пример #56
0
def wait_callback(conn, timeout=None):
    while True:
        state = conn.poll()
        if state == POLL_OK:
            break
        elif state == POLL_READ:
            wait_read(conn.fileno(), timeout=timeout)
        elif state == POLL_WRITE:
            wait_write(conn.fileno(), timeout=timeout)
        else:
            raise psycopg2.OperationalError("Bad result from poll: %r" % state)
def wait_callback(conn, timeout=None):
    while 1:
        state = conn.poll()
        if state == extensions.POLL_OK:
            return
        elif state == extensions.POLL_READ:
            socket.wait_read(conn.fileno(), timeout=timeout)
        elif state == extensions.POLL_WRITE:
            socket.wait_write(conn.fileno(), timeout=timeout)
        else:
            raise psycopg2.OperationalError("Bad result from poll: %r" % state)
Пример #58
0
 def _forward_outbound(self, channel):
     """ Forward outbound traffic (ssh -> websockets) """
     try:
         while True:
             wait_read(channel.fileno())
             data = channel.recv(1024)
             if not len(data):
                 return
             self._websocket.send(json.dumps({'data': data}))
     finally:
         self.close()