没有名为“websockets.legacy”的模块
Posted
技术标签:
【中文标题】没有名为“websockets.legacy”的模块【英文标题】:No module named 'websockets.legacy' 【发布时间】:2021-12-10 07:22:09 【问题描述】:我正在尝试使用 auto-py-to-exe 构建一个 exe。 exe构建成功,但现在我的websocket服务器 程序不工作。 如果有人知道这个问题的解决方案,请告诉我。
代码:
import asyncio
import websockets
import win32gui
import win32ui
import win32con
import win32com.client
import win32api
import json
from PIL import Image, ImageFile
import io
import time
shell = win32com.client.Dispatch('WScript.Shell')
ImageFile.MAXBLOCK = 2**20
def left_click(pos_x, pos_y):
win32api.SetCursorPos((pos_x, pos_y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, pos_x, pos_y, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, pos_x, pos_y, 0, 0)
def right_click(pos_x, pos_y):
win32api.SetCursorPos((pos_x, pos_y))
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, pos_x, pos_y, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, pos_x, pos_y, 0, 0)
async def echo(websocket, path):
while True:
try:
raw_data = await websocket.recv()
data = json.loads(raw_data)
if data['type'] == 'click':
pos_x = data['x']
pos_y = data['y']
if data['click_type'] == 1:
left_click(pos_x, pos_y)
elif data['click_type'] == 2:
left_click(pos_x, pos_y)
left_click(pos_x, pos_y)
elif data['click_type'] == 3:
right_click(pos_x, pos_y)
else:
pass
if data['type'] == 'keydown':
cmd = ''
if data['shiftKey']:
cmd += '+'
if data['ctrlKey']:
cmd += '^'
if data['altKey']:
cmd += '%'
if len(data['key']) == 1:
cmd += data['key'].lower()
else:
cmd += ''+data['key'].upper()+''
shell.SendKeys(cmd)
except websockets.ConnectionClosed as exc:
pass
except Exception as ex:
pass
try:
PREV_IMG = None
hdesktop = win32gui.GetDesktopWindow()
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
# device context
desktop_dc = win32gui.GetWindowDC(hdesktop)
img_dc = win32ui.CreateDCFromHandle(desktop_dc)
# memory context
mem_dc = img_dc.CreateCompatibleDC()
screenshot = win32ui.CreateBitmap()
screenshot.CreateCompatibleBitmap(img_dc, width, height)
mem_dc.SelectObject(screenshot)
bmpinfo = screenshot.GetInfo()
# copy into memory
mem_dc.BitBlt((0, 0), (width, height), img_dc, (left, top),win32con.SRCCOPY)
bmpstr = screenshot.GetBitmapBits(True)
pillow_img = Image.frombytes('RGB',
(bmpinfo['bmWidth'], bmpinfo['bmHeight']),
bmpstr, 'raw', 'BGRX')
with io.BytesIO()as image_data:
pillow_img.save(image_data, "JPEG", quality=80, optimize=True, progressive=True)
image_data_content = image_data.getvalue()
if image_data_content!= PREV_IMG:
await websocket.send(image_data_content)
PREV_IMG = image_data_content
mem_dc.DeleteDC()
win32gui.DeleteObject(screenshot.GetHandle())
except websockets.ConnectionClosed as exc:
pass
except Exception as ex:
pass
async def main():
async with websockets.serve(echo, "", 9999):
await asyncio.Future() # run forever
asyncio.run(main())
这个程序是为rdp和vnc做的,它使用websockets成为一个服务器,然后客户端可以通过连接它来访问计算机。
【问题讨论】:
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。 【参考方案1】:转到高级并添加隐藏的导入 (websockets.legacy) 直到成功。您可能还需要添加其他隐藏的导入(找不到的那些)
【讨论】:
以上是关于没有名为“websockets.legacy”的模块的主要内容,如果未能解决你的问题,请参考以下文章