为什么服务器要花这么多时间来接收消息?Python套接字
我正在尝试编写一个简单的共享屏幕脚本。出于某种原因,服务器接收消息需要花费大量时间。我不知道问题出在哪里。 如果你有一个解决方案,或者有一个加速过程的方法,那将非常有帮助。无论如何,我很感激你的帮助 服务器:为什么服务器要花这么多时间来接收消息?Python套接字,python,python-sockets,Python,Python Sockets,我正在尝试编写一个简单的共享屏幕脚本。出于某种原因,服务器接收消息需要花费大量时间。我不知道问题出在哪里。 如果你有一个解决方案,或者有一个加速过程的方法,那将非常有帮助。无论如何,我很感激你的帮助 服务器: import socket import pickle import select import numpy import cv2 def receive_msg(socket, HEADERSIZE):#receive message try: readySoc
import socket
import pickle
import select
import numpy
import cv2
def receive_msg(socket, HEADERSIZE):#receive message
try:
readySockets, _, _ = select.select([socket], [], [], 0.02)
if readySockets:
msgLen = socket.recv(HEADERSIZE)#receive the header/the message length
msgLen = int(msgLen)#convert fron bytes to int
msg = socket.recv(msgLen)#resive the size of the message
while len(msg) < msgLen:#if dont receive the full message / if the size of the message smaller than the size that the message sepose to be
msg += socket.recv(msgLen - len(msg))#add to the message the part that missing (the full size of the message - the size of the message that the program received)
msg = pickle.loads(msg)#extract message
else:
msg = False
except:
msg = False
return msg#return the complite massage
HEADERSIZE = 10
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1243))
s.listen(5)
clientsocket, address = s.accept()
while True:
# now our endpoint knows about the OTHER endpoint.
msg = receive_msg(clientsocket, HEADERSIZE)
if msg is not False:
image = cv2.cvtColor(msg, cv2.COLOR_RGB2BGR)#numpy array to open cv image
cv2.imshow("image", image)#show image
cv2.waitKey(1)
需要多长时间?你的屏幕有多大?从客户端到服务器有多少带宽?延迟为5.4秒。屏幕是高清的。每条消息都是6220964字节。这是延迟(发送/接收之间的时间)还是延迟(多次发送之间的时间)?是多次发送之间的时间。因此,您是否验证了延迟实际上来自传输图像,而不是捕获、转换、打包图像并将其推送到套接字?需要多长时间?你的屏幕有多大?从客户端到服务器有多少带宽?延迟为5.4秒。屏幕是高清的。每个消息都是6220964字节。这是延迟(发送/接收之间的时间)还是延迟(多次发送之间的时间)?是多次发送之间的时间?因此,您是否验证了延迟实际上是来自传输图像,而不是捕获、转换、打包图像并将其推送到套接字?
import socket
import numpy as np
import pyautogui
import pickle
def get_screen():
img = pyautogui.screenshot()# take a screenshot
img = np.array(img)#from image to array
return img
def send_msg(socket, msg, HEADERSIZE):#send a message
msg = pickle.dumps(msg)#comprass the msg
#give the mag a header / signature of the size of the message
msg = bytes(str(len(msg)) + (" " * (HEADERSIZE - len(str(len(msg))))), 'utf-8') + msg
socket.send(msg)#send the msg
HEADERSIZ = 10
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1243))
while True:
send_msg(s, get_screen(), HEADERSIZ)#send a image of the screen to the server