In today’s tech-savvy world, the ability to remotely control desktop applications adds a new dimension to productivity and convenience. Leveraging Python and Django, we can craft a powerful solution that facilitates remote control over various desktop functionalities. Let’s delve into the intricacies of achieving this feat using Django views and subprocess module
Notepad Control
import subprocess
import time
import pyautogui
from django.contrib import messages
from django.shortcuts import render, redirect
from django.http import HttpResponseBadRequest
# Function for controlling Notepad
def notepad_control(request):
if request.method == 'POST':
action = request.POST.get('action')
if action == 'open_notepad':
try:
# Open Notepad
subprocess.Popen(['notepad.exe'])
time.sleep(2)
text_to_write = request.POST.get('text_to_write', '') # Get text from form
if text_to_write:
pyautogui.typewrite(text_to_write) # Type text into Notepad
else:
raise ValueError("No text provided")
# Save text if checkbox is checked
save_option = request.POST.get('save_option')
if save_option == 'save':
pyautogui.hotkey('ctrl', 's')
time.sleep(1)
# Close Notepad if checkbox is checked
close_option = request.POST.get('close_option')
if close_option == 'close':
time.sleep(5)
subprocess.Popen(['taskkill', '/f', '/im', 'notepad.exe'])
messages.success(request, 'Notepad opened and text written successfully.')
except Exception as e:
messages.error(request, f'An error occurred: {str(e)}')
return redirect('notepad_control')
return render(request, 'control_notepad.html')
Camera Control
# Function for controlling Camera
def control_desktop_camera(request):
if request.method == 'POST':
action = request.POST.get('action')
if action == 'open_camera':
try:
# Open Camera
subprocess.Popen(["start", "microsoft.windows.camera:"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
messages.success(request, 'Camera opened successfully.')
except subprocess.CalledProcessError as e:
messages.error(request, f'An error occurred: {e.stderr.decode()}')
except Exception as e:
messages.error(request, f'An error occurred: {str(e)}')
elif action == 'close_camera':
try:
# Close Camera
subprocess.Popen(["taskkill", "/f", "/im", "WindowsCamera.exe"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
messages.success(request, 'Camera closed successfully.')
except subprocess.CalledProcessError as e:
messages.error(request, f'An error occurred: {e.stderr.decode()}')
except Exception as e:
messages.error(request, f'An error occurred: {str(e)}')
else:
# Handle invalid actions
return HttpResponseBadRequest("Invalid action requested.")
return redirect('control_desktop_camera')
# Handle GET requests
return render(request, 'control_camera.html')
Command Execution
# Function for executing commands
def cmd_exec(request):
if request.method == 'POST':
cmd = request.POST.get('cmd_input')
try:
# Execute command
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if result.returncode == 0:
return render(request, 'result.html', {'result': result})
else:
return render(request, 'error.html', {'error_message': f"Command '{cmd}' failed with return code {result.returncode}"})
except Exception as e:
return render(request, 'error.html', {'error_message': f"Error executing command '{cmd}': {str(e)}"})
return render(request, 'index.html')
Urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.control_desktop_Camera, name='control_desktop_Camera'),
path('notepad', views.notepad_control, name='notepad_control'),
path('desktop', views.cmd_exec, name='cmd_exec'),
]