1

Hi Im trying to build an application in python 3 for screen sharing and I looked in other codes from the internet (some of them from StackOverFlow) and all of them just rapidly open up screenshots of my screen without capturing them into one screen , so its impossible to work with

i cant work with a screen sharing that pops up 100 tabs of screenshot every few seconds ( i dont think this is how its supposed to work)

I would like to capture them into one screen so i can work with it and also that it wont make my screen so messy with one hundred open tabs of screenshots that i cant handle

I would love for some help or technical advice

here is the code i found From stack over flow:

Client.py

from socket import socket
from zlib import decompress

import pygame

WIDTH = 1900
HEIGHT = 1000


def recvall(conn, length):
    """ Retreive all pixels. """

    buf = b''
    while len(buf) < length:
        data = conn.recv(length - len(buf))
        if not data:
            return data
        buf += data
    return buf


def main(host='127.0.0.1', port=5001):
    pygame.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()
    watching = True

    sock = socket()
    sock.connect((host, port))
    try:
        while watching:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    watching = False
                    break

            # Retreive the size of the pixels length, the pixels length and pixels
            size_len = int.from_bytes(sock.recv(1), byteorder='big')
            size = int.from_bytes(sock.recv(size_len), byteorder='big')
            pixels = decompress(recvall(sock, size))

            # Create the Surface from raw pixels
            img = pygame.image.fromstring(pixels, (WIDTH, HEIGHT), 'RGB')

            # Display the picture
            screen.blit(img, (0, 0))
            pygame.display.flip()
            clock.tick(60)
    finally:
        sock.close()


if __name__ == '__main__':
    main()

Server.py

from socket import socket
from threading import Thread
from zlib import compress

from mss import mss


WIDTH = 1900
HEIGHT = 1000


def retreive_screenshot(conn):
    with mss() as sct:
        # The region to capture
        rect = {'top': 0, 'left': 0, 'width': WIDTH, 'height': HEIGHT}

        while 'recording':
            # Capture the screen
            img = sct.grab(rect)
            # Tweak the compression level here (0-9)
            pixels = compress(img.rgb, 6)

            # Send the size of the pixels length
            size = len(pixels)
            size_len = (size.bit_length() + 7) // 8
            conn.send(bytes([size_len]))

            # Send the actual pixels length
            size_bytes = size.to_bytes(size_len, 'big')
            conn.send(size_bytes)

            # Send pixels
            conn.sendall(pixels)


def main():
    sock = socket()
    sock.bind(('0.0.0.0', 5001))
    try:
        sock.listen(5)
        print('Server started.')

        while 'connected':
            conn, addr = sock.accept()
            print('Client connected IP:', addr)
            thread = Thread(target=retreive_screenshot, args=(conn,))
            thread.start()
    finally:
        sock.close()


if __name__ == '__main__':
    main()

p.s maybe it opens up so many tabs because im using it on my computer - and it supposed to work between two computers. i do not know , would love for some help

A link the the original code's page: screen sharing in python

1 Answer 1

0

It's not opening tab it is just showing your screen inside screen... For share between two pc all you need to add host machine/computer IPv4 Address for example: in your server and client code. Which you will get by typing ipconfig \ ifconfig in your terminal/cmd:
your server code:
server.py

#---code
def main():
    sock = socket()
    sock.bind(('192.168.0.xxx',5001))
#----code

client.py

#---code
def main(host='192.168.0.xxx',port=5001):
#---code
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.