コード例 #1
0
ファイル: audit.py プロジェクト: M0Rf30/OpenBazaar-Server
class Audit(object):
    """
    A class for handling audit information
    """

    def __init__(self, db, enabled=True):
        self.db = db
        self.enabled = enabled

        self.log = Logger(system=self)

        self.action_ids = {
            "GET_PROFILE": 0,
            "GET_CONTRACT": 1,
            "GET_LISTINGS": 2,  # Click Store tab
            "GET_FOLLOWING": 3,
            "GET_FOLLOWERS": 4,
            "GET_RATINGS": 5
        }

    def record(self, guid, action_id, contract_hash=None):
        if self.enabled is not True:
            return
        self.log.info("Recording Audit Event [%s]" % action_id)

        if action_id in self.action_ids:
            self.db.audit_shopping.set(guid, self.action_ids[action_id], contract_hash)
        else:
            self.log.error("Could not identify this action id")
コード例 #2
0
 def run_daemons(self):
     cli = subprocess.Popen("CLI", stdout=subprocess.PIPE,
         stderr=subprocess.PIPE, stdin=subprocess.PIPE)
     Logger.log("Клиент запущен")
     srv = subprocess.Popen("SRV", stdout=subprocess.PIPE,
         stderr=subprocess.PIPE, stdin=subprocess.PIPE)
     Logger.log("Сервер запущен")
コード例 #3
0
    def run(self, options):
        """
        Runs the application. 'options' contains the CLI options dictionary.
        """
        
        # Setup logging
        if options.debug:
            Logger.set_log_level(Logger.DEBUG)
        
        # Load configuration
        self.config = ConfigParser.RawConfigParser()
        self.config.read(options.config)

        MidiEngine.initialize()
        
        Logger.debug("Command line options: %s" % (options))

        self.setup()
        
        if options.list_devices:
            self.list_devies()
        elif options.interactive:
            self.interactive_mode()
        else:
            self.loop()
コード例 #4
0
ファイル: fx.py プロジェクト: westlicht/omxone
    def __set_state(self, state):
        self.__state = state
        
        Logger.debug("Switching to state %s" % (self.state_names[self.__state]))

        if self.__state == self.STATE_NORMAL:
            self.__update_levels(127, 0, 0)
            self.send_ctrl('master', 127)
            time.sleep(self.SWITCH_INTERVAL)
            self.send_ctrl('send1', 0)
            self.send_ctrl('send2', 0)
        elif self.__state == self.STATE_MUTE:
            self.__update_levels(0, 0, 0)
            self.send_ctrl('master', 0)
            self.send_ctrl('send1', 0)
            self.send_ctrl('send2', 0)
        elif self.__state == self.STATE_FX1:
            self.__update_levels(0, 127, 0)
            self.send_ctrl('send1', 127)
            time.sleep(self.SWITCH_INTERVAL)
            self.send_ctrl('master', 0)
            self.send_ctrl('send2', 0)
        elif self.__state == self.STATE_FX2:
            self.__update_levels(0, 0, 127)
            time.sleep(self.SWITCH_INTERVAL)
            self.send_ctrl('send2', 127)
            self.send_ctrl('master', 0)
            self.send_ctrl('send1', 0)
            
        self.send_ctrl('selector', (self.__state / 3.0) * 127.0)
コード例 #5
0
ファイル: artist_relation.py プロジェクト: pscn/ads
  def save(relation):
    if ArtistRelationFactory.__use_db:
      artistA = relation.artistA.key()
      artistB = relation.artistB.key()
      if artistA > artistB:
        artistC = artistB
        artistB = artistA
        artistA = artistC
      ArtistRelationFactory.__db.execute(
          u"REPLACE INTO artist_relation "\
          "(artistA, artistB, relation) VALUES (?, ?, ?)", (
          artistA, artistB, Binary(pickle.dumps(relation, -1))))
      ArtistRelationFactory.__db.commit()

    elif TrackRelationFactory.__use_fs:
      pathA = join(ArtistRelationFactory.__path, relation.artistA.key())
      fileA = u"%s.pkl" % join(pathA, relation.artistB.key())
      pathB = join(ArtistRelationFactory.__path, relation.artistB.key())
      fileB = u"%s.pkl" % join(pathB, relation.artistA.key())
      if not isdir(pathA): makedirs(pathA)
      if not isdir(pathB): makedirs(pathB)
      if fileA > fileB:
        fileC = fileB
        fileB = fileA
        fileA = fileC
      ArtistRelationFactory._save(relation, fileA)
      if fileA != fileB:
        if exists(fileB): remove(fileB)
        try:
          link(fileA, fileB)
        except OSError:
          Logger.error(u"Failed to link %s to %s" % (fileA, fileB))
      relation.lastmodified = getmtime(fileA)
コード例 #6
0
ファイル: server.py プロジェクト: freezmeinster/pravega
class Server(object):
    def __init__(self,host=None,port=None):
        set = Settings()
        self.logger = Logger()
        if host or port == None :
            host = set.get_item('core','server_host')
            port = set.get_item('core','server_port')
        self.host = host
        self.port = port
        Pyro4.config.HMAC_KEY = set.get_item('core','hmac_phrase')
        if is_ip_set(self.host):
            self.daemon = VinllaDaemon(
                host = self.host,
                port = self.port
            )
        else :
            self.logger.critical(
                "[Server] Server can't bind to address %s, Node Manager not detect this IP belonging this Server" % self.host
            )
            sys.exit()
    
    def hook_object(self,obj,id=None):
        if id == None :
            id = obj.__class__.__name__
        self.uri = self.daemon.register(obj,objectId=id)
    
    def run(self):
        self.logger.success(
            "[Server] Node Manager Server successfuly run at %s" %
            (self.daemon.locationStr)
            )
        self.daemon.requestLoop()
コード例 #7
0
ファイル: protocol.py プロジェクト: cpacia/pybitcoin
class PeerFactory(ClientFactory):

    def __init__(self, params, user_agent, inventory, subscriptions, bloom_filter, disconnect_cb, blockchain, download_listener):
        self.params = params
        self.user_agent = user_agent
        self.inventory = inventory
        self.subscriptions = subscriptions
        self.bloom_filter = bloom_filter
        self.cb = disconnect_cb
        self.protocol = None
        self.blockchain = blockchain
        self.download_listener = download_listener
        bitcoin.SelectParams(params)
        self.log = Logger(system=self)

    def buildProtocol(self, addr):
        self.protocol = BitcoinProtocol(self.user_agent, self.inventory, self.subscriptions, self.bloom_filter, self.blockchain, self.download_listener)
        return self.protocol

    def clientConnectionFailed(self, connector, reason):
        self.log.warning("Connection failed, will try a different node")
        self.cb(self)

    def clientConnectionLost(self, connector, reason):
        self.cb(self)
