socketio.emit工作但发出不工作

时间:2019-08-30 21:51:01

标签: javascript flask socket.io

我正在尝试开发一个Web应用程序,在其中放置一些条目列表,并将其与socketio一起发送到flask-socketio应用程序,我做得很好,使用socketio.emit(... ),但我想更改它,以便仅发送数据的客户端会收到基于该数据的响应,但我没有将数据发送到javascript。

正如我提到的,我使用socketio.emit(...)可以很好地工作,但是当我将其更改为emit(...)时,它无法正常工作。 也可以使用emit(...,broadcast = True)。

这是代码的一部分,可以从javascript获取信息,并将结果发送到javascript

@socketio.on('create')
def homeip(data):
    IP = data['IPT']
    IP = str(IP)
    IP.encode('ascii','ignore')
    IP = IP.split()
    error = None
    c = 0
    d = 0
    e = 0
    j = 0
    t = 0
    datactual = ''
    IPactual = ''
    data_shodan = ''
    porcentaje = 0.0
    data_BHD = []
    tmp_rbl = ''
    tmp_talos = ''
    while c < len(IP):
        try:
            data = is_malicious.malicious(IP[c])
            IPactual = IP[c]
            socketio.emit('add_result', {'msg': '######################################'})
            try:
                data_rbl = rbl_check.rbl_list(IP[c])
                data_talos = check_talos.talos(IP[c])
                emit('add_result', {'msg': ' '})
                socketio.emit('add_result', {'msg': IPactual})
                print('talos')
                while j < len(data_rbl):
                    tmp_rbl = str(data_rbl[j])
                    emit('add_result', {'msg': tmp_rbl})
                    j += 1
                emit('add_result', {'msg': ' '})
                emit('add_result', {'msg': 'Data en Talos'},broadcast=True)
                while t < len(data_talos):
                    tmp_talos = str(data_talos[t])
                    emit('add_result', {'msg': tmp_talos})
                    t += 1
                emit('add_result', {'msg': ' '})
                try:
                    data_BHD = BlackHatDirectory.SoupBHD(IP[c])
                    emit('add_result', {'msg': 'Data en BlackHatDirectory'},broadcast=True)
                    while e < len(data_BHD):
                        data_BHDtmp = data_BHD[e]
                        emit('add_result', {'msg': data_BHDtmp})
                        e += 1
                    emit('add_result', {'msg': ' '})
                except Exception as e:
                    error = str(e + ' BHD')
                    emit('add_result', {'msg': 'Error en BlackHatDirectory'})
                    emit('add_result', {'msg': error})
                    emit('add_result', {'msg': ' '})
            except Exception as e:
                error = str(e + ' RBL o Talos')
                '''return render_template('homeip.html', error=error)'''
                emit('add_result', {'msg': error})
            if data == "Sin Data alienvault":
                emit('add_result', {'msg': data})
                emit('add_result', {'msg': ' '})
            else:
                emit('add_result', {'msg': 'Pulsos en alienvault'})
                while d < len(data):
                    datactual = data[d]
                    emit('add_result', {'msg': datactual})
                    d += 1
                emit('add_result', {'msg': ' '})
            try:
                data_shodan = str(shodan_get.get_shodan(IP[c]))
                emit('add_result', {'msg': 'Data en Shodan'})
                emit('add_result', {'msg': data_shodan})
                emit('add_result', {'msg': ' '})
            except Exception as e:
                error = str(e + ' Shodan')
                emit('add_result', {'msg': error})
        except Exception as e:
            error = (str(e) + ' alienvault')
            emit('add_result', {'msg': error})
            emit('add_result', {'msg': ' '})
        c+=1
        c = float(c)
        porcentaje = (c/float(len(IP)))*100.0
        d = 0
        j = 0
        e = 0
        t = 0
        porcentaje = int(porcentaje)
        emit('updatebar', {'bar': porcentaje})
        c = int(c)
        print('end')

这是html文件的javascript部分:

<script type="text/javascript" charset="utf-8">
    var socket = io.connect('http://' + document.domain + ':' + location.port, {transports: ['websocket']});
// verify our websocket connection is established
    socket.on('connect', function() {
        console.log('Websocket connected!');
    });
    function analizar() {
      var IPTs = document.getElementById("IP");
      var IPT = IPTs.value;
      console.log('Enviando IP...'+ IPT);
      socket.emit('create', {IPT: IPT});
    }
    socket.on('add_result', function(data){
        var dat = String(data['msg']);
        $("#resultado").append(dat+'</br>');
        console.log(data['msg']);
    });
    socket.on('updatebar', function(barra){
        var valuebar = barra['bar'];
        $("#dynamic")
        .css("width", valuebar + "%")
        .attr("aria-valuenow", valuebar)
        .text(valuebar + "% Complete");
    });
  </script>

我想显示客户端“ A”发送的数据“ A”的结果,而不是像广播一样显示数据“ A”和“ B”。

0 个答案:

没有答案