美文网首页
python沪深行情监控

python沪深行情监控

作者: 博陵韩少 | 来源:发表于2018-11-23 14:16 被阅读0次

pyinstaller编译为windows下的可执行exe文件。效果图如下:


preview.PNG
image.png

代码:

# -*- coding:utf-8 -*-
# coding: utf-8 cp936
# author:hanlin
# QQ:564868230
#获取新版手机行情主站 k线 分时线line
#
#'L_stockQuote:line:sh#600016'
#'L_stockQuote:line:sz#000001'
#'L_stockQuote:kline_daily:sh#600016'
#'L_stockQuote:kline_daily:sh#600016#qfq'
#'L_stockQuote:kline_daily:sz#000001'
#
#@V1.1 非开市时间不检查 未做
#@V2.0 增加站点分组,不同站点redis开启的数量不同
#      增加各个站点显示名,业务类型(行情HQ、交易JY)
#      输出内容调整。一个IP打印四行内容变为打印1行
#      多线程处理、缓冲打印区
#@V2.0.1  增加缓冲打印区输出条件FINISHED_SVR_NUM=SvrNum,废弃refresh_time
#@V2.0.2  FINISHED_SVR_NUM>SvrNum 情况排除;加锁
#2.0.3  多线程并行,加锁,但打印时出现IP不完整问题
#2.0.4  多线程并行,加锁,lock后再计算,假并行, 存在个别站点lock抢不到情况!
#2.0.5  打印60s   多线程calc 5s,同时加一个各站点计算完标志位SERVER_FINISH_FLAG,放置不断lock_req
#2.1.0  line数据结构有变化
#2.1.2  CodeList monitor && connect exception
#V3.0.0 1)redis数据需解析flatbuff,直接远程执行脚本
#       2)增加在线人数指标,仅显示,不报警
import datetime
import time
import ctypes,sys,os
import redis
import ConfigParser
import socket
import threading
import chardet
import paramiko
import logging
import logging.config
import base64

#VERSION = '*****行情监控V2.1.0 (csc版)'
VERSION = 'HqMonitor ver2.1.0 csc'
#VERSION = VERSION.decode('utf-8').encode('gb2312')
#VERSION = VERSION.decode('utf-8').encode('cp936')#('ISO-8859-2')
#code=chardet.detect(VERSION)
#print code
ALL_REALTIME = {}
SERVER_FINISH_FLAG = {}
FINISHED_SVR_NUM = 0
lock = threading.Lock()
#------------------------My date func---------------------------

''' ''' 
def str2datetime(datestr,format='%Y%m%d'):      
    return datetime.datetime.strptime(datestr,format)  

''' '''
def date2str(date,format='%Y%m%d'):    
    return   datetime.datetime.strftime(date,format)

''' today '''
def getToday():
    return datetime.date.today()

''' yesterday '''
def getYesterday(today=None):
    if today==None:
        today=datetime.date.today()
    oneday=datetime.timedelta(days=1)
    yesterday=today-oneday
    return yesterday
''' Tomorrow '''
def getTomorrow(today=None):
    if today==None:
        today=datetime.date.today()
    oneday=datetime.timedelta(days=1)
    tomorrow=today+oneday
    return tomorrow
''' weekday '''
def getWkday(date):
    #wkday=datetime.datetime(date).strftime("%w")
    week_day_dict = {
    0 : 'Mon',
    1 : 'Tues',
    2 : 'Wed',
    3 : 'Thur',
    4 : 'Fri',
    5 : 'Sat',
    6 : 'Sun',
    }
    day = date.weekday()
    return week_day_dict[day]

'''eg input:20160729-20160801 output:[20160729,20160730,20160801]'''
def getDaysByDaysSeg(days_seg):  
    days=days_seg.split('-')
    start_day=str2datetime(days[0])
    end_day=str2datetime(days[1]) 
    oneday=datetime.timedelta(days=1)
    li=[days[0]]
    while True:
        start_day=start_day+oneday
        li.append(date2str(start_day))
        if start_day == end_day:
            break 
    return li

''' get lsat trade day'''
def getLastTradeDay(today,holiday):
    lastday=today
    while True:
        oneday=datetime.timedelta(days=1)
        lastday=lastday-oneday
        weekend=['Sat','Sun']
        if getWkday(lastday) in weekend:
            continue
        elif date2str(lastday) in holiday:
            continue
        else:
            break
    return lastday

'''getHolidayList'''
def getHolidayList(holiday):
    holiday_list=[]
    length=len(holiday)
    for i in range(length):
        if '-' in holiday[i]:
            li=getDaysByDaysSeg(holiday[i])
            for li_day in li:
                holiday_list.append(li_day)
        else:
            holiday_list.append(holiday[i])
    return holiday_list