コード例 #8
0
class ToolsLib(object):
    
    log = None
    artifactPath = "install"
    
    def __init__(self):
        
        self.log = Logger()
        
    def findFile(self, needle, extension):

        # Find the zip file (We have to find like this because the date changes)
        found = False
        for file in os.listdir(self.artifactPath):            
            if string.find(file, needle) >= 0 and re.search(extension + "$", file, re.IGNORECASE):
                found = True
                break;
        
        if not found:
            self.log.debug("files=%s" % ','.join(os.listdir(self.artifactPath)))
            raise Exception("Unable to find a zip file that has [%s] in the name with [%s] extension" % (needle, extension))
        
        # Get the name of the file minus the extension
        reobj = re.compile(r"(?P<filename>.*?)(?P<ext>\.[0-9a-z]*$)", re.IGNORECASE)
        return reobj.search(file).group("filename")
コード例 #9
0
ファイル: threadpool.py プロジェクト: cpascual/taurus
 def __init__(self, name=None, parent=None, Psize=20, Qsize=20, daemons=True):
     Logger.__init__(self, name, parent)
     self._daemons = daemons
     self.localThreadId = 0
     self.workers = []
     self.jobs = Queue(Qsize)
     self.size = Psize
     self.accept = True
コード例 #10
0
ファイル: threadpool.py プロジェクト: cpascual/taurus
 def __init__(self, pool, name=None, daemon=True):
     name = name or self.__class__.__name__
     Thread.__init__(self, name=name)
     Logger.__init__(self, name, pool)
     self.daemon = daemon
     self.pool = pool
     self.cmd = ""
     self.busy = False
コード例 #11
0
ファイル: rating.py プロジェクト: pscn/ads
 def by_key(key):
   if RatingFactory.__lib.has_key(key):
     return RatingFactory.__lib[key]
   Logger.debug(u"Rating.by_key((%s, %s, %s, %s, %s, %s, %s, %s, %s)): create from key" % (
       key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7],
       key[8]))
   return RatingFactory.get(key[0], key[1], key[2], key[3], key[4], key[5],
       key[6], key[7], key[8])
コード例 #12
0
ファイル: channel.py プロジェクト: westlicht/omxone
 def __receive(self, input, msg):
     # Filter message if it does not belong to the configured MIDI channel
     if msg.getChannel() != self.channel:
         return
     Logger.debug("[%s] Received message: %s" % (self.name, MidiEngine.dump_msg(msg)))
     # Dispatch received message
     if self.receive:
         self.receive(self, msg)
コード例 #13
0
ファイル: artist.py プロジェクト: pscn/ads
 def update(self, diff, new, factor=1.0):
   if diff > 0.0: self.lastplayed = now()
   if new:
     self.track_cnt += 1
     diff += 0.5
   self.track_rating += diff
   if self.track_cnt > 0: self.rating = self.track_rating / self.track_cnt
   Logger.info(u"updated rating: %s" % self)
コード例 #14
0
ファイル: artist_relation.py プロジェクト: pscn/ads
 def update(self, diff, new, factor=1.0):
   if new:
     self.track_cnt += 1
     diff += 0.5
   self.track_rating += diff
   if self.track_cnt > 0: self.rating = self.track_rating / self.track_cnt
   Logger.info(u"updated rating: %s" % self)
   ArtistRelationFactory.save(self)
コード例 #15
0
ファイル: factories.py プロジェクト: pscn/ads
 def load(input):
   for i in range(len(Factories.__lib)):
     Logger.debug(u"Factories.load(): loading %s" % i)
     k = pickle.load(input)
     if k != i:
       # FIXME: we should die here
       Logger.error(u"Factories.load(): key mismatch %d vs %d" % (i, k))
     Factories.__lib[k].setstate(pickle.load(input))
コード例 #16
0
ファイル: dos.py プロジェクト: yynil/OpenBazaar-Server
class BanScore(object):

    def __init__(self, peer_ip, multiplexer, ban_time=86400):
        self.peer_ip = peer_ip
        self.multiplexer = multiplexer
        self.ban_time = ban_time
        self.scores = {
            PING: 0,
            STUN: 0,
            STORE: 0,
            INV: 0,
            VALUES: 0,
        }
        self.scoring_loop = task.LoopingCall(self.adjust_scores)
        self.scoring_loop.start(30)
        self.log = Logger(system=self)

    def process_message(self, message):
        if message.command == PING:
            self.scores[PING] += 1
            if self.scores[PING] > 4:
                self.ban(PING)
        elif message.command == STUN:
            self.scores[STUN] += 1
            if self.scores[STUN] > 1:
                self.ban(STUN)
        elif message.command == HOLE_PUNCH:
            if self.peer_ip[0] != SEED_NODE[0] or self.peer_ip[0] != SEED_NODE_TESTNET[0]:
                self.ban(HOLE_PUNCH)
        elif message.command == STORE:
            args = tuple(message.arguments)
            for arg in args:
                self.scores[STORE] += len(arg)
            if self.scores[STORE] > 1000000:
                self.ban(STORE)
        elif message.command == INV:
            self.scores[INV] += 30
            if self.scores[INV] > 120:
                self.ban(INV)
        elif message.command == VALUES:
            self.scores[VALUES] += 30
            if self.scores[VALUES] > 120:
                self.ban(VALUES)

    def ban(self, message_type):
        self.log.warning("Banned %s. Reason: too many %s messages." %
                         (self.peer_ip[0], Command.Name(message_type)))
        self.multiplexer.ban_ip(self.peer_ip[0])
        self.multiplexer[self.peer_ip].shutdown()
        reactor.callLater(self.ban_time, self.multiplexer.remove_ip_ban, self.peer_ip[0])

    def adjust_scores(self):
        for k, v in self.scores.items():
            if v > 0:
                if k == STORE:
                    self.scores[k] = v - 350
                else:
                    self.scores[k] = v - 1
コード例 #17
0
 def on_server_added_act(self, data):
     Logger.log("Подключен сервер '{ip}'".format(ip = data['ip']))
     self.servers[data['id']] = {
         'ip': data['ip'],
         'sent': 0,
         'last_index': 0,
         'color': QColor.fromHsv(random.randint(0, 359), 220, 190),
     }
     self.tableView_client.add_row(data['ip'])
コード例 #18
0
ファイル: discovery.py プロジェクト: cpacia/pybitcoin
def dns_discovery(testnet=False):
    log = Logger(system="Discovery")
    addrs = []
    for seed in TESTNET3_SEEDS if testnet else MAINNET_SEEDS:
        answers = dns.resolver.query(seed)
        for addr in answers:
            addrs.append((str(addr), 18333 if testnet else 8333))
    log.info("DNS discovery returned %s peers" % len(addrs))
    return addrs
