使用python监控电脑<二>

这个是使用python监控电脑 的第二个版本,就称之为beta2吧,主要做了如下改动

  1. 邮件发送到时候将两张图片以html格式发送,而不是以附件的方式发送.
  2. 由于笔记本摄像头启动之后直接拍摄照片的话拍摄出来的图片会是黑乎乎的一片,所以做了一些调整,添加了一个循环,等摄像头完全开启之后在拍摄照片,这样效果会更好.
  3. 添加了time.sleep(240) 就是在电脑开机之后4分钟在进行下面的操作.

好了,就是这些,下面是代码

#author:liangliang
#email:liangliangyy@gmail.com
#blog:http://www.lylinux.org/
import time
from VideoCapture import Device
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib
from PIL import ImageGrab
from PIL import ImageEnhance
import os

def get_correcttime() :
    open_time = time.strftime('%m%d%H%M',time.localtime(time.time()))
    return open_time

def get_desktopimg(correcttime):
    filename = r"F:/learn/watchcomputer/desktop" + correcttime + ".jpg"
    pic = ImageGrab.grab()
    pic.save(filename)
    print "desktop img saved ok!!!!"
    return filename

def get_webcamimg(correcttime):
    filename = r"F:/learn/watchcomputer/webcam" + correcttime + ".jpg"
    

    res = (640,480)
    cam = Device()
    cam.setResolution(res[0],res[1])

    brightness = 1.0
    contrast = 1.0

    i = 0
    while i<300:
        camshot = ImageEnhance.Brightness(cam.getImage()).enhance(brightness)
        camshot = ImageEnhance.Contrast(camshot).enhance(contrast)
       # filename = "test/"+ str(time.time()) + ".jpg"
        i = i + 1
        print i
    cam.saveSnapshot(filename,timestamp=3, boldfont=1, quality=80)
    print "webcam img saved ok!!!!!!"
    return filename

def send_img(desktop,webcam):
    try:
        from_mail = "liangliangyy@gmail.com"
        to_mail = "liangliangyy@gmail.com"
        msg = MIMEMultipart()
        msg['From'] = from_mail
        msg['To'] = to_mail
        msg['Subject'] = 'my computer'
        body = 'test img send'
        html_code = '<b><i>the guy who use my computer and the screen shot.</i></b> <img alt="desktop" src="cid:image1" width="683" height="384" /><img alt="webcam" src="cid:image2" />'
        print html_code
        con = MIMEText(html_code,'html','utf-8')
        #msg.attach(con)
        #img1 = MIMEImage(file(desktop,'rb').read())
        #img2 = MIMEImage(file(webcam,'rb').read())
        #msg.attach(img1)
        #msg.attach(img2)
        msg.attach(con)
        img1 = MIMEImage(file(desktop,'rb').read())
        img2 = MIMEImage(file(webcam,'rb').read())
        img1.add_header('Content-ID','')
        img2.add_header('Content-ID','')
        msg.attach(img1)
        msg.attach(img2)
        server = smtplib.SMTP('smtp.gmail.com')
        server.docmd('ehol',from_mail)
        server.starttls()
        server.login(from_mail,'password')
        server.sendmail(from_mail,to_mail,msg.as_string())
        server.quit()
        print "mail send ok!!!!!"
        return True
    except:
        return False

def write_log(issuccess):
    log_file_dir = r"F:/learn/watchcomputer/computer.log"
    log_flie = file(log_file_dir,'a')
    log_flie.write('at '+now_time + '  img saved success! ')
    if(issuccess):
        log_flie.write('mail send success!!!!!')
    else:
        log_flie.write('mail send failed.....')
    log_flie.write('\n\n')
    log_flie.close()

time.sleep(240)
now_time = get_correcttime()
desktopimg = get_desktopimg(now_time)
webcamimg  = get_webcamimg(now_time)

if(os.path.isfile(desktopimg) and os.path.isfile(webcamimg)):
    MailIsSend = send_img(desktopimg,webcamimg)
    write_log(MailIsSend)
else:
    exit(1)

print 'haaa  running success !!!!'
exit(1)

使用python监控电脑<二>》上有3条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注