itchat是一个通过基于python3,封装了登录网页版微信并收发消息相关的接口,使用python中的itchat模块,可以快速获取好友信息等。
import itchat #第三方模块 需要网络下载
import time
itchat.auto_login() ##自动登录,会生成二维码
while True:
##给微信的手机助手发信息
chat.send(‘hello’,toUserName=‘filehelper’)
itchat.send_file(’/etc/passwd’,toUserName=‘filehelper’)
time.sleep(1)
该程序运行之后,会出现二维码进行扫描登录
安卓系统的手机无法进行操作,苹果系统应该可以
以下为安卓手机扫码后显示的报错
import itchat
itchat.auto_login(hotReload=True,enableCmdQR=True
##hotReload是自动重连,无需多次扫码,为可选项;enableCmdQR默认为False,当为True时则不会弹出图片,只在终端生成二维码,为可选项
itchat.logout() ##退出
chat.send('消息内容',toUserName='好友名称') ##给好友发送消息
itchat.send_file('文件名称',toUserName='好友名称') ##给好友发送文件
import itchat
import time
itchat.auto_login()
friends = itchat.get_friends() ##获取微信中的好友
#print(friends)
info = {}
for friends in friends[1:]: ##获取性别
if friends['Sex'] == 1:
info['male'] = info.get('male',0) + 1
elif friends['Sex'] == 2:
info['female'] = info.get('female',0) + 1
else:
info['other'] = info.get('other',0) + 1
print(info)
import 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()
import os
在python中执行shell命令
判断命令是否执行成功
返回值是0 执行成功
返回值不为0 执行不成功
print(os.system('ls'))
print(os.system('hostnamessssss'))
# 保存命令执行的结果
res = os.popen('hostname').read()
print(res)