コード例 #19
0
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

        if not os.path.exists("DOWNLOADS_PATH"):
            os.makedirs("DOWNLOADS_PATH")
        if not os.path.exists("TORRENTS_PATH"):
            os.makedirs("TORRENTS_PATH")

        r = self.geometry()
        r.moveCenter(QApplication.desktop().availableGeometry().center())
        self.setGeometry(r)

        if ("DO_LOGIN"):
            self.show_login_form()

        self.actions = {
            PACKAGE_SENT_ACT: self.on_package_sent_act,
            PACKAGE_RECEIVED_ACT: self.on_package_received_act,
            SERVER_ADDED_ACT: self.on_server_added_act,
            CLIENT_ADDED_ACT: self.on_client_added_act,
            SERVER_REMOVED_ACT: self.on_server_removed_act,
            CLIENT_REMOVED_ACT: self.on_client_removed_act,
            ANSWER_ACT: self.on_answer_act,
            FILE_RECEIVED_ACT: self.on_file_received_act,
        }

        self.cli_thread = Thread(CLI_SOCK_PATH)
        self.srv_thread = Thread(SRV_SOCK_PATH)

        self.cli_thread.msg_came.connect(self.handle_cli_backend_message)
        self.srv_thread.msg_came.connect(self.handle_srv_backend_message)
        
        self.cli_thread.error_came.connect(self.handle_cli_error)
        self.srv_thread.error_came.connect(self.handle_srv_error)

        self.cli_thread.start()
        self.srv_thread.start()

        self.tableView_main.rowSelected.connect(self.on_main_table_row_changed)
        self.tableView_main.rowDoubleClicked.connect(self.on_row_double_clicked)

        self.timer = QTimer(self)
        self.connect(self.timer, SIGNAL("timeout()"), self.on_timer)
        self.timer.start(100)

        self.timer_iteration = 0

        self.transmissions = {}
        self.expected_response = 0

        Logger.log("Старт приложения")

        if ("DAEMONIZE"):
            self.run_daemons()

        self.load_torrents()
コード例 #20
0
ファイル: collect.py プロジェクト: wangzhaojiang/CMP
def data_tran(message):
    connection = pika.BlockingConnection(pika.ConnectionParameters(host="127.0.0.1"))
    channel = connection.channel()

    channel.exchange_declare(exchange="logs", type="fanout")

    channel.basic_publish(exchange="logs", routing_key="", body=message)
    print " [x] Sent %r" % (message,)
    Logger.info("Collect & Send message to master")
    connection.close()
コード例 #21
0
ファイル: channel.py プロジェクト: westlicht/omxone
    def __init__(self, name, input, output, channel):
        Logger.debug("[%s] Initializing channel: input='%s', output='%s, channel=%d" % (name, input, output, channel))
        self.name = name
        self.input = MidiEngine.get_input_by_name(input)
        self.output = MidiEngine.get_output_by_name(output)
        self.channel = channel
        self.receive = None

        if self.input:
            self.input.receive = self.__receive
コード例 #22
0
 def on_actionStart_transmission_triggered(self):
     key = self.tableView_main.current_row
     if not key:
         return
     s = self.transmissions[key]
     if not s['finished']:
         Logger.log("Отправлен запрос на получение файла '{name}'" \
             .format(name = s['filename']))
         self.cli_thread.send_message({'action': START_TRM_ACT,
             'hsum': key, 'filename': s['filename'], 'filesize': str(s['filesize'])})
コード例 #23
0
ファイル: fx_select.py プロジェクト: westlicht/omxone
 def __switch(self, fx):
     self.__state[fx] = not self.__state[fx]
     Logger.debug("Switching state of fx%d to %d" % (fx, self.__state[fx]))
     
     if self.__state[fx]:
         value = 127
     else:
         value = 0
         
     self.send_ctrl(['fx1', 'fx2', 'fx3', 'fx4'][fx], value)
     self.set_led(['led1', 'led2', 'led3', 'led4'][fx], self.__state[fx])
コード例 #24
0
 def on_actionCreate_transmission_triggered(self):
     fname = QFileDialog.getOpenFileName(self, 'Open file to create a torrent', '~')
     if fname:
         hsum = hashlib.md5(open(fname, "rb").read()).hexdigest()
         if not os.path.exists("TORRENTS_PATH/" + hsum):
             self.create_torrent_file(ntpath.basename(fname), os.path.getsize(fname), hsum, fname)
             self.load_torrent(fname = "TORRENTS_PATH/" + hsum)
         else:
             Logger.log("Торрент файл {0} уже существует".format(fname))
             QMessageBox.information(self, 'Ошибка',
                 'Торрент файл уже существует', QMessageBox.Ok)
コード例 #25
0
    def __getattr__(self, item):
        Logger.info(item)
        item = re.split('_', item)
        module_name, mthd = item[0], '_'.join(item[1:])

        class_name = module_name.capitalize() + 'Controller'

        module = __import__('controllers.%s' % module_name)
        instance = getattr(getattr(module, module_name), class_name)()

        return getattr(instance, mthd)
コード例 #26
0
 def on_file_received_act(self, data):
     s = data['hsum']
     struct = self.transmissions[s]
     Logger.log("Файл '{name}' был полностью получен".format(name = struct['filename']))
     self.create_torrent_file(struct['filename'], struct['filesize'],
         data['hsum'], "DOWNLOADS_PATH/" + struct['filename'])
     self.load_torrent(fname = "TORRENTS_PATH/" + s, add_row = False)
     struct['sent'] = -1
     struct['active'] = 0
     struct['finished'] = 1
     self.on_main_table_row_changed(s)
コード例 #27
0
 def create_torrent_file(self, filename, filesize, hsum, fname = None):
     if type(filesize) != str:
         filesize = str(filesize)
     struct = {'filesize': filesize, 'hsum': hsum, 'filename': filename}
     if fname:
         struct['default_path'] = fname
     fname = "TORRENTS_PATH/" + hsum
     Logger.log("Был создан торрент файл для файла '{name}'" \
             .format(name = filename))
     with open(fname, 'wb') as f:
         pickle.dump(struct, f, 0)
コード例 #28
0
ファイル: track.py プロジェクト: pscn/ads
 def update(self, play, factor=1.0):
   if self.ratingref.playfactor == 0.0 and self.ratingref.skipfactor == 0.0:
     new = True
     old_rating = 0.5
   else:
     new = False
     old_rating = self.rating
   self.ratingref = self.ratingref.update(play, factor)
   self.rating = self.ratingref.rating
   self.artist.update((self.rating - old_rating), new, factor)
   Logger.info(u"updated rating: %s" % self)
コード例 #29
0
class SMTPNotification(object):
    """
    A class for sending SMTP notifications
    """

    def __init__(self, db):
        self.db = db
        self.server = 'localhost:25'
        self.sender = 'OpenBazaar'
        self.recipient = ''
        self.username = None
        self.password = None

        self.log = Logger(system=self)

        self.get_smtp_settings()

    def get_smtp_settings(self):
        settings = self.db.settings.get()
        self.server = settings[15]
        self.sender = settings[16]
        self.recipient = settings[17]
        self.username = settings[18]
        self.password = settings[19]

    def send(self, subject, body):

        settings = self.db.settings.get()
        is_enabled = True if settings[14] == 1 else False

        if is_enabled:
            # Construct MIME message
            msg = MIMEMultipart('alternative')
            msg['Subject'] = subject
            msg['From'] = self.sender
            msg['To'] = self.recipient

            html_body = MIMEText(body, 'html')
            msg.attach(html_body)

            try:
                server = smtplib.SMTP(self.server)
                server.starttls()

                # Authenticate if username/password set
                if self.username and self.password:
                    server.login(self.username, self.password)

                server.sendmail(self.sender, self.recipient, msg.as_string())
            except SMTPAuthenticationError as e:
                self.log.error(e)
                print e

            server.quit()
