python自動傳送結果到企業微信

2021-09-11 16:10:08 字數 2332 閱讀 1302

直接上**:

首先在當前目錄將乙個:config.conf 檔案,填寫如下資訊。具體配置請參考:

[mass]

corpid = '***x'

corpsecret = '***x'

agentid = '***x'

touser = '***x' # 接收者使用者名稱

2、新建wechat.py,輸入如下**
#!/usr/bin/env python

# encoding: utf-8

# time : 2/28/2019 4:19 pm

# author : luzaofa

import time

import requests

import json

import configparser

class config(object):

'''解析配置檔案'''

def get_config(self, lable, value):

cf = configparser.configparser()

cf.read("config.conf")

config_value = cf.get(lable, value)

return config_value

class wechat(config):

def __init__(self):

'''初始化配置'''

super(config, self).__init__()

self.corpid = self.get_config("mass", "corpid")

self.corpsecret = self.get_config("mass", "corpsecret")

self.agentid = self.get_config("mass", "agentid")

self.touser = self.get_config("mass", "touser")

def _get_access_token(self):

'''發起請求'''

url = ''

values =

req = requests.post(url, params=values)

data = json.loads(req.text)

print data

return data["access_token"]

def get_access_token(self):

'''獲取token,儲存到本地'''

try:

with open('access_token.conf', 'r') as f:

t, access_token = f.read().split()

except exception:

with open('access_token.conf', 'w') as f:

access_token = self._get_access_token()

cur_time = time.time()

f.write('\t'.join([str(cur_time), access_token]))

return access_token

else:

cur_time = time.time()

if 0 < cur_time - float(t) < 7260:

return access_token

else:

with open('access_token.conf', 'w') as f:

access_token = self._get_access_token()

f.write('\t'.join([str(cur_time), access_token]))

return access_token

def send_data(self, message):

'''傳送資訊'''

msg = message.encode('utf-8')

send_url = '' + self.get_access_token()

send_data = '}' % (

self.agentid, self.touser, msg)

r = requests.post(send_url, send_data)

print r.content

return r.content

if __name__ == '__main__':

wx = wechat()

wx.send_data("test")

python 自動傳送郵件

測試環境 python3.4 發件伺服器 網易yeah 收件伺服器 移動139郵箱 注意 一定要在發件的郵箱裡面設定pop3 smtp imap為開啟狀態,要不然會報錯 535 error authentication failed 通過本程式,可以自動給自己的手機郵箱傳送郵件,在手機郵箱中把發件位...

Python自動傳送郵件

本文主要介紹如何使用python實現郵件的自動傳送。首先這個功能要求使用的模組是stmplib,email。smtp message transport protocol 簡單資訊傳輸協議,只能由於傳送郵件。下面是簡要的程式 usr bin python coding utf 8 import sm...

Python 自動傳送郵件

可直接複製進行呼叫 修改main裡對應引數即可 usr bin env python coding utf 8 time 2022 2 11 11 05 author file myemail.py version 1.0 function import datetime import smtpli...