是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句linux
import导入模块执行的操做shell
方法json
import itchat #导入itchat模块 itchat.auto_login() #自动登录 itchat.send('hello',toUserName='filehelper') #给微信助手发送'hello' #itchat.send_file('/etc/passwd',toUserName='filehelper') friends = itchat.get_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)
import qrcode img=qrcode.make('此后,是平庸是惊世是绚丽是落魄,祝福你') img.save('happy.png')
首先,咱们须要在图灵机器人官网上注册一个机器人,能够选择不一样用途的机器人
获取到apikeyapi
import random import requests import itchat import time def get_tuling_response(_info): #图灵机器人聊天函数 print(_info) # 图灵机器人的网址 api_url = "http://www.tuling123.com/openapi/api" data = { 'key': '49f783cdeef84fc2bec444339f7bXXXX', #这里使用申请好的机器人api,笔者把本身的api后四位隐藏了 'info': _info, 'userid':'wechat-robot' } # 发送数据到执行网址 res = requests.post(api_url, data).json() # print(res, type(res)) # 给用户返回数据 print(res['text']) return res['text'] @itchat.msg_register(itchat.content.TEXT,isGroupChat=True) def text_reply(msg): #获取好友发送的消息 content = msg['Content'] #将好友消息发送给机器人,处理结果返回给好友 returnContent = get_tuling_response(content) #time.sleep(random.randint(2)) return returnContent if __name__ =='__main__': itchat.auto_login(hotReload=True) itchat.run()
#os模块 import os import time import itchat import random import requests #网络请求处理库 #兼容性 #系统目录间的分隔符 #linux : /var/log/messages #win:C:\\Progjct\hello.py print(os.path.sep) #显示路径分隔符 #在linux里面,执行shell命令 # 1.第一种方式,能够判断命令是否执行成功 #返回值为0,执行成功 #不然,执行失败 res =os.system('hostname') print('res:',res) # 第二种方法:用来保存命令的执行结果 res = os.popen('hostname') print('res:',res.read()) @itchat.msg_register(itchat.content.TEXT) def text_reply(msg): #获取文件助手发来的消息,执行发送内容 # 1.执行成功,显示执行成功:执行结果 # 2.反之,显示执行失败 print(msg) if msg['ToUserName']=='filehelper': #若是是文件传输助手法来消息,执行代码 command = msg['Content'] if os.system(command) ==0: res =os.popen(command).read() #os.popen() 方法用于从一个命令打开一个管道,command -- 使用的命令。 result = "命令执行成功,执行结果:" +res itchat.send(result,'filehelper') else: result = "命令执行失败" itchat.send(result,'filehelper') #shutdown -h 1 #一秒后执行关机命令 return 'hello' if __name__ =='__main__': itchat.auto_login(hotReload=True) itchat.run()