您好,我试图在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()
p.s 也许它打开了很多标签,因为我在我的计算机上使用了它-它应该可以在两台计算机之间工作。我不知道,想寻求帮助
链接原始代码的页面: screen sharing in python
答案 0 :(得分:0)
嗨,它没有打开标签,只是在屏幕内显示屏幕...为了在两台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