def wait_on_connect():
    """
    socketio连接成功后执行该函数
    """

    # 要先开启服务器才能接入socket
    if service.status and check_login():
        with thread_lock:
            if service.first_connect:
                # 判断是否已经开启过后台监听了, 不然多连会出事
                thread.threads.append(
                    socketio.start_background_task(target=push_cpu_usage))
                thread.threads.append(
                    socketio.start_background_task(target=wait_connect))
                service.first_connect = False
Esempio n. 2
0
def start_progress_thread(func, **kwargs):
    """
    Starts a background task using socketio.

    Parameters
    ----------
    func : function
        Function to run in background
    kwargs : kwargs
        Kwargs to pass to function
    """
    global thread
    print("Starting Thread")
    thread = socketio.start_background_task(background_task, func=func, **kwargs)
Esempio n. 3
0
def connect() -> Resnponse:
    """
    开启服务器, 只接收POST请求
    """

    # 判断服务器是否已经在运行
    if client.status:
        return Resnponse.error("已有连接, 请勿重连",
                               code=CodeRequestOkButNoChange,
                               status=StatusBadRequest)
    conn, msg = client.connect()
    if conn:
        print('....................................................')
        traffic.run()
        thread.threads.append(socketio.start_background_task(target=wait_recv))
        return Resnponse.success(msg)
    return Resnponse.error(msg)
Esempio n. 4
0
def start_progress_thread(func, **kwargs):
    global thread
    print("Starting Thread")
    thread = socketio.start_background_task(background_task,
                                            func=func,
                                            **kwargs)
Esempio n. 5
0
def wait_recv():
    """
    在后台接收客户端消息
    :param client: 连接对象
    :param key: 连接对象的键值 IP:PORT
    :return:
    """
    global push_usage_status
    while client.status:
        try:
            data = client.sock.recv(4096)
        except ConnectionResetError:
            # 服务器主动与客户端断开连接
            _disconnection()
            break
        except ConnectionAbortedError:
            # 客户端主动与服务器断开连接
            _disconnection()
            break
        except:
            _disconnection()
            break

        if data == b'':
            # 接收到空数据
            _disconnection()
            break

        # 讲信息编码
        data = data.decode('utf8', 'ignore')
        if data[:9] == '[::usage]':
            client.send_usaged = False
            client.send_usaged = True
            if not push_usage_status:
                push_usage_status = True
                thread.usage.append(
                    socketio.start_background_task(target=push_usage))
        elif data[:10] == '[::nusage]':
            client.send_usaged = False
            push_usage_status = False
            thread.stop_usage()
        elif data[:19] == "[::file_distribute]":
            # 拿到文件大小
            size, filename = data[19:].split('&&')
            size = int(size)

            # 循环接受内容, 知道接收到一模一样长度的内容, 跳出循环保存文件
            while True:
                # 接受数据内容
                filedata = client.sock.recv(size)

                if len(filedata) == size: break
                # 如果不是一模一样长度的数据, 就是别的命令, 作为别的命令去解析

                if data[:9] == '[::usage]':
                    client.send_usaged = False
                    client.send_usaged = True
                    if not push_usage_status:
                        push_usage_status = True
                        thread.usage.append(
                            socketio.start_background_task(target=push_usage))
                elif data[:10] == '[::nusage]':
                    client.send_usaged = False
                    push_usage_status = False
                    thread.stop_usage()
                else:
                    # 先推送信息给前端
                    socketio.emit('server_response', {
                        'action': 'on_recv',
                        'data': {
                            'key': 'Service',
                            'msg': filedata,
                            "t": get_timer()
                        }
                    },
                                  namespace='/channel')

                    # 开线程执行命令
                    thread.threads.append(
                        threading.Thread(target=exec_command,
                                         args=(filedata, )).start())

                    log.write('{}\r'.format(filedata), 'Service')

            threading.Thread(target=save_file,
                             args=(filename, filedata)).start()
        else:
            # 先推送信息给前端
            socketio.emit('server_response', {
                'action': 'on_recv',
                'data': {
                    'key': 'Service',
                    'msg': data,
                    "t": get_timer()
                }
            },
                          namespace='/channel')

            # 开线程执行命令
            thread.threads.append(
                threading.Thread(target=exec_command, args=(data, )).start())

            log.write('{}\r'.format(data), 'Service')