[파이썬] socket 원격 데스크톱

In this blog post, we will explore how to create a remote desktop application using Socket programming in Python. With the help of sockets, we can establish a connection between a client and a server, allowing the client to remotely control the server’s desktop.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of Python programming and socket programming concepts. It will also be helpful to have the following libraries installed:

Creating the Server

Let’s start by creating the server-side application. The server will be responsible for capturing the desktop screen and sending it to the connected client.

import socket
import pyautogui

# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Get the local machine name and define the port to listen on
host = socket.gethostname()
port = 9999

# Bind the socket to a specific address and port
server_socket.bind((host, port))

# Listen for incoming connections
server_socket.listen(1)

# Accept a connection from the client
client_socket, client_address = server_socket.accept()

# Send the screen dimensions to the client
screen_width, screen_height = pyautogui.size()
client_socket.send(f"{screen_width},{screen_height}".encode())

# Continuously capture and send the desktop screen
while True:
    # Capture the screen and convert it to bytes
    screen = pyautogui.screenshot()
    screen_bytes = screen.tobytes()

    # Send the size of the screen bytes to the client
    client_socket.send(len(screen_bytes).to_bytes(4, 'big'))

    # Send the screen bytes to the client
    client_socket.sendall(screen_bytes)

Creating the Client

Now let’s create the client-side application. The client will connect to the server and display the received desktop screen.

import socket
from PIL import Image
from io import BytesIO
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt5.QtGui import QPixmap

# Create a socket object
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Get the local machine name and the server's port
host = socket.gethostname()
port = 9999

# Connect to the server
client_socket.connect((host, port))

# Receive the screen dimensions from the server
screen_dimensions = client_socket.recv(1024).decode().split(',')
screen_width, screen_height = map(int, screen_dimensions)

# Create the application window
app = QApplication([])
window = QMainWindow()
window.setGeometry(0, 0, screen_width, screen_height)

# Create a label to display the screen
label = QLabel(window)
label.setGeometry(0, 0, screen_width, screen_height)

# Continuously receive and display the desktop screen
while True:
    # Receive the size of the screen bytes from the server
    size_bytes = client_socket.recv(4)
    size = int.from_bytes(size_bytes, 'big')

    # Receive the screen bytes from the server
    screen_bytes = b''
    while len(screen_bytes) < size:
        screen_bytes += client_socket.recv(size - len(screen_bytes))

    # Convert the screen bytes to an image and display it
    screen_image = Image.open(BytesIO(screen_bytes))
    pixmap = QPixmap.fromImage(screen_image)
    label.setPixmap(pixmap)

    # Update the application
    app.processEvents()

Conclusion

In this blog post, we have seen how to create a remote desktop application using Socket programming in Python. We created a server application that captures the desktop screen and sends it to a connected client. The client application receives the screen bytes and displays them on the screen using PyQt5.

With socket programming, we can achieve various networked applications, and remote desktop is just one example. By expanding and enhancing this basic implementation, we can build more sophisticated remote desktop solutions.