#------------------------My color func---------------------------
# Windows CMD命令行 字体颜色定义 text colors
FOREGROUND_BLACK = 0x00 # black.
FOREGROUND_DARKBLUE = 0x01 # dark blue.
FOREGROUND_DARKGREEN = 0x02 # dark green.
FOREGROUND_DARKSKYBLUE = 0x03 # dark skyblue.
FOREGROUND_DARKRED = 0x04 # dark red.
FOREGROUND_DARKPINK = 0x05 # dark pink.
FOREGROUND_DARKYELLOW = 0x06 # dark yellow.
FOREGROUND_DARKWHITE = 0x07 # dark white.
FOREGROUND_DARKGRAY = 0x08 # dark gray.
FOREGROUND_BLUE = 0x09 # blue.
FOREGROUND_GREEN = 0x0a # green.
FOREGROUND_SKYBLUE = 0x0b # skyblue.
FOREGROUND_RED = 0x0c # red.
FOREGROUND_PINK = 0x0d # pink.
FOREGROUND_YELLOW = 0x0e # yellow.
FOREGROUND_WHITE = 0x0f # white.
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12

# get handle
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
 
def set_cmd_text_color(color, handle=std_out_handle):
    Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
    return Bool
 
#reset text white
def resetColor():
    set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)

def colorText(text,color):
    set_cmd_text_color(color)
    sys.stdout.write(text)
    resetColor()

#------------------------My SSH func---------------------------
def isPortOpen(ip,port):
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try:
        s.connect((ip,int(port)))
        s.shutdown(1)
        #print('%s:%d is open'%(ip,port))
        return True
    except:
        #print('%s:%d is down'%(ip,port))
        return False

def ssh2(ip,username,passwd,cmd):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip,22,username,passwd,timeout=5)
        out = []
        for m in cmd:
            try:
                stdin, stdout, stderr = ssh.exec_command(m)
#                stdin.write("Y")   # ‘Y’
                err_disp = stderr.readlines()
                if err_disp!=[]:
                    print '--inner--',m,err_disp
                    logging.error(err_disp)
                outm = stdout.readlines()
                out += outm
#                print out,outm
#                for o in outm:
#                    print o
            except Exception,e:
                print Exception,"::",e
                return Exception
        ssh.close()
#        print len(out)
        return out
    except Exception,e:
        print Exception,"::",e
        return Exception

#------------------------My quote func---------------------------       
def getlineKey(market,stock):
    return 'L_stockQuote:line:'+market+'#'+stock

def getKlineKey(market,stock):
    return 'L_stockQuote:kline_daily:'+market+'#'+stock

def printLineTime(ip,market,stock,last_time,ctime,flag):
    if flag:
        colorText('[Success]',FOREGROUND_GREEN)
    else:
        colorText('[Failed ]',FOREGROUND_RED)
    print('IP:%16s %2s %6s  line----%8s CurrentTime:%8s'%(ip,market,stock,last_time,ctime))


def getRealCondition(realtime):
    #['10.237.0.51', 'null', 'null', '20161119', '100726', 'null', 'null', '20161119', '100726']
    #sh
    ip = realtime[0]
    last_date_sh = realtime[1]
    last_time_sh = realtime[2]
    last_date_sz = realtime[5]
    last_time_sz = realtime[6]
    cdate = realtime[3]
    ctime = realtime[4]
    if last_time_sh != 'null':
#        dif_sh = abs(int(ctime[-4:-2])-int(last_time_sh[-4:-2]))
        dif_sh = abs(int(ctime[2:4])-int(last_time_sh[2:4]))
    if last_time_sz != 'null':
        dif_sz = abs(int(ctime[2:4])-int(last_time_sh[2:4]))
    # 1: Success 0:Failed
    if last_date_sh == 'null' or last_time_sh=='null':
        sh_flag = 0   
    elif last_date_sh != cdate:
        sh_flag = 0
    elif dif_sh > 1:
        sh_flag =0
    else:
        sh_flag =1
    if last_date_sz == 'null' or last_time_sz=='null':
        sz_flag = 0
    elif last_date_sz != cdate:
        sz_flag = 0
    elif dif_sz > 1:
        sz_flag =0
    else:
        sz_flag =1
    return [ip,last_date_sh,last_time_sh,last_date_sz,last_time_sz,cdate,ctime,sh_flag,sz_flag]

