python——模拟服务器发送邮件

python发送邮件脚本记录

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def Mailer(to_list, th1=None, Subject=None, unipath=None):
    mail_host = 'smtp.qq.com'  # 邮箱服务器
    mail_user = ' ......'  # 发件人邮箱密码(当时申请smtp给的口令)
    mail_pwd = '......'  # SMTP密码
    s = smtplib.SMTP_SSL(mail_host, 465, timeout=5)
    s.login(mail_user, mail_pwd)
    mail = str(th1)
    msg = MIMEMultipart()
    msgtext = MIMEText(mail.encode('utf8'), _subtype='html', _charset='utf8')
    msg['From'] =u'测试机 <%s>'%mail_user
    msg['Subject'] = Subject
    msg['To'] = ",".join(to_list)
    if unipath is not None:
        att1 = MIMEText(open(unipath, 'rb').read(), 'base64', 'gb2312')
        att1["Content-Type"] = 'application/octet-stream'
        att1.add_header('Content-Disposition', 'attachment', filename='LoginServerInfo.xls')
        msg.attach(att1)
    msg.attach(msgtext)
    try:
        s.sendmail(mail_user, to_list, msg.as_string())
        s.close()
        print('发送成功')
    except Exception as e:
        print(e)
to_list = [
    'zhangxuyang_m@cyou-inc.com',
]
Mailer(to_list, th1="LoginServerInfo.txt", Subject='【配置文件】', unipath=r'/opt/xuyang/shujuchuli/infodir/newdate.xls')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们开始Python模拟登录imooc.com的练习。 首先,我们需要导入需要的库,包括requests和BeautifulSoup。requests库用于发送http请求,BeautifulSoup库用于解析html文本。 ```python import requests from bs4 import BeautifulSoup ``` 接下来,我们需要获取登录页面的html代码。我们可以使用requests库的get()方法来发送一个get请求,并将返回的html代码存储在一个变量中。 ```python login_url = 'https://www.imooc.com/' login_page = requests.get(login_url) login_soup = BeautifulSoup(login_page.text, 'html.parser') ``` 接下来,我们需要从登录页面中获取登录所需的信息,包括表单的action、method、以及需要提交的表单数据。我们可以使用BeautifulSoup库来解析html代码,并使用find()方法来查找表单元素。 ```python # 获取登录表单元素 login_form = login_soup.find('form', attrs={'id': 'signup-form'}) # 获取表单的action和method action_url = login_form['action'] method = login_form['method'] # 获取需要提交的表单数据 email = input('请输入邮箱:') password = input('请输入密码:') data = { 'email': email, 'password': password } ``` 接下来,我们可以使用requests库的post()方法来发送一个post请求,将表单数据提交到服务器。需要注意的是,我们需要将表单数据和请求头一起提交到服务器。 ```python # 构造请求头 headers = { 'Referer': login_url, 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} # 发送post请求 response = requests.post(action_url, data=data, headers=headers) ``` 最后,我们可以判断登录是否成功,可以通过检查返回的html代码中是否包含登录成功的提示信息来判断。 ```python # 判断登录是否成功 if '个人中心' in response.text: print('登录成功!') else: print('登录失败!') ``` 完整代码如下: ```python import requests from bs4 import BeautifulSoup # 获取登录页面html代码 login_url = 'https://www.imooc.com/' login_page = requests.get(login_url) login_soup = BeautifulSoup(login_page.text, 'html.parser') # 获取登录表单元素 login_form = login_soup.find('form', attrs={'id': 'signup-form'}) # 获取表单的action和method action_url = login_form['action'] method = login_form['method'] # 获取需要提交的表单数据 email = input('请输入邮箱:') password = input('请输入密码:') data = { 'email': email, 'password': password } # 构造请求头 headers = { 'Referer': login_url, 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} # 发送post请求 response = requests.post(action_url, data=data, headers=headers) # 判断登录是否成功 if '个人中心' in response.text: print('登录成功!') else: print('登录失败!') ``` 注意,此代码仅供学习参考。在实际应用中,请勿使用模拟登录的方式获取个人信息,以免造成不必要的风险。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值