コード例 #30
0
class HeartbeatFactory(protocol.ClientFactory):
    def __init__(self, libbitcoin_client):
        self.libbitcoin_client = libbitcoin_client
        self.log = Logger(system=self)

    def buildProtocol(self, addr):
        self.protocol = HeartbeatProtocol(self.libbitcoin_client)
        return self.protocol

    def clientConnectionFailed(self, connector, reason):
        self.libbitcoin_client.connected = False
        self.log.critical("Libbitcoin server offline")
コード例 #31
0
def main():
    # Create Model, Criterion and State
    model, criterion, state = create_model(args)
    print("=> Model and criterion are ready")

    # Create Dataloader
    if not args.test_only:
        train_loader = get_train_loader(args)
    val_loader = get_test_loader(args)
    print("=> Dataloaders are ready")

    # Create Logger
    logger = Logger(args, state)
    print("=> Logger is ready")  # Create Trainer
    trainer = Trainer(args, model, criterion, logger)
    print("=> Trainer is ready")

    if args.test_only:
        test_summary = trainer.test(0, val_loader)
        print("- Test:  Acc %6.3f " % (test_summary['acc']))
    else:
        start_epoch = logger.state['epoch'] + 1
        print("=> Start training")
        # test_summary = trainer.test(0, val_loader)

        for epoch in range(start_epoch, args.n_epochs + 1):
            train_summary = trainer.train(epoch, train_loader)
            test_summary = trainer.test(epoch, val_loader)

            logger.record(epoch, train_summary, test_summary, model)

        logger.final_print()
コード例 #32
0
    def __init__(self):
        # author database:
        self.connection = db.Connection(config.authordb["host"],
                                        config.authordb["db"],
                                        config.authordb["user"],
                                        config.authordb["password"],
                                        config.authordb["schema"])
        # Creates any missing tables that are expected to exist in the application database.
        # Does NOT verify whether the current columns are accurate.
        with self.connection.db.cursor() as cursor:
            helpers.Make_tables(cursor)

        # PROD DATABASE with article info:
        self.PROD = db.Connection(config.rxdb["host"], config.rxdb["db"],
                                  config.rxdb["user"], config.rxdb["password"],
                                  config.rxdb["schema"])

        # Requests HTML configuration
        self.session = HTMLSession(mock_browser=False)
        self.session.headers['User-Agent'] = config.user_agent

        self.log = Logger()
コード例 #33
0
ファイル: mysql.py プロジェクト: penger/bl_spark
 def init_task_sceduling(self, taskinfo, run_date):
     try:
         con = MySQLdb.connect(host=self.mysqlinfo["host"],
                               port=string.atoi(self.mysqlinfo["port"]),
                               user=self.mysqlinfo["user"],
                               passwd=self.mysqlinfo["passwd"],
                               db=self.mysqlinfo["dbname"])
         cursor = con.cursor()
         for task_id in taskinfo.keys():
             values = [
                 run_date,
                 task_id,
                 False,
                 True,
             ]
             sql = "insert into task_sceduling(run_date,task_id,runstatus,ifnormal ) values(%s,%s,%s,%s)"
             cursor.execute(sql, values)
         con.commit()
     except Error, e:
         Logger.error("mysql error insert_task_log" + e.args[0].__str__() +
                      ":" + e.args[1].__str__())
         print "mysql error insert_task_log %d: %s" % (e.args[0], e.args[1])
コード例 #34
0
 def __init__(self, browser):
     self._logger = Logger(__file__)
     # baidu search website
     self.baidu = "https://www.baidu.com"
     #cap["phantomjs.page.settings.loadImages"] = True
     #cap["phantomjs.page.settings.disk-cache"] = True
     #cap["phantomjs.page.settings.userAgent"] = "Mozilla/5.0 (X11; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"
     #self.browser = webdriver.PhantomJS(desired_capabilities=cap)
     #self.browser = webdriver.PhantomJS()
     if not browser:
         ## get the Firefox profile object
         profile = FirefoxProfile()
         ## Disable CSS. We can not disable this then get images from baidu
         #profile.set_preference('permissions.default.stylesheet', 2)
         ## Disable images
         #profile.set_preference('permissions.default.image', 2)
         ## Disable Flash
         profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so',
                                'false')
         self.browser = webdriver.Firefox(profile)
     else:
         self.browser = browser
コード例 #35
0
    def __init__(self, console, otp_key, parent=None):
        super(Form, self).__init__(parent)
        self.console = console
        self.otp_key = otp_key

        self.logger = Logger('Rule', Logger.INFO)

        self.setWindowTitle("PttOTP 驗證視窗")
        self.setWindowIcon(util.load_icon(config.icon_small))
        self.setWindowFlags(Qt.WindowStaysOnTopHint)

        rule_text = '''
1. 請開啟 google authenticator 掃描以下圖片
2. 於下方輸入框輸入 google authenticator 顯示的驗證碼
'''
        rule_text = rule_text.strip()

        layout = QVBoxLayout()
        for rule_line in rule_text.split('\n'):
            layout.addWidget(QLabel(rule_line))

        label = QLabel()
        label.setPixmap(QPixmap('./temp.png'))
        layout.addWidget(label)

        button_layout = QHBoxLayout()

        self.otp_edit = QLineEdit()
        button_layout.addWidget(self.otp_edit)

        ok_button = QPushButton("驗證")
        ok_button.clicked.connect(self.click_verify)
        button_layout.addWidget(ok_button)

        layout.addLayout(button_layout)

        self.setLayout(layout)

        self.ok = False
コード例 #36
0
    def keyClose(all_list):
        '''一键全关只能关闭电脑,然后关闭投影,间隔4分钟后,最后继电器'''
        Logger.getLog().logger.info("关闭电脑")
        commonData.SENDSIG.sendText("关闭电脑")
        for c in commonData.TERM_DICT['comput']:
            # checksocket = socket.socket()
            # checksocket.settimeout(2)
            # intstatus = checksocket.connect_ex((c['IP'], 5800))
            # if(intstatus == 0 or intstatus==10061):
            #     ComputService.wake_upfromJd(c['ip2'],c['port2'],c['addr'],c['road'])
            ComputService.shutComput(c['IP'], 'shutdown -s -f -t 00')
            time.sleep(0.4)

        time.sleep(30)
        Logger.getLog().logger.info("关闭投影")
        commonData.SENDSIG.sendText("关闭投影")
        for i in range(0, 2):
            for ty in commonData.TERM_DICT['touying']:
                Logger.getLog().logger.info('关闭投影机' + ty['IP'])
                # TouyingService.comm(ty['IP'], 4196,bytes.fromhex('02 50 4F 46 03'))
                TouyingService.Pjlink(ty['IP'], b'%1POWR 0\r')
                time.sleep(0.5)
        Logger.getLog().logger.info('等待50秒')
        time.sleep(300)
        Logger.getLog().logger.info("关闭电源")
        commonData.SENDSIG.sendText("关闭电源")
        for i in range(0, 2):
            for d in range(len(commonData.JD_DICT['devices'])):
                dev = commonData.JD_DICT['devices'][d]['device']
                for t in dev:
                    ip = t['ip']
                    port = t['port']
                    addr = t['addr']
                    road = t['road'] - 1
                    if not (addr == 1 and road == 7):
                        cmod = JDService.getSingleCommand(
                            hex(addr), hex(road), '0000')
                        JDService.sendCommand(ip, port, cmod)
                        time.sleep(0.3)
