1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Flutter stack overflow error on using python script

Discussão em 'Python' iniciado por Stack, Agosto 30, 2025.

  1. Stack

    Stack Membro Participativo

    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. [​IMG]

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

    Continue reading...

Compartilhe esta Página