''' [last_date,last_time,cdate,ctime] ''' ##----Not in use in V3.0.0
def getKline(ip,port,port_num,market,stock,stype):
    cdate=time.strftime('%Y%m%d',time.localtime(time.time()))
    ctime=time.strftime('%H%M%S',time.localtime(time.time()))
    key_line=getlineKey(market,stock)
    key_kline=getKlineKey(market,stock)
    is_exists_key_line = False
    is_exists_key_kline = False
    for i in range(port_num):
        try:
            #print(i)
            r=redis.Redis(host=ip,port=port+i)
        except:
            print('[Error]')
        if r.exists(key_line):
            llen=r.llen(key_line)
            line_last=r.lindex(key_line,llen-1)
            line_last_3=line_last.split('#')
            last_time=line_last_3[len(line_last_3)-3]
            is_exists_key_line =True
            #ctime=time.strftime('%H%M%S',time.localtime(time.time()))   
            #dif = abs(int(ctime[2:4])-int(last_time[2:4]))# 待修改为时间格式计算
        if stype == 'HQ' and r.exists(key_kline):
            llen=r.llen(key_kline)
            kline_last=r.lindex(key_kline,llen-1)
            last_date=kline_last.split('#')[0]
            is_exists_key_kline = True
            #cdate=time.strftime('%Y%m%d',time.localtime(time.time()))
    if is_exists_key_line == False:
        last_time = 'null'
    if is_exists_key_kline == False:
        last_date = 'null'
    if stype == 'JY':
        last_date = cdate
    return [last_date,last_time,cdate,ctime]

''' hlen '''     ##----Not in use in V3.0.0
def getCodeListNum(ip,port,port_num):
    cdate=time.strftime('%Y%m%d',time.localtime(time.time()))
    ctime=time.strftime('%H%M%S',time.localtime(time.time()))
    key_codelist='H_stockQuote:stockDetails:codeList'
    hlen=0
    for i in range(port_num):
        try:
            #print(i)
            r=redis.Redis(host=ip,port=port+i)
        except:
            print('[Error]')
        if r.exists(key_codelist):
            hlen=r.hlen(key_codelist)
    return [hlen]

def hqMonitorBackgrd(server_dict,market,stock,refresh_time):
    stype = server_dict['stype']
    sname = server_dict['name']
    IP_list = server_dict['IP_list']
    port = server_dict['port']
    port_num = server_dict['port_num']
    cmd_list = server_dict['cmd_list']
    user = server_dict['user']
    pwd = server_dict['pwd']
    marketSH=market[0]
    marketSZ=market[1]
    stockSH=stock[0]
    stockSZ=stock[1]
    global FINISHED_SVR_NUM
    global ALL_REALTIME
    global SERVER_FINISH_FLAG
    SERVER_FINISH_FLAG[sname]=False
    while True:        
        if SERVER_FINISH_FLAG[sname] == False:
        ## if server_sname[ip1 ip2 ip3] not finished
            ALL_REALTIME[sname]=[]          
            for IPi in IP_list:
#                sh_realtime = getKline(IPi,port,port_num,marketSH,stockSH,stype)
#                sz_realtime = getKline(IPi,port,port_num,marketSZ,stockSZ,stype)
                cdate=time.strftime('%Y%m%d',time.localtime(time.time()))
                ctime=time.strftime('%H%M%S',time.localtime(time.time()))
                cmd_out = ssh2(IPi,user,pwd,cmd_list)
                ## format 2 str
#                print cmd_out
#                len_cmd = len(cmd_out)
                for i in range(len(cmd_out)):
                    ## delete '\n'
                    cmd_out[i] = str(cmd_out[i]).replace("\n","")
                sh_realtime = cmd_out[0].split(' ')+[cdate,ctime]
                sz_realtime = cmd_out[1].split(' ')+[cdate,ctime]
                codelist = int(cmd_out[2])
                #print '@@',codelist,type(codelist),int(codelist)
                realtime = [IPi]
                realtime += sh_realtime
                realtime += sz_realtime
        ## add *flag ,is realtime correct?
                realtime = getRealCondition(realtime)
        ## add codelist num
                realtime += [codelist] ##getCodeListNum(IPi,port,port_num)
                ALL_REALTIME[sname].append(realtime)
            SERVER_FINISH_FLAG[sname]=True
        ##After get all realtime @server, req lock to FINISHED_SVR_NUM++
            while True:
                if lock.acquire():
                    #print('Before %s'%FINISHED_SVR_NUM)
                    FINISHED_SVR_NUM += 1
                    #print('After %s %s'%(sname,FINISHED_SVR_NUM))
                    lock.release()
                    break
                time.sleep(0.5)
        time.sleep(int(5))

