how to make a python socket client take a screenshot and send it to the server [closed]

Closed. This question should be specified . Answers to it are not accepted at the moment.

Want to improve this question? Reformulate the question so that it focuses on only one problem.

Closed 1 month ago.

Improve the question

How can I take a screenshot of the client and send it to the server? I initially thought this would be an easy task but ran into a problem how can I accomplish it if you just pass the command, it will not be executed, but simply saved and that's it

Author: Selasi, 2020-12-25

1 answers

Server.py - a simple TCP server that accepts any message as a file. After writing the file, its real mime-type is checked. This is necessary for compatibility with Windows, because Windows does not want to open a file without an extension, unlike the mighty Linux. After that, the file is renamed with the addition of its mime-type. You can specify which IP address to run the server on. (0.0.0.0), so that hosts from different networks can connect to this server. localhost - only from this computer). You can also specify the directory to save incoming files to.

import socket
from os.path import expanduser
import os
import magic


class Server:

    def __init__(self, address='localhost', port=8080, save_dir=expanduser('~')):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind((address, port))
        self.file_id = 1
        self.save_dir = save_dir
        self.address = address
        self.port = port

    def run(self):
        self.socket.listen(1)
        while True:
            client_socket, client_address = self.socket.accept()
            file_path = os.path.join(self.save_dir, str(self.file_id))
            file = open(file_path, 'wb')
            while 1:
                data = client_socket.recv(1024)
                if data:
                    file.write(data)
                else:
                    break
            mime = magic.from_file(file_path, mime=True).split('/')[1]
            os.rename(file_path, file_path + '.' + mime)
            self.file_id = self.file_id + 1
            file.close()
            client_socket.close()


if __name__ == '__main__':
    server = Server()
    server.run()

Client.py - a simple TCP client that accepts a file and sends it to the TCP server. Also contains logic to take a screenshot using the PIL{[5] library]}

from os.path import expanduser
from PIL import ImageGrab
import socket
import os


class Client:

    def __init__(self, host_address='localhost', host_port=8080):
        self.socket = socket.socket()
        self.host_address = host_address
        self.host_port = host_port
        self.socket.connect((host_address, host_port))

    def send_file(self, file_name):
        file = open(file_name, 'rb')
        self.socket.sendfile(file)


def screenshot(to_file, mime='JPEG'):
    _screenshot = ImageGrab.grab()
    _screenshot.save(to_file, mime)


if __name__ == '__main__':
    file_name = os.path.join(expanduser('~'), '30.jpeg')
    screenshot(file_name)
    cli = Client()
    cli.send_file(file_name)

The two files are run separately. First the server, then the client. Every time you want to download a screenshot of the computer, you need to run client.py It seems that you will not even have to install third-party dependencies and it seems to be cross-platform(tested only on Ubuntu 20.04)

 3
Author: skoriy, 2020-12-25 19:25:30