我无法发送图像(socket 和 ngrok)

问题描述 投票:0回答:1

客户:

from PIL import Image, ImageTk
from tkinter import *
import socket, threading, base64
window = Tk()
window.title('Stream')
window.geometry('810x540')
window.configure(bg='black')
def GetPic():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(('localhost', 8000))
    server_socket.listen(1)
    client_socket, client_address = server_socket.accept()
    while True:

        #try:
        dir2 = r'C:\Users\SystemX\AppData\Local\Temp\screen_tr2.png'

        data = client_socket.recv(11111393216) # 
        data = base64.b64decode(data)
            
        with open(dir2, 'wb') as f:
                f.write(data)
                
        img = PhotoImage(file=dir2)
        lbl.configure(image=img)
        #except Exception as e:
        #    print(e)
            
#os.system(dir2)
lbl = Label(window, bg='black', image=None)
lbl.place(x=0,y=0)
threading.Thread(target=GetPic).start()
window.mainloop()

服务器:

from PIL import ImageGrab
from tkinter import *
import socket, time, base64

dir = r'C:\Users\SystemX\AppData\Local\Temp\screen_tr.png'

ip, port = 'IP:PORT'.split(':')

timee=time.time()
print('started')
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((ip, int(port)))
while True:

    #time.sleep(2)
    screenshot = ImageGrab.grab()
    print(f'grabbed ({time.time() - timee}s)')
    screenshot.save(dir)
    print(f'saved ({time.time() - timee}s)')
    with open(dir, 'rb') as f:
        content = f.read()
        #print(content)
        enc = base64.b64encode(content)
        client_socket.send(enc)
        #client_socket.send(content)

#server_socket.close()

图像传输过程中中断。我该如何解决这个问题?

图像未完全传输。 ngrok 似乎破坏了形象。也许还有其他合适的方式打开端口? 当传输未经 Base64 解码的图像时,图像也会损坏。

python sockets ngrok
1个回答
0
投票

这将帮助您解决问题的套接字部分。我已经更改了服务器,以便它在“读取”上循环,直到返回 0,这在套接字关闭时发生。这使得它能够接收整个图像。请注意,我已将

break
添加到两个循环中,这样它就不会永远持续下去。我还删除了 tkinter 部分,以避免过多地混淆问题。

请注意,“服务器”和“客户端”是相反的。执行“接受”操作的是服务器。进行“连接”的就是客户端。我还删除了

ImageGrab
,因为这在我拥有的 Wayland 的 Linux 上不起作用。

服务器:

from PIL import Image, ImageTk
import socket

BUF = 16384
def GetPic():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(('localhost', 8000))
    server_socket.listen(1)
    client_socket, client_address = server_socket.accept()
    dir2 = 'p2.png'
    while True:
        data = b''
        while True:
            buf = client_socket.recv(BUF)
            if not buf:
                break
            data += buf
            print(len(buf),len(data))
            
        print('received',len(data),'bytes')
        with open(dir2, 'wb') as f:
            f.write(data)
        break

GetPic()

客户:

from PIL import ImageGrab
import socket, time, base64

dir = 'xxxxxx.png'

ip, port = 'localhost:8000'.split(':')

timee=time.time()
print('started')
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((ip, int(port)))
while True:
    with open(dir, 'rb') as f:
        content = f.read()
    print('sending',len(content),'bytes')
    client_socket.sendall(content)
    break

从哲学上讲,您所做的是将桌面图像回显到另一台计算机的糟糕方法。有完善且高度优化的工具可以帮助您完成该任务。

© www.soinside.com 2019 - 2024. All rights reserved.