python实现微信控制电脑

可用微信中的文件传输助手控制cmd命令,如电脑关机,查看当前使用电脑的用户的照片等。
python实现微信控制电脑_第1张图片

程序代码:

import itchat
import os
import time
import cv2
sendMsg = u"{消息助手}:暂时无法回复"
usageMsg = u"使用方法:\n1.运行CMD命令:cmd xxx (xxx为命令)\n" \
           u"-例如关机命令:\ncmd shutdown -s -t 0 \n" \
           u"2.获取当前电脑用户:cap\n3.启用消息助手(默认关闭):ast\n" \
           u"4.关闭消息助手:astc"
flag = 0  #消息助手开关
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("消息助手已开启", "filehelper")
        if message == "astc":
            flag = 0
            itchat.send("消息助手已关闭", "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()

为方便使用,可将程序打包成exe文件。
安装pyinstaller pip installer pyinstaller
在cmd下,进入到程序所在目录,执行命令pyinstaller 文件名.py -F

你可能感兴趣的:(python)