博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python牛刀小试(四)--代码解析(邮件发送功能)
阅读量:7040 次
发布时间:2019-06-28

本文共 4179 字,大约阅读时间需要 13 分钟。

1.工具类python

点击(此处)折叠或打开

  1. # -*-coding=utf-8-*-
  2. __author__ = 'zhangshengdong'
  3. '''
  4. 个人技术博客:http://blog.chinaunix.net/uid/26446098.html
  5. 联系方式: sdzhang@cashq.ac.cn
  6. '''
  7. class Toolkit():                          #工具类,后面会调用,用于分割例3的配置文件。
  8.     @staticmethod
  9.     def getUserData(cfg_file):
  10.         f=open(cfg_file,'r')
  11.         account={
    }
  12.         for i in f.readlines():
  13.             ctype,passwd=i.split('=')
  14.             #print ctype
  15.             #print passwd
  16.             account[ctype.strip()]=passwd.strip()
  17.         return account
2.价格比较策略代码

点击(此处)折叠或打开

  1. #-*-coding=utf-8-*-
  2. __author__ = 'zhangshengdong'
  3. '''
  4. 个人技术博客:http://blog.chinaunix.net/uid/26446098.html
  5. 联系方式: sdzhang@cashq.ac.cn
  6. '''
  7. import os
  8. import sys
  9. import smtplib
  10. import tushare as ts
  11. from Ztoolkit import Toolkit as TK
  12. from email.mime.text import MIMEText
  13. from email import Encoders, Utils
  14. reload(sys)
  15. sys.setdefaultencoding('utf8')
  16. class MailSend():
  17.     def __init__(self, smtp_server, from_mail, password, to_mail):   #邮箱初始化配置的函数
  18.         self.server = smtp_server
  19.         self.username = from_mail.split("@")[0]
  20.         self.from_mail = from_mail
  21.         self.password = password
  22.         self.to_mail = to_mail
  23.     def send_txt(self, name, real_price,price, percent, status):    #信息内容排版的函数
  24.         if 'up' == status:
  25. # content = 'stock name:%s current price: %.2f higher than price up:%.2f , ratio:%.2f' % (name, real_price,price, percent)
  26.              content = '证券名称:%s 当前价格: %.2f 高于 定价模型上限价格:%.2f , 可卖出 预计盈利率:%.2f' %(name, real_price,price, percent+15)
  27.         if 'down' == status:
  28. # content = 'stock name:%s current price: %.2f lower than price down:%.2f , 盈利率:%.2f' % (name, real_price,price, percent)
  29.              content = '证券名称:%s 当前价格: %.2f 低于 定价模型下限价格:%.2f , 可买入 预计盈利率:%.2f' %(name, real_price,price, percent+15)
  30.         content = content + '%'
  31.         print content
  32.         subject = '%s' % name
  33.         self.msg = MIMEText(content, 'plain', 'utf-8')
  34.         self.msg['to'] = self.to_mail
  35.         self.msg['from'] = self.from_mail
  36.         self.msg['Subject'] = subject
  37.         self.msg['Date'] = Utils.formatdate(localtime=1)
  38.         try:
  39.             self.smtp = smtplib.SMTP_SSL(port=465)
  40.             self.smtp.connect(self.server)
  41.             self.smtp.login(self.username, self.password)
  42.             self.smtp.sendmail(self.msg['from'], self.msg['to'], self.msg.as_string())
  43.             self.smtp.quit()
  44.             print "sent"
  45.         except smtplib.SMTPException, e:
  46.             print e
  47.             return 0
  48. def push_msg(name, real_price,price, percent, status):            #信息推送的函数
  49.     cfg = TK.getUserData('data.cfg')
  50.     from_mail = cfg['from_mail']
  51.     password = cfg['password']
  52.     to_mail = cfg['to_mail']
  53.     obj = MailSend('smtp.qq.com', from_mail, password, to_mail)   ##这里调用了上述MailSend类
  54.     obj.send_txt(name,real_price, price, percent, status)
  55. def read_stock(name):
  56.     f = open(name)
  57.     stock_list = []
  58.     for s in f.readlines():
  59.         s = s.strip()
  60.         row = s.split(';')
  61.          #print row
  62.         print "code :",row[0]
  63.         print "price_down :",row[1]
  64.         print "price_up :",row[2]
  65.         stock_list.append(row)
  66.     return stock_list
  67. def compare_price(code, price_down, price_up):
  68.     try:
  69.         df = ts.get_realtime_quotes(code)
  70.     except Exception, e:
  71.         print e
  72.         time.sleep(5)
  73.         return 0
  74.     real_price = df['price'].values[0]
  75.     name = df['name'].values[0]
  76.     print "stock name :",name
  77.     real_price = float(real_price)
  78.     print "current price :",real_price
  79.     pre_close = float(df['pre_close'].values[0])
  80.     print "close price :",pre_close
  81.     if real_price >= price_up:
  82.         percent_up = (real_price - price_up) / price_up * 100
  83.         print 'percent : %.2f\n' % (percent_up),
  84.         print '%s real_price %.2f higher than price_up %.2f , %.2f' % (name, real_price, price_up,percent_up),
  85.         print '%'
  86.         push_msg(name, real_price,price_up, percent_up, 'up')
  87.         return 1
  88.     if real_price = price_down:
  89.         percent_down = (price_down - real_price) / price_down * 100
  90.         print '%s real_price %.2f lower than price_down %.2f , %.2f' % (name, real_price, price_down,percent_down),
  91.         print '%'
  92.         push_msg(name, real_price,price_down, percent_down, 'down')
  93.         return 1
  94. def main():
  95.     #read_stock('price.txt')
  96.     # choice = input("Input your choice:\n")
  97.     # if str(choice) == '1':
  98.         stock_lists_price = read_stock('price.txt')
  99.         while 1:
  100.             t = 0
  101.             for each_stock in stock_lists_price:
  102.                 code = each_stock[0]
  103.                 price_down = float(each_stock[1])
  104.                 price_up = float(each_stock[2])
  105.                 t = compare_price(code, price_down, price_up)
  106.                 if t:
  107.                     stock_lists_price.remove(each_stock)
  108. if __name__ == '__main__':
  109.     path=os.path.join(os.getcwd(),'data')
  110.     if os.path.exists(path)==False:
  111.          os.mkdir(path)
  112.     os.chdir(path)
  113.     main()
