python - Python屏幕共享

标签 python sockets screensharing python-mss

您好,我试图在python 3中构建一个用于屏幕共享的应用程序,我从互联网上查看了其他代码(其中一些来自StackOverFlow),所有这些代码都只是快速打开了我的屏幕截图,而没有将它们捕获到一个屏幕中,不可能一起工作

我无法使用每隔几秒钟就会弹出100个屏幕截图的屏幕共享的工作方式(我不认为这是应该工作的方式)

我想将它们捕获到一个屏幕中,以便我可以使用它
而且它不会让我的屏幕变得困惑不堪,无法打开屏幕截图的一百个选项卡

我希望获得一些帮助或技术建议

这是我从堆栈溢出流中找到的代码:

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()


也许它打开了很多标签,因为我在我的计算机上使用了它-它应该可以在两台计算机之间工作。我不知道,很想帮忙

原始代码页面的链接:
screen sharing in python

最佳答案

您好它的未打开选项卡,它只是在屏幕内显示您的屏幕...要在两台PC之间共享,您需要添加主机/计算机的IPv4地址,例如:
在服务器和客户端代码中..您可以通过在终端/cmd中键入ipconfig\ifconfig来获得:
您的服务器代码:
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

关于python - Python屏幕共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61362185/

相关文章:

python - 如何更改 .npz 文件中的值?

python - 完成当前迭代后,如何结束带有套接字操作的无限循环?

sockets - epoll_data结构存在缺陷,是否需要改进?

c# - 在屏幕共享程序中存储带哈希的图像缓冲区的内存管理

plugins - 编写屏幕共享插件/驱动程序

python - Matlab delaunayn 和 Scipy Delaunay 之间的区别

python - 单例生成器,多个消费者

python - 函数python的短路列表

java - 从生物识别指纹考勤设备中检索数据

ios - iOS Objective C 中的 Tokbox 屏幕共享开/关切换