python中的第三方itchat模块

1. itchat简介

  • itchat是一个开源的微信号个人接口(API应用),可以使用不到30行代码,就可以完成一个处理所有信息的微信机器人
  • apl:Application Pragrammig Interface 即:应用程序编程接口 API就是操作系统给应用程序的调用接口,应用程序通过调用操作系统的Api而使得操作系统去执行应用程序的命令。在windows中,系统API是以调用函数的方式提供的

2. itchat的基本使用及实现的基本功能

#itchat第三方模块的导入 (需要注意的是:联网)
import itchat
import time
#登录
itchat.auto_login()

#实现功能1:给微信助手发信息(toUserName 手机文件助手)
while True:
    #给微信助手发消息
    itchat.send('hello',toUserName='filehelper')
    #给微信助手发文件
    itchat.send_file('/etc/passwd',toUserName='filehelper')
    time.sleep(1)

#实现功能2:统计微信好友信息

firends=itchat.get_friends()  #获得好友信息
print(firends)

info={}
#统计男女比例
for friends in friends[1:]: #获取的好友信息是从第二个元素开始的
    if friends['sex']==1:  #1代表女2代表男
        info['male']=info.get('male',0)+1   #先get女性人数,找到返回女性人数,再加1;如果没有找到返回0 再加1
    elif friends['sex']==2:
        info['female']=info.get('female',0)+1
    else:
        info['other']=info.get('other',0)+1  #对没有性别人数统计
print(info)

注:执行程序出现该Error由于存在安全设定,出ios系统或取消安全设定外的客户机都还不能登录成功

Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Your wechat account may be LIMITED to log in WEB wechat, error info:
<error><ret>1203</ret><message>为了你的帐号安全,此微信号不能登录网页微信。你可以使用Windows微信或Mac微信在电脑端登录。Windows微信下载地址:https://pc.weixin.qq.com  Mac微信下载地址:https://mac.weixin.qq.com</message></error>

登录成功提醒如下:

在这里插入图片描述

3.itchat实现用户通过微信助手执行os中shell命令,并且系统将执行结果返回给微信助手

# 实现:在文件助手输入命令,并将执行结果返回给微信助手
# 命令返回值为0 执行不成功
# 命令返回值为1 执行成功

import os  #导入os系统模块
import itchat

@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)  ##需要使用装饰器
def text_reply(msg):
    if msg['ToUserName'] == 'filehelper':
        # 获取要执行的命令的内容
        command = msg['Content']
        # 让电脑执行命令代码
        # 如果执行成功,返回值为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()  ## 执行语句

print(os.system('cat /etc/passwd') )
print(os.system('ls'))
print(os.system('hostnamessssss'))

os.popen保存命令执行的结果

res = os.popen('hostname').read()
print(res)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值