I’m working on a Flask web application that is supposed to run an Nmap scan and provide real-time updates to the client via Flask-SocketIO. However, I’m encountering an issue where the Nmap scan doesn’t seem to be executing, and no real-time data is being sent to the client. The Flask server doesn’t show any errors, and the WebSocket connection appears to be established correctly, but the start_nmap_scan event doesn’t seem to trigger the Nmap scan.
Here’s my Flask app setup (app.py):
from flask import Flask, render_template
from flask_socketio import SocketIO
import subprocess
import threading
app = Flask(__name__)
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
def run_nmap_realtime(url):
def emit_output(proc):
for line in iter(proc.stdout.readline, b''):
socketio.emit('nmap_output', {'data': line.decode()})
proc = subprocess.Popen(["nmap", "-sC", "-sV", url], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
threading.Thread(target=emit_output, args=(proc,)).start()
@socketio.on('start_nmap_scan')
def handle_nmap_scan(json):
run_nmap_realtime(json['url'])
if __name__ == '__main__':
socketio.run(app, debug=True)
And here’s my JavaScript (scripts.js):
var socket = io.connect('http://' + document.domain + ':' + location.port);
socket.on('connect', function() {
console.log('WebSocket connected!');
});
function performSearch() {
var url = document.getElementById('url-input').value;
socket.emit('start_nmap_scan', {url: url});
}
socket.on('nmap_output', function(msg) {
document.querySelector('.recons-box').innerText += msg.data;
});
// Code to handle content change omitted for brevity
I’ve confirmed that the WebSocket connection is established, but when I click the “Search” button to start the Nmap scan, nothing happens. No data is emitted back to the client, and there are no errors in the Flask log.
I can only make it work if I don’t use WebSocket in general, but the output from Nmap shows up after the scan is complete which is not ideal.



