Python中的第三方模块
亲亲,需要联网才可以实现以下功能哦~~
前提:
首先需要手动导入itchat包,依次点击以下信息:Settings->Project:westos->Project Interpreter->加号->itchat->Install Package
举例一:
当扫码后输出微信好友的名称以及微信好友中男女数量统计
import random
import time
import itchat
itchat.auto_login()
friends = itchat.get_friends()
print(friends)
info = {}
for friend in friends[1:]:
if friend['Sex'] == 1:
info['male'] = info.get('male',0) + 1
elif friend['Sex'] == 2:
info['female'] = info.get('female',0) + 1
else:
info['other'] = info.get('other',0) + 1
print(info)
生成的二维码如下:
当用手机微信成功登陆后,会出现以下结果,显示微信好友的昵称以及男女数量统计
举例二:
当微信扫码后,会在手机助手中一直输出’hello’
import random
import time
import itchat
itchat.auto_login()
while True:
itchat.send('hello',toUserName='filehelper')
time.sleep(random.randint(1, 3))
生成的二维码如下:
手机中收到的消息如下:
举例三:
当微信扫码后,会一直给手机助手发送文件/etc/passwd
import random
import time
import itchat
itchat.auto_login()
while True:
itchat.send_file('/etc/passwd', toUserName='filehelper')
time.sleep(random.randint(1, 3)) ##1到3s随机休眠
生成的二维码如下:
手机中收到的消息如下:
举例四:
当微信扫码后,可在手机助手中发送命令运行电脑中的文件
import itchat
import os
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
if msg['ToUserName'] == 'filehelper':
#获取要执行命令的内容
command = msg['Content']
print(command) #让电脑执行命令代码 #如果执行成功,返回值为0
if os.system(command) == 0:
res = os.popen(command).read()
result = '【返回值】 - 命令执行成功,执行结果:\n' + res
itchat.send(result,'filehelper')
#命令执行失败,请重新输入命令
else:
result = '【返回值】 - 命令%s执行失败,请重试' %command
itchat.send(result,'filehelper')
itchat.auto_login()
itchat.run()
手机中发送的命令如下;
代码及结果如下:
手机中发送的指令如果错误:
如果输入的命令不正确时,输出以下结果: