preface

Several days didn’t give you write an article, the reason is a little busy these days, and he immediately began school, so there are some things that some less, but after that, I will adjust time, try to bring a week 3 or 4 of article, the following a period of time, the article content will have some associated with Python foundation, after all, the purpose is to study together, So I’ll cover some of Python’s common data structures and some advanced uses for those who need them. Back to the point, today is to bring you a small Python program, thought is quite interesting, so I share with you. Let’s take a look at the results of the program.





When you run the program, it will send a message to your wechat account, which you can use to perform different functions, such as retrieving current computer users





If you are out and your computer is open, you want to know who is using your computer, then this function will help you, and the person will not notice, if you don’t want him to use your computer, then you can execute the shutdown command. Just think about it, if your best friend is eating chicken on your computer, and you shut down your computer with wechat when he is about to eat chicken, I think he must be confused at this moment.



Program way of thinking

The implementation of this small program is also relatively simple, mainly using OpencV and itChat library. Opencv library is a third party visual library in Python, which implements many general algorithms in image processing and computer vision. If you need to do face recognition or TWO-DIMENSIONAL code verification, you may use this library. Overall, the third-party library is very powerful, and interested readers can refer to the official documents on Baidu to learn. At present, the program only adds two functions, one is to obtain the image information of the current user, call the camera of the computer through OpencV, take a picture of the current user using the computer, and send it to your wechat. As for the second function remote shutdown is by calling OS library, send CMD name can be realized. Of course, if you want to implement the specific functionality you want, you just need to add specific commands and related libraries.



Program source code



import itchat
import os
import time
import cv2

sendMsg = u{message assistant} : unable to reply at this time
usageMsg = u"Use method: \n1. Run CMD command: CMD XXX (XXX is command)\n" \
           u"- For example shutdown command: NCMD shutdown -s -t 0 \n" \
           u"2. Get current computer user: cap\n3. Enable message Assistant (off by default) : ast\n" \
           u"4. Turn off message assistant: ASTC"
flag = 0 # Message assistant switch
nowTime = time.localtime()
filename = str(nowTime.tm_mday)+str(nowTime.tm_hour)+str(nowTime.tm_min)+str(nowTime.tm_sec)+".txt"
myfile = open(filename, 'w')

@itchat.msg_register('Text')
def text_reply(msg):
    global flag
    message = msg['Text']
    fromName = msg['FromUserName']
    toName = msg['ToUserName']

    if toName == "filehelper":
        if message == "cap":
            cap = cv2.VideoCapture(0)
            ret, img = cap.read()
            cv2.imwrite("weixinTemp.jpg", img)
            itchat.send('@img@%s'%u'weixinTemp.jpg'.'filehelper')
            cap.release()
        if message[0:3] == "cmd":
            os.system(message.strip(message[0:4]))
        if message == "ast":
            flag = 1
            itchat.send("Message assistant is on."."filehelper")
        if message == "astc":
            flag = 0
            itchat.send("Message assistant is closed."."filehelper")
    elif flag == 1:
        itchat.send(sendMsg, fromName)
        myfile.write(message)
        myfile.write("\n")
        myfile.flush()

if __name__ == '__main__':
    itchat.auto_login(hotReload=True)
    itchat.send(usageMsg, "filehelper")
    itchat.run()

Copy the code




Using the tutorial

Get the source code and run it on your computer. A QR code for wechat web login will pop up.

Use your mobile phone wechat scan login, wait for a while, wechat file assistant will receive the corresponding operation information.





To shutdown the current computer, send CMD shutdown -s -t 0.

Send the message “CAP” to call the computer camera to take the current user, and then send the picture to wechat.

Of course, CMD naming can also do more interesting things, you can go to the Internet search.

Have you learned how to operate this wave? Go get your best friend and try it on!



Recommended reading:

Special program for the one you love the most

Distributed crawler is the key to user information crawling

Shenzhen data analysis post



This article was first published on the public account [TWcoding]. For those who are interested in Python, crawlers, data analysis and algorithms, please add the wechat public account [TWcoding] and let’s play Python together. Python 2018 is the latest Python resource.



If it works for you.Please,star.



God helps those who help themselves