def printOneByOne(realtime_with_flag):
    #[ip,last_date_sh,last_time_sh,last_date_sz,last_time_sz,cdate,ctime,sh_flag,sz_flag,codelistNum]
    #[ip,last_date_sh,last_time_sh,last_date_sz,last_time_sz,cdate,ctime,sh_flag,sz_flag]
    # 1: Success 0:Failed
    sh_flag = realtime_with_flag[7]
    sz_flag = realtime_with_flag[8]
    n_codelist = realtime_with_flag[9]
    ip = realtime_with_flag[0]
    last_date_sh = realtime_with_flag[1]
    last_time_sh = realtime_with_flag[2]
    cdate = realtime_with_flag[5]
    ctime = realtime_with_flag[6]
    #colorText(ip+'    ',FOREGROUND_GREEN)
    sh_realtime = 'SH: '+realtime_with_flag[1]+'--'+realtime_with_flag[2]+'  '
    sz_realtime = 'SZ: '+realtime_with_flag[3]+'--'+realtime_with_flag[4]+'  '
    if sh_flag == 1 and sz_flag == 1:
        colorText('[Success]',FOREGROUND_GREEN)
    elif sh_flag == 0 and sz_flag == 0:
        colorText('[Failed:sh&sz]',FOREGROUND_RED)
    elif sh_flag == 0:
        colorText('[Failed:sh]',FOREGROUND_RED)
    else:
        colorText('[Failed:sz]',FOREGROUND_RED)
    content='\t'+ip+'\t'+sh_realtime+'\t'+sz_realtime+'\tCodeList:'+str(n_codelist)+'\tNow:'+ctime
    print(content)
    
def print2Console(ALL_REALTIME,server_num):
    for i in range(int(server_num)):
        sname = config.get("Server"+str(i),"server_name")
        print(sname)
        server_realtime = ALL_REALTIME[sname]
        for i in range(len(server_realtime)):
            printOneByOne(server_realtime[i])

def printAlarms(ALL_REALTIME,server_num):
    mean = {}
    for i in range(int(server_num)):
        sname = config.get("Server"+str(i),"server_name")
        mean[sname]=0
        server_realtime = ALL_REALTIME[sname]
        sub_ser_n = len(server_realtime)
        for j in range(sub_ser_n):
#            print server_realtime[i]
            ## [ip,last_date_sh,last_time_sh,last_date_sz,last_time_sz,cdate,ctime,sh_flag,sz_flag,n_codelist]
            mean[sname] += server_realtime[j][-1]
        mean[sname] = mean[sname]/int(sub_ser_n)
        #mean['diff'] = 0
    l_sort_desc = sorted(mean.items(),key=lambda e:e[1],reverse=True)
    ## static High
    isalarm = 0
    for i in range(int(server_num)):
        if l_sort_desc[i][1] != l_sort_desc[0][1]:
            ##sname = config.get("Server"+str(i),"server_name")
            sname = l_sort_desc[i][0]
            colorText('[CodeListNum Wrong:]',FOREGROUND_PINK)
            print sname,mean[sname]
            isalarm = 1
    if isalarm == 0:
        colorText('############################################################',FOREGROUND_GREEN)
        print('CodeListNum:OK')
        

#------------------------End of My func's ---------------------------

if __name__=='__main__':
    #title
    ctime=time.strftime('%Y-%m-%d %a %H:%M:%S',time.localtime(time.time()))
    print "*************************************************** "
    print "****------  Hq Monitor for SJZQ V3.0.0  -------**** "
    print "****-------------------------------------------**** "
    print "****------------------- By: hanlin  2018-04-25 **** "
    print "*************************************************** "
    print("Start @ %s... ..."%ctime)
    print ' '
    ## read INI
    config=ConfigParser.ConfigParser()
    config.readfp(open('set.ini'))   
##    sections = config.sections();
##    sec_num = len(sections)
##    print sections,sec_num
    user = config.get("ConnInfo","user")
    pwd = config.get("ConnInfo","pwd")
