我的类

# -*- coding: utf-8 -*-
import sqlalchemy
import tushare
import pandas
########################################################################
class Myclass:
    """"""
    #----------------------------------------------------------------------
    def __init__(self, User = 'sa', Password = '123456', Dsn = 'XiTongDSN', Host = '192.168.1.3', Port = 12345):
        """本地数据库 sql server 信息初始化"""
        self.User = User
        self.Password = Password
        self.Dsn = Dsn
        self.Engine = sqlalchemy.create_engine('mssql+pyodbc://'+self.User+':'+self.Password+'@'+self.Dsn)
        """网络 socket 地址信息初始化"""
        self.Host = Host
        self.Port = Port
    #----------------------------------------------------------------------
    def Get_urldata(self):
        """从网站 tushare.org 下载数据"""
        Codelist = pandas.DataFrame((tushare.get_today_all())['code'])
        Codelist.to_sql('Codelist', self.Engine, if_exists = 'replace', index = False)
        for i in range(0, len(Codelist)):
            Stockdata = tushare.get_h_data(codelist['code'][i])
            Index = list(Stockdata['open'].index)
            Stockdata['date'] = pandas.Series(Index, Index)
            pandas.DataFrame(Stockdata, Index)
            Stockdata.to_sql(Codelist['code'][i], self.Engine, if_exists = 'replace', index = False)
    #----------------------------------------------------------------------
    def Get_sqldata(self, Code = 'sh'):
        """调用本地数据库 sql server 数据"""
        Connection = self.Engine.connect()
        Stockdata = pandas.read_sql_table(Code, Connection)
        Connection.close()
        return Stockdata
    #----------------------------------------------------------------------
    def Run_server(self):
        """"""
        Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        Socket.bind((self.Host, self.Port))
        Socket.listen(5)
        Engine = sqlalchemy.create_engine('mssql+pyodbc://sa:123456@XiTongDSN')
        '''修改1'''
        Dataframe = pandas.read_sql('sh', Engine)
        I = list(Dataframe['date'].index)
        O = Dataframe['open']
        H = Dataframe['high']
        L = Dataframe['low']
        C = Dataframe['close']
        V = Dataframe['volume']
        i = 0
        while True:            
            Connection, Address = Socket.accept()
            if Connection.recv(1024) == b'Link' and i < len(I):               
                '''修改2'''                
                Connection.send(struct.pack('i5f', I[i], O[i], H[i], L[i], C[i], V[i]))
                i += 1
            else:
                Connection.close()
                Socket.close()
    #----------------------------------------------------------------------
    def Run_client(self):
        """"""
        Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        Socket.connect((self.Host, self.Port))
        Socket.send(Message)
        '''修改3'''
        I, O, H, L, C, V = struct.unpack('i5f', Socket.recv(1024)[:24])
        Socket.close()
        return I, O, H, L, C, V
    #----------------------------------------------------------------------
    def Tempfile(self):
        """"""
posted @ 2016-06-03 01:57  佩雷尔曼的信徒  阅读(262)  评论(0编辑  收藏  举报