3.邮箱配置文件

点击(此处)折叠或打开

  1. from_mail=xxxxxxx@qq.com
  2. password=xxxxxxxx
  3. to_mail=xxxxxxx@qq.com

转载地址:http://kcaal.baihongyu.com/

你可能感兴趣的文章
Throws与Throw
查看>>
php趣味编程 - php求黑色星期五
查看>>
zabbix安装
查看>>
ELK之权限管理
查看>>
×_7_12_2013 I: Light on or off
查看>>
JIT
查看>>
巧用escalations限制Nagios报警次数 - [Nagios
查看>>
Entity SQL与LINQ TO Entity的本质区别
查看>>
python unittest 深入failfast及实际应用【示例】
查看>>
MSSQL中文排序规则设置
查看>>
30 个有关 Python 的小技巧
查看>>
CDN下nginx获取用户真实IP地址
查看>>
Jsp技术总结
查看>>
Sakai 11.x Build Failure
查看>>
面向对象+模块化设计绘制canvas星空动画
查看>>
Elastic Search学习笔记3——集群配置
查看>>
Unity客户端资源智能管理
查看>>
SVN在Windows下的安装配置步骤
查看>>
网页两侧悬浮广告js代码
查看>>
算法练习:判断一个字符串中的字符是否唯一(只用基本数据结构)
查看>>