嗨,我正试图在python 3中构建一个用于屏幕共享的应用程序,我从互联网上查找了其他代码(其中一些来自StackOverFlow(,所有这些代码都只是快速打开我的屏幕截图,而不将其捕获到一个屏幕中,因此不可能使用
我不能使用每几秒钟弹出100个屏幕截图标签的屏幕共享(我不认为这是应该的工作方式(
我想把它们捕捉到一个屏幕上,这样我就可以使用它了而且它不会让我的屏幕上有一百个打开的屏幕截图标签,我无法处理
我很想得到一些帮助或技术建议
这是我从堆栈溢出中找到的代码:
客户端.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()
服务器.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也许它打开了这么多标签,因为我在电脑上使用它——而且它应该在两台电脑之间工作。我不知道,很想得到的帮助
原始代码页面的链接:python 中的屏幕共享
hi它没有打开选项卡,它只是在屏幕内显示您的屏幕。。。对于两台电脑之间的共享,您只需要添加主机/计算机IPv4地址,例如:在服务器和客户端代码中。。通过在终端/cmd中键入ipconfig
\ifconfig
即可获得:
您的服务器代码:
服务器.py
#---code
def main():
sock = socket()
sock.bind(('192.168.0.xxx',5001))
#----code
客户.py
#---code
def main(host='192.168.0.xxx',port=5001):
#---code