# Buggy code
def screen_cast(request):
    """Start or Stop ScreenCast Feature"""
    logger.info("Invoking ScreenCast Service in VM/Device")
    try:
        global TCP_SERVER_MODE
        data = {}
        if request.method == "POST":
            mode = request.POST["mode"]
            if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD":
                ip_address = "10.0.2.2"
            else:
                ip_address = settings.SCREEN_IP
            port = str(settings.SCREEN_PORT)
            if mode == "on":
                args = [
                    "am",
                    "startservice",
                    "-a",
                    ip_address + ":" + port,
                    "opensecurity.screencast/.StartScreenCast",
                ]
                data = {"status": "on"}
                TCP_SERVER_MODE = "on"
            elif mode == "off":
                args = ["am", "force-stop", "opensecurity.screencast"]
                data = {"status": "off"}
                TCP_SERVER_MODE = "off"
            if mode in ["on", "off"]:
                try:
                    adb_command(args, True)
                    screen_trd = threading.Thread(target=screencast_service)
                    screen_trd.setDaemon(True)
                    screen_trd.start()
                except:
                    PrintException("[ERROR] Casting Screen")
                    data = {"status": "error"}
                    return HttpResponse(
                        json.dumps(data), content_type="application/json"
                    )
            else:
                data = {"status": "failed"}
        else:
            data = {"status": "failed"}
        return HttpResponse(json.dumps(data), content_type="application/json")
    except:
        PrintException("[ERROR] Casting Screen")
        return HttpResponseRedirect("/error/")


# Traceback (KeyError)
[INFO] 08/Feb/2019 16:17:54 - VM Starting
[INFO] 08/Feb/2019 16:17:55 - "POST /DynamicAnalyzer/ HTTP/1.1" 200 32400
[INFO] 08/Feb/2019 16:17:55 - "GET /static/css/devices.min.css HTTP/1.1" 200 44241
[INFO] 08/Feb/2019 16:17:55 - "GET /download/screen/screen.png HTTP/1.1" 302 0
[DEBUG] 08/Feb/2019 16:17:55 - Exception while resolving variable 'exp' in template 'general/error.html'.
Traceback (most recent call last):
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/base.py", line 829, in _resolve_lookup
current = current[bit]
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/context.py", line 83, in __getitem__
raise KeyError(key)
KeyError: 'exp'

During handling of the above exception, another exception occurred:

# Fixed code
def screen_cast(request):
    """Start or Stop ScreenCast Feature"""
    logger.info("Invoking ScreenCast Service in VM/Device")
    try:
        global TCP_SERVER_MODE
        data = {}
        if request.method == "POST":
            mode = request.POST["mode"]
            if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD":
                ip_address = "10.0.2.2"
            else:
                ip_address = settings.SCREEN_IP
            port = str(settings.SCREEN_PORT)
            if mode == "on":
                args = [
                    "am",
                    "startservice",
                    "-a",
                    ip_address + ":" + port,
                    "opensecurity.screencast/.StartScreenCast",
                ]
                data = {"status": "on"}
                TCP_SERVER_MODE = "on"
            elif mode == "off":
                args = ["am", "force-stop", "opensecurity.screencast"]
                data = {"status": "off"}
                TCP_SERVER_MODE = "off"
            if mode in ["on", "off"]:
                try:
                    adb_command(args, True)
                    screen_trd = threading.Thread(target=screencast_service)
                    screen_trd.setDaemon(True)
                    screen_trd.start()
                except:
                    PrintException("Casting Screen")
                    data = {"status": "error"}
                    return HttpResponse(
                        json.dumps(data), content_type="application/json"
                    )
            else:
                data = {"status": "failed"}
        else:
            data = {"status": "failed"}
        return HttpResponse(json.dumps(data), content_type="application/json")
    except:
        PrintException("Casting Screen")
        return print_n_send_error_response(request, "Error Casting Screen", True)