说明:
python 程序,基于 itchat 实现微信控制电脑。你可以通过在微信发送命令,来拍摄当前电脑的使用者,然后图片会发送到你的微信上。甚至你可以发送命令来远程关闭电脑。
程序思路:
此次程序使用的环境是 python3.6 + windows10,在运行程序之前请先确保你已经安装好了 opencv-python 和 matplotlib。通过 pip install 即可安装。
程序主要是通过使用 itchat 库来登录到微信网页端,然后通过 itchat 来发送消息和接收消息。并通过 opencv 来调用电脑的摄像头,把当前使用电脑的用户拍照下来,发送到你的微信上。至于远程关机是通过调用 os 库,发送 cmd 命名即可实现。
程序源码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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()
程序解析及说明:
定义了一些发送的消息,然后通过调用 itchat 和 cv2 相关库的操作,即可实现。关于 itchat 库的一些操作,可以去网上找相关的文档。