Example #1
0
def view(key):
    if Config.getBoolValue('TIME_IT_ENABLED'):
        message = "{0} => {1}s".format(key, TimeIt.timings[key])
        if Config.getBoolValue('TIME_IT_CONSOLE'):
            print(message)
        if Config.getBoolValue('TIME_IT_FILE'):
            Log.debug(message)
 def count(self, subDatabase=None):
     try:
         self.open(subDatabase)
         with self.db.begin() as db:
             return db.stat(db=self.subDb)['entries']
     except IOError:
         Log.error('Unable to count database entries')
     finally:
         self.close()
 def remove(self, key, subDatabase=None):
     try:
         self.open(subDatabase)
         with self.db.begin(write=True, db=self.subDb) as db:
             db.delete(key)
     except IOError:
         Log.error('Unable to remove record using key: %s' % key)
     finally:
         self.close()
 def get(self, key, subDatabase=None):
     try:
         self.open(subDatabase)
         with self.db.begin(db=self.subDb) as db:
             return db.get(key)
     except IOError:
         Log.error('Unable to get record using key: %s' % key)
     finally:
         self.close()
 def set(self, key, value, subDatabase=None):
     try:
         self.open(subDatabase)
         with self.db.begin(write=True) as db:
             db.put(key, value, db=self.subDb)
     except IOError:
         Log.error('Unable to set record using key: %s value: %s' %
                   (key, value))
     finally:
         self.close()
Example #6
0
def dump():
    try:
        PersistentStorage.persistent.open()
        with PersistentStorage.persistent.db.begin() as storage:
            for keyBytes, valueBytes in storage.cursor(
                    db=PersistentStorage.persistent.subDb):
                print('key', keyBytes, 'value', valueBytes)
    except IOError:
        Log.error('Unable to read persistent storage')
    finally:
        PersistentStorage.persistent.close()
Example #7
0
def getHostname(includePort=True):
    try:
        if Config.getBoolValue("REQUIRE_HOSTNAME"):
            hostname = socket.gethostname()
        else:
            hostname = getIpAddress(False)
        if includePort:
            hostname += "{0}{1}".format(':', getSocketPort())
        return hostname
    except IOError:
        Log.error('Unable to get hostname')
    return None
Example #8
0
def sendDataByUDP(host, port, data):
    try:
        with socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
                           socket.IPPROTO_UDP) as sock:
            sock.settimeout(Config.getIntValue("CONNECTION_TIMEOUT"))
            data = DataType.serialize(data)
            sock.sendto(data, (host, port))
            return sock.recvfrom(
                Config.getIntValue("SOCKET_RECEIVE_BUFFER_SIZE"))
    except IOError:
        Log.error('Unable to send data by UDP to host: %s port: %s data: %s' %
                  (host, port, data))
    return None, None
Example #9
0
def getPeers():
    peers = []
    try:
        Peers.peers.open()
        with Peers.peers.db.begin() as tx:
            cursor = tx.cursor(db=Peers.peers.subDb)
            while cursor.next():
                peerBytes = cursor.value()
                peer = getPeerFromBytes(peerBytes)
                if peer != None:
                    peers.append(peer)
    except IOError:
        Log.error('Unable to open peers database: %s' %
                  Config.getValue("PEERS_DB"))
    finally:
        Peers.peers.close()
    return peers
Example #10
0
def send(url, data=None, doBasicAuth=False, headers=None):
    try:
        if not url.startswith("http"):
            url = "http://" + url
        if data != None:
            data = DataType.serialize(data)
        httpHeaders = {}
        if doBasicAuth:
            httpHeaders.update({'Authorization': getBasicAuth()})
        if headers != None:
            httpHeaders.update(headers)
        req = Request(url, data, httpHeaders)
        response = urlopen(req,
                           timeout=Config.getIntValue("CONNECTION_TIMEOUT"))
        return response.read()
    except IOError:
        Log.error('Unable to send request to url: %s' % url)
    return None
Example #11
0
def getIpAddress(includePort=True):
    ipAddress = None
    try:
        hostname = socket.gethostname()
        ipAddress = socket.gethostbyname(hostname)
    except IOError:
        Log.error('Unable to get ip address')
    if ipAddress == None or ipAddress.startswith("127"):
        with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
            try:
                sock.connect(("8.8.8.8", 53))
                sockname = sock.getsockname()
                ipAddress = sockname[0]
            except IOError:
                Log.error('Unable to get ip address via fallback.')
    if includePort:
        ipAddress += "{0}{1}".format(':', getSocketPort())
    return ipAddress
Example #12
0
def _sendData(host, data, hasPayload):
    try:
        if ':' in host:
            host = host.split(':')
            host[1] = DataType.asInt(host[1])
            host = tuple(host)
        else:
            host = (host, getSocketPort())
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
            sock.connect(host)
            sock.settimeout(Config.getIntValue("CONNECTION_TIMEOUT"))
            data = DataType.serialize(data)
            dataLen = len(data)
            dataLen = DataType.intToBytes(
                dataLen, Config.getIntValue("SOCKET_HEADER_BUFFER_SIZE"))
            sock.sendall(dataLen)
            sock.sendall(data)
            if hasPayload:
                payload = getSocketPayload(sock)
                return RLP.decode(payload)
    except IOError:
        Log.error('Unable to send data to host: %s data: %s' % (host, data))
    return None