コード例 #37
0
 def __init__(self, embedding_len, lr=1., momentum=.9, batch_size=50,
              window_size=2, epoch=1, use_cuda=False, embedding_path=None,
              verbose=1, tensorboard=False, wordlist_path=None, log_folder='runs',
              use_nce=False):
   super(CBOW, self).__init__()
   self.embedding_len = embedding_len
   self.lr = lr
   self.momentum = momentum
   self.epoch = epoch
   self.use_cuda = use_cuda
   self.embedding_path = embedding_path
   self.logger = Logger(verbose)
   self.tensorboard = tensorboard
   self.use_nce = use_nce
   if self.tensorboard:
     from tensorboardX import SummaryWriter
     self.writer = SummaryWriter(log_folder)
   self.loader = Loader(for_embedding=True, window_size=window_size,
                        batch_size=batch_size, logger=self.logger,
                        wordlist_file=wordlist_path)
   self.vocab_size = self.loader.get_vocab_size()
   self._build_model()
コード例 #38
0
ファイル: main.py プロジェクト: zhangzhenfeng/OfficeAnalysis
def sub(name, i):
    """
    绩效扣分
    :param name: 姓名
    :param i: 扣分数量
    :return:
    """
    name = nameToPin.get(name)
    obj = None
    count_path = os.path.join(base_path, src_path, "count.txt")
    with open(count_path, 'r') as f:
        obj = f.readline()
        if obj is not None and obj != "":
            obj = eval(obj)
            if obj.get(name) is not None and obj.get(name) != "":
                obj[name] = obj[name] - i
            else:
                Logger().logger.error("[-]count.txt文件内容没有[%s]" % (name))
        else:
            Logger().logger.error("[-]count.txt文件内容为空")
    with open(count_path, 'w') as f:
        f.write(str(obj))
コード例 #39
0
class Form(QDialog):
    def __init__(self, console, otp_key, parent=None):
        super(Form, self).__init__(parent)
        self.console = console
        self.otp_key = otp_key

        self.logger = Logger('Rule', Logger.INFO)

        self.setWindowTitle("PttOTP 驗證視窗")
        self.setWindowIcon(util.load_icon(config.icon_small))
        self.setWindowFlags(Qt.WindowStaysOnTopHint)

        rule_text = '''
1. 請開啟 google authenticator 掃描以下圖片
2. 於下方輸入框輸入 google authenticator 顯示的驗證碼
'''
        rule_text = rule_text.strip()

        layout = QVBoxLayout()
        for rule_line in rule_text.split('\n'):
            layout.addWidget(QLabel(rule_line))

        label = QLabel()
        label.setPixmap(QPixmap('./temp.png'))
        layout.addWidget(label)

        button_layout = QHBoxLayout()

        self.otp_edit = QLineEdit()
        button_layout.addWidget(self.otp_edit)

        ok_button = QPushButton("驗證")
        ok_button.clicked.connect(self.click_verify)
        button_layout.addWidget(ok_button)

        layout.addLayout(button_layout)

        self.setLayout(layout)

        self.ok = False

    def click_verify(self):
        self.logger.show(Logger.INFO, '驗證')

        current_otp = self.otp_edit.text()
        self.logger.show_value(Logger.INFO, 'Current otp', current_otp)

        otp_obj = pyotp.TOTP(self.otp_key)
        otp = otp_obj.now()

        self.logger.show_value(Logger.INFO, 'otp', otp)

        if current_otp != otp:
            self.console.system_alert('驗證失敗請再輸入一次')
            return

        self.ok = True
        self.close()
        self.console.system_alert('驗證成功')
コード例 #40
0
ファイル: listeners.py プロジェクト: td00/OpenBazaar-Server
class MessageListenerImpl(object):
    implements(MessageListener)

    def __init__(self, web_socket_factory, database):
        self.ws = web_socket_factory
        self.db = database
        self.log = Logger(system=self)

    def notify(self, plaintext, signature):
        try:
            success = self.db.messages.save_message(
                plaintext.sender_guid.encode("hex"), plaintext.handle,
                plaintext.pubkey, plaintext.subject,
                PlaintextMessage.Type.Name(plaintext.type), plaintext.message,
                plaintext.timestamp, plaintext.avatar_hash, signature, False)

            if plaintext.subject != "":
                self.db.purchases.update_unread(plaintext.subject)
                self.db.sales.update_unread(plaintext.subject)
                self.db.cases.update_unread(plaintext.subject)

            if success:
                message_json = {
                    "message": {
                        "sender": plaintext.sender_guid.encode("hex"),
                        "subject": plaintext.subject,
                        "message_type":
                        PlaintextMessage.Type.Name(plaintext.type),
                        "message": plaintext.message,
                        "timestamp": plaintext.timestamp,
                        "avatar_hash": plaintext.avatar_hash.encode("hex"),
                        "public_key": plaintext.pubkey.encode("hex")
                    }
                }
                if plaintext.handle:
                    message_json["message"]["handle"] = plaintext.handle
                self.ws.push(json.dumps(sanitize_html(message_json), indent=4))
        except Exception as e:
            self.log.error('Market.Listener.notify Exception: %s' % e)
コード例 #41
0
    def _risk_assessment_helper(self,
                                outer_k,
                                experiment_class,
                                exp_path,
                                debug=False,
                                other=None):

        dataset_getter = DatasetGetter(outer_k)

        best_config = self.model_selector.model_selection(
            dataset_getter, experiment_class, exp_path, self.model_configs,
            debug, other)

        # Retrain with the best configuration and test
        experiment = experiment_class(best_config['config'], exp_path)

        # Set up a log file for this experiment (run in a separate process)

        logger = Logger.Logger(str(
            os.path.join(experiment.exp_path, 'experiment.log')),
                               mode='a')

        dataset_getter = DatasetGetter(outer_k)
        dataset_getter.set_inner_k(None)  # needs to stay None

        training_scores, test_scores = [], []

        # Mitigate bad random initializations
        for i in range(3):
            training_score, test_score = experiment.run_test(
                dataset_getter, logger, other)
            print(
                f'Final training run {i + 1}: {training_score}, {test_score}')

            training_scores.append(training_score)
            test_scores.append(test_score)

        training_score = sum(training_scores) / 3
        test_score = sum(test_scores) / 3

        logger.log('End of Outer fold. TR score: ' + str(training_score) +
                   ' TS score: ' + str(test_score))

        with open(os.path.join(exp_path, self._OUTER_RESULTS_FILENAME),
                  'w') as fp:
            json.dump(
                {
                    'best_config': best_config,
                    'OUTER_TR': training_score,
                    'OUTER_TS': test_score
                }, fp)
