1

I was trying to integrate a basic network scanner using python into flutter. The scanner function returns a List in the format List[List, Int]. To call this I use MainActivity.kt like this

package com.example.ctos

import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import com.chaquo.python.Python
import com.chaquo.python.android.AndroidPlatform

class MainActivity: FlutterActivity() {
    private val CHANNEL = "python"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        
        // Initialize Python
        if (!Python.isStarted()) {
            Python.start(AndroidPlatform(this))
        }
        
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
            when (call.method) {
                "startScan" -> {
                    val hosts = call.argument<String>("hosts")
                    val ports = call.argument<String>("ports")
                    val scanType = call.argument<String>("scanType")
                    
                    val py = Python.getInstance()
                    val results = py.getModule("network_scanner")
                        .callAttr("startScan", hosts, ports, scanType)
                    
                    result.success(results)
                }
                else -> result.notImplemented()
            }
        }
    }
}

And a python bridge to call the script

import 'package:flutter/services.dart';

class PythonBridge {
  static const MethodChannel _channel = MethodChannel('python');

  static Future<List> scanPort(
      String hosts, String ports, String scanType) async {
    return await _channel.invokeMethod(
        'startScan', {'hosts': hosts, 'ports': ports, "ScanType": scanType});
  }
}

and in the dart fileusing a try catch I am calling it like this

 var results = await _channel.invokeMethod('startScan', {
        'hosts': '192.168.84.229',
        'ports': '8999-9001',
        'scanType': 'tcp_scan'
      });

      print(results);

The python script have print statements to make sure its working and whenever the function is called through dart I can see the output is getting printed after all scan is done. Scans are done through threads for parallel processing.

with ThreadPoolExecutor(max_workers=max_threads) as executor:
        future_to_target = {
            executor.submit(tcp_single_scan, host, port, timeout): (host, port) for host, port in scan_targets
        }
        
        for future in as_completed(future_to_target):
            result = future.result()
            results.append(result)
            completed+=1
            progress = (completed / total) * 100
            if(int(progress) != int_progress):
                print(str(int(progress))+"%")
                int_progress = int(progress)

But after all scan done I am getting stack overflow error and app gets exit. error image

Is this something related to threads. I even tried with just scanning one port and still causing issue.

1
  • 3
    always put error message as text, not image. We can't select text on image to use it in comment or in answer or to search in Google. Commented Aug 30 at 9:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.