#    print pwd
#    pwd_encode = base64.encodestring(pwd)
#    pwd_decode = base64.decodestring(pwd_encode)
#    print pwd,pwd_encode,pwd_decode
    cmd_list = config.get("ConnInfo","cmd_list")
    cmd_list = list(eval(cmd_list))
    server_num = config.get("ServerNum","server_num")
    marketSH = config.get('SH','market')
    stockSH = config.get('SH','stock')
    marketSZ = config.get('SZ','market')
    stockSZ = config.get('SZ','stock')
    refresh_time = config.get('Other','refresh_time')
    market = [marketSH,marketSZ]
    stock = [stockSH,stockSZ]
    #print(VERSION)
    #print('SH: %s  SZ: %s   RefreshTime: %s'%(stockSH,stockSZ,refresh_time))
    for i in range(int(server_num)):
        server_type = config.get("Server"+str(i),"server_type")
        server_name = config.get("Server"+str(i),"server_name")
        #code=chardet.detect(server_name)
        #print(code)
        IP_list = config.get("Server"+str(i),"IP_list")
        IP_list = list(eval(IP_list))
        port = int(config.get('Server'+str(i),'port'))
        port_num = int(config.get('Server'+str(i),'port_num'))
        server_dict = {}
        server_dict['stype'] = server_type
        server_dict['name'] = server_name
        server_dict['IP_list'] = IP_list
        server_dict['port'] = port
        server_dict['port_num'] = port_num
        server_dict['user'] = user
        server_dict['pwd'] = pwd
        server_dict['cmd_list'] = cmd_list
        #print server_type,server_name
        #hqMonitorBackgrd(server_dict,market,stock,refresh_time)
        thread_tmp=threading.Thread(target=hqMonitorBackgrd,args=(server_dict,market,stock,refresh_time))
        thread_tmp.setDaemon(True)
        thread_tmp.start()
    # first output --if all the server not finished calc then print schedule like: 1/6 (FINISHED_SVR_NUM/server_num)
    while True:
        print('FINISHED_SVR_NUM:%s/%s'%(FINISHED_SVR_NUM,server_num))
        if FINISHED_SVR_NUM == int(server_num):
            #print(VERSION)
            print('SH: %s  SZ: %s   RefreshTime: %s'%(stockSH,stockSZ,refresh_time))
            print2Console(ALL_REALTIME,server_num)
            colorText('############################################################',FOREGROUND_BLUE)
            ctime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
            print(ctime)
            ## Extra printing Other alarms like codelistnum 
            printAlarms(ALL_REALTIME,server_num)
            FINISHED_SVR_NUM = 0
            for k,v in SERVER_FINISH_FLAG.items():
                SERVER_FINISH_FLAG[k] = False
            break
        #print('*0* FINISHED_SVR_NUM:%s server_num:%s'%(FINISHED_SVR_NUM,server_num))
        time.sleep(1)
    # Circle output
    while True:
        #print('FINISHED_SVR_NUM:%s server_num:%s'%(FINISHED_SVR_NUM,server_num))
        if FINISHED_SVR_NUM == int(server_num):
            #print(VERSION)
            print('SH: %s  SZ: %s   RefreshTime: %s'%(stockSH,stockSZ,refresh_time))
            print2Console(ALL_REALTIME,server_num)
            colorText('############################################################',FOREGROUND_BLUE)
            ctime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
            print(ctime)
            ## Extra printing Other alarms like codelistnum 
            printAlarms(ALL_REALTIME,server_num)
            FINISHED_SVR_NUM = 0
            for k,v in SERVER_FINISH_FLAG.items():
                SERVER_FINISH_FLAG[k] = False
        #print('*0* FINISHED_SVR_NUM:%s server_num:%s'%(FINISHED_SVR_NUM,server_num))
        time.sleep(int(refresh_time))

set.ini

[ConnInfo]
user = root
pwd = csc_1o8
cmd_list = ['/tmp/getQuoteTime.sh','/tmp/getQuoteTime2.sh','/tmp/getCodelist.sh']

[ServerNum]
#此处个数为行情+交易站点总个数!
server_num = 1

[Server0]
# server_type: 行情HQ、交易后端JY
server_type = HQ
server_name = 测试行情主站
IP_list = ['192.168.237.120']
#参考行情主站或交易后端redis的设置
port = 6379
port_num = 20

[Server1]
server_type = JY
server_name = server1
IP_list = ['10.237.0.51','10.237.0.116']
port = 6379
port_num = 10

[Server2]
server_type = 测试交易主站
server_name = server2
IP_list = ['10.237.0.52','10.237.0.116']
port = 6379
port_num = 10


[SH]
market=sh
#stock=000001 上证指数
stock=600050

[SZ]
market=sz
#srock=399001 深证指数
stock = 399001

[Other]
#单位 秒
#refresh_time=300
refresh_time=15
#debug=1 开启debug模式
debug=0 

[Holiday]
#模块未开启
holiday = 20160101,20160208,20160209,20160210,20160211,20160212,20160404,20160502,20160609,20160610,20160915,20160916,20161003,20161004,20161005,20161006,20161007

相关文章

网友评论

      本文标题:python沪深行情监控

      本文链接:https://www.haomeiwen.com/subject/pfjjqqtx.html