Python *args **kargs 传参
Python *args
*args:(表示的就是将实参中按照位置传值,多出来的值都给args,且以元组的方式呈现)
def foo(x,*args):
print(x);
print()
print(args);
foo(1,2,3,4,5)
Python **kargs
**kwargs:(表示的就是
def foo(c,b,d,a=1):
print(a);
print(b);
print(c);
print(d)
foo(**{'c':3,'b':2,'d':4})
形参中按照关键字传值把多余的传值以字典的方式呈现)
钉钉消息自动化
import requests;
import json
# 钉钉消息发送
def SendDingTalkMsg(content,token,atMobiles,isAtAll=False):
url=f"https://oapi.dingtalk.com/robot/send?access_token={token}"
HEADERS={"Content-Type":"application/json;charset=utf-8"}
String_textMsg={"msgtype":"text",
"text":{
"content":content
},
"at": {
"atMobiles": atMobiles,
"isAtAll": isAtAll
}
}
String_textMsg=json.dumps(String_textMsg)
res=requests.post(url,data=String_textMsg,headers=HEADERS)
return(res.text)
content = 'test:凭一个男人的直觉,开心就好';
token = 'XXXXXX';
atMobiles = ['xxxx','xxxx']
l = [content,token,atMobiles]
res_text = SendDingTalkMsg(*l,isAtAll=True)
if eval(res_text)['errmsg'] == 'ok':
print('钉钉消息发送成功');
else:
print('钉钉消息发送失败');
邮件自动化发送 -- 企业邮箱
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# 发送邮件到管理员邮箱(添加附件)
def mail_sender(my_sender, my_pass, my_user, mail_content, mail_title, path):
ret = True
try:
# 文本信息
textmsg = MIMEMultipart()
textmsg['From'] = formataddr(["BI管理员", my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
textmsg['To'] = ",".join(my_user)
textmsg['Subject'] = mail_title # 邮件的主题,也可以说是标题
content = MIMEText(mail_content, 'html', 'utf-8')
textmsg.attach(content)
# 添加附件
filename = os.path.split(path)[1];
att1 = MIMEApplication(open(path, 'rb').read())
att1.add_header('Content-Disposition', 'attachment', filename=filename)
# 附件添加
textmsg.attach(att1);
server = smtplib.SMTP_SSL("smtp.qiye.aliyun.com", 465) # 发件人邮箱中的SMTP服务器,端口是25
server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
if isinstance(my_user, list):
server.sendmail(my_sender, my_user, textmsg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
else:
server.sendmail(my_sender, [my_user, ], textmsg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
except Exception: # 如果 try 中的语句没有执行,则会执行下面的 ret=False
ret = False
return ret
####
mail_info = {
'my_sender':'xxxx',
'my_pass':'xxx',
'my_user':[sq_mail],
'mail_content':'test:凭一个男人的直觉,开心就好',
'mail_title':'test',
'path':path
}
ret = mail_sender(**mail_info);
if ret:
print("邮件发送成功");
else:
print("邮件发送失败");