def __on_close(self, ws): Log.info(self.__class__.__name__, "Socket <%s> is closed." % self.id) self._connecting = False self._connected = False if len(self.on_close_handlers) > 0: for handler in self.on_close_handlers: handler(ws)
def on_close_handler(self, instmt, ws): """ Socket on close handler :param instmt: Instrument :param ws: Web socket """ Logger.info(self.__class__.__name__, "Instrument %s is unsubscribed in channel %s" % \ (instmt.get_instmt_name(), instmt.get_exchange_name())) instmt.set_subscribed(False)
def connect(self, **kwargs): """ Connect :param path: sqlite file to connect """ addr = kwargs['addr'] self.conn.bind(addr) Logger.info(self.__class__.__name__, 'Zmq client is connecting to %s' % addr) return self.conn is not None
def start(self, instmt): self.init_instmt_snapshot_table(instmt) Logger.info( self.__class__.__name__, 'instmt snapshot table: {}'.format(instmt.get_instmt_code())) return [ self.api_socket.connect( self.api_socket.get_link(), on_message_handler=partial(self.on_message_handler, instmt), on_open_handler=partial(self.on_open_handler, instmt), on_close_handler=partial(self.on_close_handler, instmt)) ]
def connect(self, url, on_message_handler=None, on_open_handler=None, on_close_handler=None, on_error_handler=None, reconnect_interval=10): """ :param url: Url link :param on_message_handler: Message handler which take the message as the first argument :param on_open_handler: Socket open handler which take the socket as the first argument :param on_close_handler: Socket close handler which take the socket as the first argument :param on_error_handler: Socket error handler which take the socket as the first argument and the error as the second argument :param reconnect_interval: The time interval for reconnection """ Log.info(self.__class__.__name__, "Connecting to socket <%s>..." % self.id) if on_message_handler is not None: self.on_message_handlers.append(on_message_handler) if on_open_handler is not None: self.on_open_handlers.append(on_open_handler) if on_close_handler is not None: self.on_close_handlers.append(on_close_handler) if on_error_handler is not None: self.on_error_handlers.append(on_error_handler) if not self._connecting and not self._connected: self._connecting = True self.ws = websocket.WebSocketApp(url, on_message=self.__on_message, on_close=self.__on_close, on_open=self.__on_open, on_error=self.__on_error) self.wst = threading.Thread(target=lambda: self.__start( reconnect_interval=reconnect_interval)) self.wst.start() return self.wst
def create(self, table, columns, types, primary_key_index=(), is_ifnotexists=True): """ Create table in the database :param table: Table name :param columns: Column array :param types: Type array :param is_ifnotexists: Create table if not exists keyword """ file_path = os.path.join(self.file_directory, table + ".csv") columns = [e.split(' ')[0] for e in columns] if len(columns) != len(types): return False self.lock.acquire() if os.path.isfile(file_path): Logger.info(self.__class__.__name__, "File (%s) has been created already." % file_path) else: with open(file_path, 'w+') as csvfile: csvfile.write(','.join(["\"" + e + "\"" for e in columns])+'\n') self.lock.release() return True
def on_open_handler(self, instmt, ws): """ Socket on open handler :param instmt: Instrument :param ws: Web socket """ Logger.info(self.__class__.__name__, "Instrument %s is subscribed in channel %s" % \ (instmt.get_instmt_name(), instmt.get_exchange_name())) if not instmt.get_subscribed(): Logger.info( self.__class__.__name__, 'last trade string:{}'.format( self.api_socket.get_last_trades_subscription_string( instmt))) Logger.info( self.__class__.__name__, 'ticker string:{}'.format( self.api_socket.get_ticker_subscription_string(instmt))) #ws.send(self.api_socket.get_last_trades_subscription_string(instmt)) ws.send(self.api_socket.get_ticker_subscription_string(instmt)) instmt.set_subscribed(True)
from selenium import webdriver import time from appModules.LoginAction import LoginAction from appModules.AddContact import NewContactPersonAction from config.varCondig import * from util.ParseExcel import ParseExcel from util.Log import Logger import logging import traceback log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO) p = ParseExcel() sheetName = p.wb.sheetnames # 获取所有的sheetname 是个列表 # print(sheetName) def bDriver(): try: driver = webdriver.Firefox() driver.get('https://mail.126.com') driver.implicitly_wait(30) except Exception as e: raise e else: return driver def testMailLogin(driver): ''' 测试用例 :return:
for handler in self.on_message_handlers: handler(m) def __on_open(self): Log.info(self.__class__.__name__, "Socket <%s> is opened." % self.id) self._connected = True if len(self.on_open_handlers) > 0: for handler in self.on_open_handlers: handler(self.ws) def __on_close(self, ws): Log.info(self.__class__.__name__, "Socket <%s> is closed." % self.id) self._connecting = False self._connected = False if len(self.on_close_handlers) > 0: for handler in self.on_close_handlers: handler(ws) def __on_error(self, ws, error): Log.info(self.__class__.__name__, "Socket <%s> error:\n %s" % (self.id, error)) if len(self.on_error_handlers) > 0: for handler in self.on_error_handlers: handler(ws, error) if __name__ == '__main__': Log.init_log() socket = WebsocketApiClient('test') socket.connect('ws://localhost', reconnect_interval=1) time.sleep(10)
isFetchAll=True): """ Select rows from the table :param table: Table name :param columns: Selected columns :param condition: Where condition :param orderby: Order by condition :param limit: Rows limit :param isFetchAll: Indicator of fetching all :return Result rows """ return [] def delete(self, table, condition='1==1'): """ Delete rows from the table :param table: Table name :param condition: Where condition """ return True if __name__ == '__main__': Logger.init_log() db_client = ZmqClient() db_client.connect(addr='tcp://127.0.0.1:5000') for i in range(1, 100): Logger.info('1', '2') db_client.insert('test', ['c1', 'c2', 'c3', 'c4'], [], ['abc', u'sb_' + str(i), 1.1, 5]) time.sleep(0.1)
#!/usr/bin/python #-*-coding:UTF-8-*- import pyhs2 from util.Log import Logger # 构建hive server2连接 aLog = Logger("HiveClient") class HiveClient: def __init__(self, db_host, user, password, database, port=10001, authMechanism="PLAIN"): """ create connection to hive server2 """ self.conn = pyhs2.connect( host=db_host, port=port, authMechanism=authMechanism, user=user, password=password, database=database, ) self.cur = self.conn.cursor()
:param instmt: Instrument :param message: Message """ print message def start(self, instmt): self.init_instmt_snapshot_table(instmt) Logger.info( self.__class__.__name__, 'instmt snapshot table: {}'.format(instmt.get_instmt_code())) return [ self.api_socket.connect( self.api_socket.get_link(), on_message_handler=partial(self.on_message_handler, instmt), on_open_handler=partial(self.on_open_handler, instmt), on_close_handler=partial(self.on_close_handler, instmt)) ] if __name__ == '__main__': import logging import websocket websocket.enableTrace(True) logging.basicConfig() Logger.init_log() exchange_name = 'Deribit' instmt_name = 'BTC' instmt_code = 'BTC-20DEC19-7250-C' instmt = Instrument(exchange_name, instmt_name, instmt_code) exch = ExchGwTradeDeribit([]) exch.start(instmt)
#!/usr/bin/python #-*-coding:UTF-8-*- __author__ = 'huixia.cao' import pymongo from util.Log import Logger # 构建pymongo连接 iLog = Logger("MonClient") class MonClient: def __init__(self, db_host, port): """ create connection to pymongo """ self.conn = pymongo.MongoClient(host=db_host, port=port) def insertone(self, database, collection, data): """ query """ db = self.conn[database] query = db[collection] try: insert = query.insert_one(data) iLog.info("mongodb insert one: %s" % data) except Exception, e: iLog.error("mongodb insert one error:%s" % e) def insertmany(self, database, collection, datalist):
def __start(self, reconnect_interval=10): while True: self.ws.run_forever() Log.info(self.__class__.__name__, "Socket <%s> is going to reconnect..." % self.id) time.sleep(reconnect_interval)
#!/usr/bin/python # -*- coding:utf-8 -*- from util.Log import Logger from hbase.ttypes import * from util.MojiConfig import MojiConfig from util.Log import Logger from util import HbaseClient import datetime, os iLog = Logger("Similars") thrift_host = "54.223.210.215" port = 9090 # if __name__ == "__main__":
def __on_error(self, ws, error): Log.info(self.__class__.__name__, "Socket <%s> error:\n %s" % (self.id, error)) if len(self.on_error_handlers) > 0: for handler in self.on_error_handlers: handler(ws, error)
def __on_open(self): Log.info(self.__class__.__name__, "Socket <%s> is opened." % self.id) self._connected = True if len(self.on_open_handlers) > 0: for handler in self.on_open_handlers: handler(self.ws)