def changeAk(url): global ak global akQueue if akQueue.qsize()>0: oldAK = ak ak = akQueue.get() print('ak还剩下:'+str(akQueue.qsize())) #更新当前url url = url.replace(oldAK,ak) return url else :#已经没有ak可以用了,保存状态 #保存状态 SMTP.send('ak用完') os._exit(0)
def changeAk(url, failTimes): global ak global akQueue if failTimes == 0: if akQueue.qsize() > 0: if akQueue.qsize() % 10 == 0: SMTP.send('更换ak') oldAK = ak ak = akQueue.get() #更新当前url url = url.replace(oldAK, ak) return url else: #已经没有ak可以用了,保存状态 #保存状态 SMTP.send('ak用完') os._exit(0) else: #返回原来的url return url
def main(): try: getFacilityNum() #数据清空 clearFile() print('抓取完成') #getProcedure() except KeyboardInterrupt: print("ctr+c中断并且保存状态和数据") saveProcejure() except requests.exceptions.ConnectionError as e: saveProcejure() print(e) print('保存状态,重启') main() except BaseException as e: print('出现未知错误') print(e) saveInLog(str(e)) SMTP.send('程序被终止了') saveProcejure()
def saveProcejure(): global maxLat, minLat, maxLng, minLng global cityList global oneCity global FaN global FList global lat #当前纬度 global lng #当前经度 global posResults #当前结果集 #把当前城市插回去 cityList.append(oneCity) #把当前设施插回去 FList.append(FaN) saveInFile(','.join(cityList), 'city') saveInFile(','.join(FList), 'facility') #保存当前已遍历的点的状态 str1 = str(maxLat) + ',' + str(minLat) + ',' + str(maxLng) + ',' + str( minLng) + '|' + str(lat) + ',' + str(lng) saveInFile(str1, 'procejure') #把现在的数据存储起来 saveInCsv(posResults, FaN) #进行邮件的提醒 content = getContent() SMTP.send(content)
def changeAk(posResults, url): global lat #当前纬度 global lng #当前经度 global failTimes global ak global FaN global akQueue if failTimes == 0: if akQueue.qsize() > 0: if akQueue.qsize() % 10 == 0: SMTP.send('更换ak,剩余:' + str(akQueue.qsize())) oldAK = ak ak = akQueue.get() failTimes = 30 #更新当前url url = url.replace(oldAK, ak) return url else: #已经没有ak可以用了,保存状态 #保存状态 SMTP.send('ak用完') saveProcejure() os._exit(0) else: #返回原来的url return url
def sendmail(msgstr, envrecip, envsender): """Send e-mail via direct SMTP, or by opening a pipe to the sendmail program. msgstr is an rfc2822 message as a string. envrecip is the envelope recipient address. envsender is the envelope sender address. """ import Defaults # Sending mail with a null envelope sender address <> is not done # the same way across the different supported MTAs, nor across the # two mail transports (SMTP and /usr/sbin/sendmail). # # The most common method is to use the string '<>'. There are two # exceptions where an empty string must be used instead. # # 1) When running qmail/courier and using the sendmail transport. # qmail munges the envelope sender address into <"<>"@domain.dom # if "sendmail -f <>" is used. # # 2) When running Postfix and using the sendmail transport. # Old versions of Postfix apparently also have trouble with # "sendmail -f <>", though Postfix 2.0.x does not. if envsender == '': envsender = '<>' if envsender == '<>' and \ Defaults.MAIL_TRANSFER_AGENT in ('postfix', 'qmail') and \ Defaults.MAIL_TRANSPORT == 'sendmail': envsender = '' if Defaults.MAIL_TRANSPORT == 'sendmail': # You can avoid the shell by passing a tuple of arguments as # the command instead of a string. This will cause the # subprocess.Popen() code to execvp() "/usr/bin/sendmail" with # these arguments exactly, with no trip through any shell. cmd = (Defaults.SENDMAIL_PROGRAM, '-i', '-f', envsender, '--', envrecip) runcmd_checked(cmd, msgstr) elif Defaults.MAIL_TRANSPORT == 'smtp': import SMTP server = SMTP.Connection() server.sendmail(envsender, envrecip, msgstr) server.quit() else: raise Errors.ConfigError, \ "Invalid MAIL_TRANSPORT method: " + Defaults.MAIL_TRANSPORT
def main(): # 请自行修改下面的邮件发送者和接收者 sender = '*****@*****.**' receivers = ['*****@*****.**', '*****@*****.**'] message = MIMEText('用Python发送邮件的示例代码.', 'plain', 'utf-8') message['From'] = Header('邵朋', 'utf-8') #发件人 message['To'] = Header('鹏少', 'utf-8') #收件人 message['Subject'] = Header('示例代码试验邮件', 'utf-8') #邮件内容 smtper = SMTP('smtp.163.com') #使用smtp邮件协议 # 请自行修改下面的登录口令 smtper.login(sender, 'shao1990PENG') #邮箱登录密码 smtper.sendmail(sender, receivers, message.as_string()) print('邮件发送完成!')
def main(): # 创建一个带附件的邮件消息对象 message = MIMEMultipart() # 创建文本内容 text_content = MIMEText('附件中有本月数据请查收', 'plain', 'utf-8') message['Subject'] = Header('本月数据', 'utf-8') # 将文本内容添加到邮件消息对象中 message.attach(text_content) # 读取文件并将文件作为附件添加到邮件消息对象中 # python读取windows系统文件要么写全路径,要么绝对路径要么相对路径 with open('/Uscers/Administrator/Desktop/hello', 'rb') as f: txt = MIMEText(f.read(), 'base64', 'utf-8') txt['Content-Type'] = 'text/plain' txt['Content-Disposition'] = 'attachment; filename=hello.txt' message.attach(txt) # 读取文件并将文件作为附件添加到邮件消息对象中 with open('/Usecrs/Administrator/Desktop/媒体版故障处理.docx', 'rb') as f: xls = MIMEText(f.read(), 'base64', 'utf-8') xls['Content-Type'] = 'application/vnd.ms-doc' # 或 application/vnd.ms-excel xls['Content-Disposition'] = 'attachment; filename=month-data.doc' #重命名为month-data.xlsx message.attach(xls) # 创建SMTP对象 smtper = SMTP('smtp.163.com') # 开启安全连接 # smtper.starttls() sender = '*****@*****.**' receivers = ['*****@*****.**'] # 登录到SMTP服务器 # 请注意此处不是使用密码而是邮件客户端授权码进行登录 # 对此有疑问的读者可以联系自己使用的邮件服务器客服 smtper.login(sender, 'shao1990PENG') # 发送邮件 smtper.sendmail(sender, receivers, message.as_string()) # 与邮件服务器断开连接 smtper.quit() print('发送完成!')
def main(): calendar_url = 'http://www.altamontschool.org/calendars/index.aspx?ModuleID=52:53:93' global calendar_data calendar_data = scrape.get_calendar_days( urllib.request.urlopen(calendar_url)) print("Calendar Scrape Finished") IMAP_conn = IMAP.IMAP_Connection() print("IMAP Connection started!") SMTP_conn = SMTP.SMTP_Connection() print("SMTP Connection started!") # note Main Loop while True: msgs_raw = IMAP_conn.wait_for_message( ) # SMTP_conn is passed as an arg so that it gets noop()'d when IMAP_conn idles. if type(msgs_raw) == str: msgs_raw = [msgs_raw] for msg_raw in msgs_raw: msg = TextMessage(msg_raw) print('Message Received!\n' + msg.text) reply = msg.build_reply() SMTP_conn.send_smtp(msg.sender, reply)
#!python 3 # simple experiment of sending a mail with time info import smtplib, datetime from retrievejson import exchg_rate import SMTP MAIL_ADDR = '*****@*****.**' pw = input('Enter your password:'******'The current time is '+ datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S') # TEXT = 'May the Force be with you. Sincerely, Jon from the past' # msg = mail_msg(SUBJECT, TEXT) rate = exchg_rate('USD', 'JPY')[2] msg = SMTP.mail_msg('Exchange Rate', 'The exchange rate for USD/JPY is ' + str(rate)) print('Sending Exchange Rate....') smtpObj.sendmail('*****@*****.**', '*****@*****.**', msg) smtpObj.quit()
import SMTP if __name__ == "__main__": SMTP.main()