#! /usr/bin/python2 # coding=utf-8 import os import threading import urllib2 import time from Tkinter import * #from multiprocessing import Process from multiprocessing import Process, Value, Array, Manager val_price = 20 val_percent = 5 / 100 def func(): os._exit(0) def creatfram(name): root = Tk() w = Label(root, text=name) w.pack() root.geometry('150x20+0+0') root.resizable(width=False, height=False) root.mainloop() #print sys.getdefaultencoding() reload(sys) sys.setdefaultencoding('utf-8') #print sys.getdefaultencoding() def get_price(code): url = 'http://hq.sinajs.cn/?list=%s' % code #print url req = urllib2.Request(url) #print req # 如果不需要设置代理,下面的set_proxy就不用调用了。由于公司网络要代理才能连接外网,所以这里有set_proxy… # req.set_proxy('proxy.XXX.com:911', 'http') content = urllib2.urlopen(req).read() str = content.decode('gbk', 'ignore') data = str.split('"')[1].split(',') name = "%-6s" % data[0] price_current = "%-6s" % float(data[3]) change_percent = (float(data[3]) - float(data[2])) * 100 / float(data[2]) change_percent = "%-6s" % round(change_percent, 2) #print("股票名称:{0} 涨跌幅:{1} 最新价:{2}".format(name, change_percent, price_current)) return change_percent, price_current def p_stock(code_list): """ A doubling function that can be used by a process """ for code in code_list: percent, price = get_price(code['code']) #print code, price name = code['code'] + " " + percent + " " + price flg_show = 0 #print code['code'], float(price), float(percent), " alarm ",code['price'], code['percent'] if (((float(price) > code['price'] and 0 != code['price']) ) and (1 == code['flg_price'])): flg_show = 1 code['flg_price'] = 0 if (((float(percent) > code['percent'] and 0 != code['percent'])) and (1 == code['flg_percent'])): flg_show = 1 code['flg_percent'] = 0 if 1 == flg_show: timer = threading.Timer(5, func) timer.start() creatfram(name) if __name__ == '__main__': mgr = Manager() A = Manager().list() A.append(mgr.dict(code='sz300127', price=20, percent=5, flg_price=1, flg_percent=1)) A.append(mgr.dict(code='sz000639', price=20, percent=5, flg_price=1, flg_percent=1)) A.append(mgr.dict(code='sh600718', price=0, percent=5, flg_price=1, flg_percent=1)) A.append(mgr.dict(code='sh600452', price=0, percent=5, flg_price=1, flg_percent=1)) A.append(mgr.dict(code='sh600489', price=0, percent=0, flg_price=1, flg_percent=1)) #print A[0]['flg_price'], A[0]['flg_percent'] flg_time = 0 old = 0 while True: proc = Process(target=p_stock, args=(A, )) proc.start() proc.join() if 0 == flg_time: for code in A: if (0 == code['flg_price'] or 0 == code['flg_percent'] ): flg_time = 1 old = time.time() print "---------------- ------------", old if ((1 == flg_time) and ((time.time() - old) > (1 * 60))) : flg_time = 0 print "++++++++++++++++++++++", time.time() for code in A: code['flg_price'] = 1 code['flg_percent'] = 1 #print A[0]['flg_price'], A[0]['flg_percent']