示例#1
0
    def handle_request(self, request):
        def handler(ws_sock):
            ssh_instance = None
            sock_id = self.server.hub.add_wsock(ws_sock)
            try:
                ssh_instance = self.handle_connect(request, sock_id, ws_sock)
                self.container.spawn_manage_thread(ssh_instance.forward,
                                                   args=(ws_sock, ))
                while self.accpted:
                    data = ws_sock.wait()
                    resp = json.loads(data)
                    code = resp['code']
                    data = resp['data']
                    if str(code) == '0':
                        data = data.replace('\r', '\n')
                        if not data:
                            break
                        ssh_instance.channel.send(data)
                        self.handle_message(request, sock_id, data)
                    if str(code) == '1':
                        ssh_instance.set_pty(**data)
            except Exception as e:
                msg = 'ws_sock:{} send msg error {}'.format(sock_id, e.message)
                logger.error(msg)
            finally:
                exc_info = sys.exc_info()
                ssh_instance and ssh_instance.close()
                self.handle_sockclose(request, sock_id, exc_info)

        response = WebSocketWSGI(handler)
        return response
示例#2
0
 def handle_request(self, request):
     def handler(ws_sock):
         sock_id = self.server.hub.add_wsock(ws_sock)
         try:
             self.handle_connect(request, sock_id, ws_sock)
             while self.accpted:
                 data = ws_sock.wait()
                 if data is None:
                     break
                 ws_mesg = self.handle_message(request, sock_id, data)
                 ws_sock.send(ws_mesg.as_json())
         except Exception as e:
             msg = 'ws_sock:{} send msg error {}'.format(sock_id, e.message)
             logger.error(msg)
         finally:
             exc_info = sys.exc_info()
             self.handle_sockclose(request, sock_id, exc_info)
     response = WebSocketWSGI(handler)
     return response
示例#3
0
    def websocket_mainloop(self, initial_context_data):
        def handler(ws):
            socket_id, context_data = self.add_websocket(
                ws, initial_context_data)
            try:
                ws.send(
                    self.serialize_event('connected',
                                         {'socket_id': socket_id}))

                while 1:
                    raw_req = ws.wait()
                    if raw_req is None:
                        break
                    ws.send(
                        self.handle_websocket_request(socket_id, context_data,
                                                      raw_req))
            finally:
                self.remove_socket(socket_id)

        return WebSocketWSGI(handler)
示例#4
0
        while True:
            m = ws.wait()
            if m is None:
                break
            ws.send(m)
    elif ws.path == '/range':
        for i in range(10):
            ws.send("msg %d" % i)
            eventlet.sleep(0.01)
    elif ws.path == '/error':
        # some random socket error that we shouldn't normally get
        raise socket.error(errno.ENOTSOCK)
    else:
        ws.close()

wsapp = WebSocketWSGI(handle)


class TestWebSocket(tests.wsgi_test._TestBase):
    TEST_TIMEOUT = 5

    def set_site(self):
        self.site = wsapp

    def test_incorrect_headers(self):
        http = httplib.HTTPConnection(*self.server_addr)
        http.request("GET", "/echo")
        response = http.getresponse()
        assert response.status == 400

    def test_incomplete_headers_75(self):