0

The need is server takes it network data and send it all the client and the client shows the data.
This is server code.

from flask import Flask
from flask_cors import CORS
from flask_socketio import SocketIO, send
import scapy.all as scapy

app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*")

@app.route('/')
def home():
    return 'hey!'

@socketio.on('connect')
def test_connect():
    print('connected')

def send_sniffed_packet(packet):
    socketio.emit('packet', packet, broadcast=True)

if __name__ == "__main__":
    socketio.run(app)
    scapy.sniff(prn=send_sniffed_packet)

The problem is if the code runs the socket it doesn't run scapy and I interchange the line scapy will run and flask won't. How to make both run??
This is html code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js" integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==" crossorigin="anonymous"></script>
</head>
<body>
    <div></div>
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            var socket = io.connect('http://localhost:5000');

            socket.on('connect', () => {
                console.log("Connected");
            });
            socket.on('packet', (msg)=>{
                console.log(msg);
            })
        });
    </script>    
</body>
</html>
Subramanya G
  • 39
  • 2
  • 8

1 Answers1

0

Try running one of the processes in a background thread. For example:

if __name__ == "__main__":
    socketio.start_background_task(scapy.sniff, prn=send_sniffed_packet)
    socketio.run(app)
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • socketio don't had a start_background_thread, it had start_background_task and using that make it tosniff and don't do flask task. – Subramanya G May 01 '21 at 06:23