コード例 #42
0
ファイル: TyService.py プロジェクト: longjiegit/control1.2
 def comm(ip, port, command):
     try:
         Logger.getLog().logger.info('发送指令到投影' + str(command, 'utf-8'))
         commonData.SENDSIG.sendText('发送指令到投影' + str(command, 'utf-8'))
         s = socket.socket()
         s.settimeout(3)
         port = int(port)
         s.connect((ip, port))
         s.send(command)
         re = s.recv(1024)
         Logger.getLog().logger.info('投影返回结果')
         Logger.getLog().logger.info(str(re, 'utf-8'))
         commonData.RECSIG.sendText('投影返回结果' + str(re, 'utf-8'))
     except Exception as e:
         Logger.getLog().logger.error(e)
コード例 #43
0
ファイル: sarsa.py プロジェクト: ciupakabra/minigrid
    def __init__(self, observation_space, action_space, from_dir=None):
        self.action_space = action_space

        self.memory = deque(maxlen=FLAGS.memory_size)
        self.exploration_rate = FLAGS.exploration_max

        self.reward_logs = Logger()
        self.reward_update_logs = Logger()

        if from_dir is None:
            self.model = tf.keras.models.Sequential([
                InputLayer(input_shape=observation_space),
                Flatten(),
                Dense(128,
                      input_shape=(observation_space, ),
                      activation="relu"),
                Dense(128, activation="relu"),
                Dense(self.action_space, activation="linear")
            ])
        else:
            with open(os.path.join(from_dir, "architecture.json"),
                      "r") as json_file:
                model_json = json_file.read()

            self.model = tf.keras.models.model_from_json(model_json)
            self.model.load_weights(os.path.join(from_dir, "weights.h5"))

        self.model.compile(loss="mse",
                           optimizer=tf.keras.optimizers.Adam(
                               FLAGS.learning_rate))

        self.target_model = tf.keras.models.clone_model(self.model)

        self.steps = 0

        self.last_transition = None

        self.model.summary()
コード例 #44
0
 def decompression(file_path, target_path):
     """
     解压rar、zip压缩包到指定文件夹
     :param file_path: 压缩包路径
     :param target_path: 解压路径
     :return:
     """
     if file_path.endswith(".rar"):
         try:
             # mode的值只能为'r'
             rar_file = rarfile.RarFile(file_path, mode='r')
             # 得到压缩包里所有的文件
             rar_list = rar_file.namelist()
             for f in rar_list:
                 rar_file.extract(f, target_path)
         except Exception as e:
             Logger('decompress.log', level='error').logger.error(e)
     elif file_path.endswith(".zip"):
         # zip_file = zipfile.ZipFile(file_path, mode='r')
         with zipfile.ZipFile(file_path, mode='r') as zip_file:
             # 得到解压包所有文件
             zip_list = zip_file.namelist()
             for f in zip_list:
                 # 循环解压文件到指定目录
                 zip_file.extract(f, target_path)
             for file in os.listdir(target_path):
                 if file in zip_list:
                     try:
                         new_file = file.encode('cp437').decode('gbk')
                         print(new_file)
                         try:
                             os.rename(join(target_path, file), join(target_path, new_file))
                         except Exception as e:
                             os.remove(join(target_path, file))
                     except Exception as e:
                         Logger('decompress.log', level='error').logger.error(e)
     else:
         pass
コード例 #45
0
ファイル: config.py プロジェクト: kzailac/argo-egi-connectors
    def __init__(self, caller, confpath=None, **kwargs):
        self.optional = dict()

        self.logger = Logger(str(self.__class__))
        self._filename = '/etc/argo-egi-connectors/global.conf' if not confpath else confpath
        self._checkpath = kwargs['checkpath'] if 'checkpath' in kwargs.keys(
        ) else False

        self.optional.update(self._lowercase_dict(self.conf_ams))
        self.optional.update(self._lowercase_dict(self.conf_auth))

        self.shared_secopts = self._merge_dict(self.conf_ams,
                                               self.conf_general,
                                               self.conf_auth, self.conf_conn,
                                               self.conf_state)
        self.secopts = {
            'topology-gocdb-connector.py':
            self._merge_dict(self.shared_secopts, self.conf_topo_schemas,
                             self.conf_topo_output),
            'downtimes-gocdb-connector.py':
            self._merge_dict(self.shared_secopts, self.conf_downtimes_schemas,
                             self.conf_downtimes_output),
            'weights-vapor-connector.py':
            self._merge_dict(self.shared_secopts, self.conf_weights_schemas,
                             self.conf_weights_output),
            'poem-connector.py':
            self._merge_dict(self.shared_secopts, self.conf_poem_schemas,
                             self.conf_poem_output, self.conf_poem_prefilter),
            'prefilter-egi.py':
            self._merge_dict(self.conf_general, self.conf_prefilter_output,
                             self.conf_prefilter_schemas,
                             self.conf_prefilter_prefilter)
        }

        if caller:
            self.caller_secopts = self.secopts[os.path.basename(caller)]
        else:
            self.caller_secopts = self.shared_secopts
コード例 #46
0
class Selenium:

    vmrunPath = None
    log = Logger()

    def __init__(self):
        """Also this doc should be in shown in library doc."""

    def start(self):

        p = subprocess.Popen(
            ['java', '-jar', 'java/selenium-server-standalone-2.21.0.jar'],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
コード例 #47
0
ファイル: mysql.py プロジェクト: penger/bl_spark
    def select_task_impala_load_ready(self):
        try:
            con = MySQLdb.connect(host=self.mysqlinfo["host"],
                                  port=string.atoi(self.mysqlinfo["port"]),
                                  user=self.mysqlinfo["user"],
                                  passwd=self.mysqlinfo["passwd"],
                                  db=self.mysqlinfo["dbname"])
            cursor = con.cursor()
            task_str = self.select_task_impala_taskname_list()
            sql = "select task_name,node_num,batch_id,h_db_name,h_table_name,h_table_part,i_db_name,i_table_name,i_table_part,insert_type,count(1) as num \
                    from task_impala_load where status='READY' \
		    and task_name in {task_str} \
                    group by task_name,node_num,batch_id,h_db_name,h_table_name,h_table_part,i_db_name,i_table_name,i_table_part,insert_type having num=node_num;"

            sql = sql.replace("{task_str}", task_str)
            cursor.execute(sql)
            results = cursor.fetchall()
            return results
        except Error, e:
            Logger.error("mysql error select_task_impala_load_ready" +
                         e.args[0].__str__() + ":" + e.args[1].__str__())
            print "mysql error select_task_impala_load_ready%d: %s" % (
                e.args[0], e.args[1])
コード例 #48
0
ファイル: add_log.py プロジェクト: opsnoober/InstallAQCP
    def _wrapper(func):
        logger = Logger(logname=os.path.join(os.path.dirname(basedir), 'logs',
                                             'install.log'),
                        loglevel=1,
                        logger=str(loggername)).getlog()

        def __wrapper():
            logger.info("+++++++++++++ begin %s +++++++++++++++++++++++" %
                        loggername)
            func()
            logger.info("+++++++++++++ finish %s +++++++++++++++++++++++" %
                        loggername)

        return __wrapper
コード例 #49
0
class Main(object):
    def __init__(self):
        self._logger = Logger().get_logger()
        self._logger.info("Main class create")

    def __del__(self):
        self._logger.info("Main class release")

    def run(self):
        self._logger.info("Main thread running")

        main_launcher = LaunchMainInterface()
        main_launcher.start()
        main_launcher.join()

        self._logger.info("Main thread run done")
コード例 #50
0
    def _get_historical_data(self):
        base_url = 'https://finance.naver.com/item/sise_day.nhn?code=%s&page=%d'
        for event, code in self.event_code_dict.iteritems():
            ratio = 0
            page = 1
            datas = []
            while True:
                url = base_url % (code, page)
                page_datas, last_page = self._get_page_datas(url)
                datas.extend(page_datas)
                page += 1

                if page > last_page:
                    break

                new_ratio = int((float(page) / last_page) * 100)
                if new_ratio - ratio > 1:
                    ratio = new_ratio
                    Logger.info('proc: %s %d%%' % (event, ratio))

            self._write_event_data(event, datas)

        self._make_done_file()
コード例 #51
0
 def __init__(self,
              embedding_path,
              lr=.01,
              momentum=.9,
              batch_size=50,
              epoch=1000,
              use_cuda=False,
              verbose=1,
              tensorboard=False):
     super(SentimentClassification, self).__init__()
     self.embedding_path = embedding_path
     self.lr = lr
     self.momentum = momentum
     self.batch_size = batch_size
     self.epoch = epoch
     self.use_cuda = use_cuda
     self.logger = Logger(verbose)
     self.tensorboard = tensorboard
     self._build_model()
     self.loader = Loader(for_embedding=False, logger=self.logger)
     if self.tensorboard:
         from tensorboardX import SummaryWriter
         self.writer = SummaryWriter('logs')
コード例 #52
0
    def keyClose(self):
        '''一键全关只能关闭电脑,然后关闭投影,间隔4分钟后,最后继电器'''
        self.sendText.append(':'.join(
            (time.strftime('%Y-%m-%d %H:%M:%S'), '电脑全关')))

        for c in self.cpt:
            checksocket = socket.socket()
            checksocket.settimeout(2)
            intstatus = checksocket.connect_ex((c['IP'], 5800))
            print(intstatus)
            if (intstatus == 0 or intstatus == 10061):
                cs.shutComputfromJd(c['ip2'], c['port2'], c['addr'], c['road'])
            time.sleep(0.3)
        self.sendText.append(':'.join(
            (time.strftime('%Y-%m-%d %H:%M:%S'), '投影全关')))
        for ty in self.t:
            Logger.getLog().logger.info('关闭投影机' + ty['IP'])
            self.comm(ty['IP'], 4196, bytes.fromhex('02 50 4F 46 03'))
        Logger.getLog().logger.info('等待50秒')
        time.sleep(50)
        self.sendText.append(':'.join(
            (time.strftime('%Y-%m-%d %H:%M:%S'), '继电器全关')))
        for d in range(len(self.devices)):
            dev = self.devices[d]['device']
            for t in dev:
                ip = t['ip']
                port = t['port']
                addr = t['addr']
                road = t['road'] - 1
                if (addr == 2) and (road == 2):
                    print("no close")
                elif (addr == 1) and (road == 4):
                    print("no close")
                else:
                    cmod = self.getSingleCommand(hex(addr), hex(road), '0000')
                    self.sendCommand(ip, port, cmod, '继电器全关')
                    time.sleep(0.3)
コード例 #53
0
ファイル: bluetooth.py プロジェクト: zenghui0-0/tempfile
    def __init__(self):
        """
        constructor
        """
        IWatchdog.__init__(self)
        self._eventloop = GObject.MainLoop()
        self.logger = Logger("bt.watchdog")
        self.logger.debug("initializing Bluetooth watchdog")
        self._events = WatchdogEvent(self.logger)
        self.bt_powered = False
        # global objects about DBus
        self._bus = dbus.SystemBus()
        self._dbus_obj = self._bus.get_object(Consts.Bt.SVC_NAME,
                                              Consts.DBus.OBJ_MNGR_PATH)
        try:
            self._dbus_manager = dbus.Interface(self._dbus_obj,
                                                Consts.DBus.OBJ_MNGR)
        except dbus.DBusException as E:
            self.logger.error("%s" % (E.get_dbus_message()))
            self._dbus_manager = None
        except:
            self.logger.error(
                "Unknown error while retrieving DBUS object manager")
            self._dbus_manager = None
        self._dbus_adapter_obj = None

        self._adapter = [Adapter(self.logger)]
        # identify it as the used adapter
        self._used_adapter = self._adapter[0]
        self.logger.debug("registering adapter")
        EXPOSED_OBJECTS["adapter"] = self._used_adapter
        self.logger.debug(EXPOSED_OBJECTS)

        # devices is expected to be a @MAC -> Device object mapping
        self._devices = self._used_adapter.get_devices()
        # map_devices is expected to be a path -> @MAC mapping
        self._map_devices = {}
コード例 #54
0
class SynorepUtil(object):
    '''
    分词后进行同义词替换工具
    '''

    Log = Logger.getLogger(__name__)

    @staticmethod
    def synorepm(resource, customdic, synodic):
        seq_re = None
        try:
            assert isinstance(resource, str) and os.path.exists(customdic) \
                and os.path.exists(synodic)
            seq_pr = SynorepUtil().__partictxt(resource, customdic)
            seq_re = SynorepUtil().__repsynor(seq_pr, synodic)
        except BaseException as e:
            SynorepUtil.Log.error(e)
            raise ParticException(e)
        else:
            return seq_re

    def __partictxt(self, resource, customdic):
        # 添加自定义词典
        try:
            jieba.load_userdict(customdic)
            seg_list = jieba.cut(resource, cut_all=False)
        except BaseException as e:
            SynorepUtil.Log.error(e)
            raise ParticException(e)
        else:
            return "/".join(seg_list)

    def __repsynor(self, resource, synodic):
        # 同义词替换返回替换后的字符串
        combine_dict = {}
        for w in open(synodic, "r", encoding='utf-8'):
            w_1 = w.strip().split("\t")
            num = len(w_1)
            for i in range(0, num):
                combine_dict[w_1[i]] = w_1[0]

        seg_list_2 = ""
        for word in resource.split("/"):
            if word in combine_dict:
                word = combine_dict[word]
                seg_list_2 += word
            else:
                seg_list_2 += word
        return seg_list_2
コード例 #55
0
    def __init__(self, console):
        super(Form, self).__init__(None)

        self.logger = Logger('OTP', Logger.INFO)

        self.console = console
        console.system_alert = self.system_alert

        self.icon = util.load_icon(config.icon_small)
        self.setIcon(self.icon)

        self.set_menu(False)

        self.setToolTip('Ptt OTP')

        self.show()

        self.activated.connect(self.icon_clicked)

        self.login_success = False
        self.in_process = False
        self.about_window_form = None

        self.show_login_form()
コード例 #56
0
ファイル: main.py プロジェクト: BellaFirst/GACNN2
def GA_CNN2_MAIN():
    args = parser()
    cfg = Config.fromfile(args.config)
    log = Logger(cfg.PARA.utils_paths.log_path + 'GA_CNN' + '_log_new.txt', level='info')


    # GA_POP
    gacnn = GA_CNN2(args, cfg, log)
    # gacnn.Init_Chrom()#初始化种群
    gacnn.Evolution() #开始进化
    gacnn.Plot_Save() #绘图并保存数据

    # Rand_POP
    randcnn = Rand_POP_CNN2(args, cfg, log)
    randcnn.Evolution()
コード例 #57
0
class Server(object):
    def __init__(self, kserver, signing_key, database):
        """
        A high level class for sending direct, market messages to other nodes.
        A node will need one of these to participate in buying and selling.
        Should be initialized after the Kademlia server.
        """
        self.kserver = kserver
        self.signing_key = signing_key
        self.router = kserver.protocol.router
        self.db = database
        self.log = Logger(system=self)
        self.protocol = MarketProtocol(kserver.node, self.router, signing_key,
                                       database)

        # TODO: we need a loop here that republishes keywords when they are about to expire

        # TODO: we also need a loop here to delete expiring contract (if they are set to expire)

    def querySeed(self, list_seed_pubkey):
        """
        Query an HTTP seed for known vendors and save the vendors to the db.

        Args:
            Receives a list of one or more tuples Example [(seed, pubkey)]
            seed: A `string` consisting of "ip:port" or "hostname:port"
            pubkey: The hex encoded public key to verify the signature on the response
        """

        for sp in list_seed_pubkey:
            seed, pubkey = sp
            try:
                self.log.debug("querying %s for vendors" % seed)
                c = httplib.HTTPConnection(seed)
                c.request("GET", "/?type=vendors")
                response = c.getresponse()
                self.log.debug("Http response from %s: %s, %s" %
                               (seed, response.status, response.reason))
                data = response.read()
                reread_data = data.decode("zlib")
                proto = peers.PeerSeeds()
                proto.ParseFromString(reread_data)
                verify_key = nacl.signing.VerifyKey(
                    pubkey, encoder=nacl.encoding.HexEncoder)
                verify_key.verify("".join(proto.serializedNode),
                                  proto.signature)
                v = self.db.VendorStore()
                for peer in proto.serializedNode:
                    try:
                        n = objects.Node()
                        n.ParseFromString(peer)
                        v.save_vendor(n.guid.encode("hex"), peer)
                    except Exception:
                        pass
            except Exception, e:
                self.log.error("failed to query seed: %s" % str(e))
コード例 #58
0
    def classify(self, sentence, lower_bounder=None, upper_bounder=None):
        """
        ML model wrapper. No time regular expression involved!
        input: sentence - type string
        return label
        """
        if self.log is None:
            self.log = Logger(self.__class__.__name__,
                              level=ENV.MODEL_LOG_LEVEL.value).logger
        sentence = jieba.cut(sentence, cut_all=False)
        sentence = ' '.join(sentence)
        matrix = self.tfidf.transform([sentence])
        self.log.debug(
            'In transfered tfidf, the number of words in vocalbulary is: {}'.
            format(len(matrix.data)))
        result = np.vstack((self.svc.predict_proba(matrix),
                            self.logistic.predict_proba(matrix),
                            self.nb.predict_proba(matrix)))

        av_pred = np.mean(result, axis=0)
        max_pred = np.max(av_pred, axis=0)
        max_arg = np.argmax(av_pred)
        response = None
        label = max_arg

        dictionary = {
            'label': label,
            'pred_prob': result,
            'av_pred': av_pred,
            'other_response': response
        }
        self.log.debug('Final Pred label is: {}'.format(dictionary['label']))
        self.log.debug('svc,logistic,nb result:\n {}'.format(
            dictionary['pred_prob']))
        self.log.debug('ave result:\n {}'.format(dictionary['av_pred']))
        return dictionary
コード例 #59
0
class Usable_proxy(Public):
    def __init__(self):
        self.logger = Logger('filer_log')
        conn = MongoClient(constants.conn_db)
        self.db = conn[constants.proxy_db]
        self.my_set = self.db[constants.filer_colletion]

    # 获取测试代理ip
    @property
    def get_proxy(self):
        # 从数据库中获取数据
        my_colletion = self.db[constants.proxy_db]
        self.logger.dbuglog('从数据库中读取到数据:%s' % my_colletion.count())
        proxy_list = my_colletion.find()
        for val in proxy_list:
            yield val

    # 测试方法
    def check(self, proxy):
        url = 'https://baidu.com'
        try:
            proxies = {
                'http': 'http://%s' % proxy,
                'https': 'http://%s' % proxy
            }
            res = requests.get(url,
                               headers=self.headers,
                               proxies=proxies,
                               timeout=0.5)
            self.logger.dbuglog(proxy + ' -----该代理ip可用,响应速度在0.5s内')
            return True
        except Exception as e:
            return False

    def save(self):

        for val in self.get_proxy:
            try:
                judge = self.check(val['ip_port'])
                if judge:
                    # 保存到数据库
                    self.my_set.save(val)
                    self.logger.dbuglog('存储该优质代理ip')
            except Exception as e:
                self.logger.errlog(val['ip_port'] + '---------该优质代理ip保存失败')
                pass
コード例 #60
0
ファイル: rpcudp.py プロジェクト: hoffmabc/OpenBazaar-Server
    def __init__(self,
                 proto,
                 router,
                 waitTimeout=5,
                 noisy=True,
                 testnet=False):
        """
        Args:
            proto: A protobuf `Node` object containing info about this node.
            router: A `RoutingTable` object from dht.routing. Implies a `network.Server` object
                    must be started first.
            waitTimeout: Consider it a connetion failure if no response
                    within this time window.
            noisy: Whether or not to log the output for this class.
            testnet: The network parameters to use.

        """
        self.proto = proto
        self.router = router
        self._waitTimeout = waitTimeout
        self._outstanding = {}
        self.noisy = noisy
        self.testnet = testnet
        self.log = Logger(system=self)