本文整理汇总了Python中kademlia.network.Server.saveStateRegularly方法的典型用法代码示例。如果您正苦于以下问题:Python Server.saveStateRegularly方法的具体用法?Python Server.saveStateRegularly怎么用?Python Server.saveStateRegularly使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kademlia.network.Server
的用法示例。
在下文中一共展示了Server.saveStateRegularly方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect
# 需要导入模块: from kademlia.network import Server [as 别名]
# 或者: from kademlia.network.Server import saveStateRegularly [as 别名]
def connect(self, port):
if(self._app_deployer):
from twisted.application import service, internet
from twisted.python.log import ILogObserver
from twisted.internet import reactor, task
import sys, os
sys.path.append(os.path.dirname(__file__))
from kademlia.network import Server
from kademlia import log
application = service.Application("kademlia")
application.setComponent(ILogObserver, log.FileLogObserver(sys.stdout, log.INFO).emit)
if os.path.isfile('cache.pickle'):
kserver = Server.loadState('cache.pickle')
else:
kserver = Server()
kserver.bootstrap([("127.0.0.1", port)])
kserver.saveStateRegularly('cache.pickle', 10)
server = internet.UDPServer(8468, kserver.protocol)
server.setServiceParent(application)
else:
self._node.listen(port)
self._node.bootstrap([("127.0.0.1",port)]).addCallback(self.bootStrapDone)
示例2: DHTResolver
# 需要导入模块: from kademlia.network import Server [as 别名]
# 或者: from kademlia.network.Server import saveStateRegularly [as 别名]
class DHTResolver(object):
def __init__(self, config, bootstrapNeighbors):
self.config = config
self.log = Logger(system=self)
if os.path.isfile(config['dht.state.cache']):
self.kserver = Server.loadState(config['dht.state.cache'])
else:
self.kserver = Server()
self.kserver.bootstrap(bootstrapNeighbors)
self.kserver.saveStateRegularly(config['dht.state.cache'], 60)
def getProtocol(self):
return self.kserver.protocol
def getPublicKey(self, keyId):
"""
Get the public key from the network, and return only if the key is
the one that matches the keyId based on hash.
"""
def verify(key):
if key is not None and PublicKey(key).getKeyId() == keyId:
return key
return None
self.log.debug("Getting key text for key id %s" % keyId)
return self.kserver.get(keyId).addCallback(verify)
def resolve(self, keyId):
def parse(locations):
self.log.debug("Locations for %s: %s" % (keyId, locations))
results = []
if locations is None or locations == "":
return results
for location in locations.split(','):
host, port = location.split(':')
results.append((host, int(port)))
return results
d = self.kserver.get("%s-location" % keyId)
return d.addCallback(parse)
def announceLocation(self, myKeyId, myPublicKey):
def announce(ips):
ips = self.localAddresses() + ips
ipports = map(lambda ip: "%s:%i" % (ip, self.config['s2s.port']), ips)
return self.kserver.set("%s-location" % myKeyId, ",".join(ipports))
d = self.kserver.set(myKeyId, str(myPublicKey))
d.addCallback(lambda _: self.kserver.inetVisibleIP())
return d.addCallback(announce)
def localAddresses(self):
result = []
for iface in netifaces.interfaces():
addys = netifaces.ifaddresses(iface).get(netifaces.AF_INET)
result += [ addy['addr'] for addy in (addys or []) if addy['addr'] != '127.0.0.1' ]
return result
示例3: DHTResolver
# 需要导入模块: from kademlia.network import Server [as 别名]
# 或者: from kademlia.network.Server import saveStateRegularly [as 别名]
class DHTResolver(object):
def __init__(self, config, bootstrapNeighbors):
if os.path.isfile(config['dht.state.cache']):
self.kserver = Server.loadState(config['dht.state.cache'])
else:
self.kserver = Server()
self.kserver.bootstrap(bootstrapNeighbors)
self.kserver.saveStateRegularly(config['dht.state.cache'], 60)
def getProtocol(self):
return self.kserver.protocol
def resolve(self, keyId):
return self.kserver.get(keyId)
def announceLocation(self, myKeyId):
def announce(ips):
if len(ips) == 0:
return defer.succeed(False)
return self.kserver.set(myKeyId, ips[0])
d = self.kserver.inetVisibleIP()
return d.addCallback(announce)
示例4: Server
# 需要导入模块: from kademlia.network import Server [as 别名]
# 或者: from kademlia.network.Server import saveStateRegularly [as 别名]
import tornado.ioloop
import tornado.web
import hashlib
from os.path import join, getsize, exists
import mutagen
import pickle
import time
from twisted.internet import reactor
from kademlia.network import Server
if os.path.isfile('self.cache.pickle'):
kserver = Server.loadState('self.cache.pickle')
else:
kserver = Server()
kserver.bootstrap([("185.97.32.250", 8468)])
kserver.saveStateRegularly('self.cache.pickle', 10)
kserver.listen(8468)
class FileServer(tornado.web.RequestHandler):
def initialize(self, cache):
self.cache=cache
def get(self, arg):
filename = self.cache[arg]
with open(filename,"rb") as fp:
self.set_header("Content-Type", "audio/mpeg")
self.set_header('content-length',os.stat(fp.fileno()).st_size)
# self.set_header('Content-Disposition',' inline; filename="{}"'.format(os.path.basename(fp.name)))
self.write(fp.read())
self.finish()
示例5: Server
# 需要导入模块: from kademlia.network import Server [as 别名]
# 或者: from kademlia.network.Server import saveStateRegularly [as 别名]
from twisted.application import service, internet
from twisted.python.log import ILogObserver
from twisted.internet import reactor, task
import sys, os
#sys.path.append(os.path.dirname(__file__))
from kademlia.network import Server
from kademlia import log
application = service.Application("tau")
application.setComponent(ILogObserver, log.FileLogObserver(sys.stdout, log.INFO).emit)
kad_cache = 'cache.pickle'
if os.path.isfile(kad_cache):
kserver = Server.loadState(kad_cache)
else:
kserver = Server()
kserver.bootstrap([("1.2.3.4", 8468)])
kserver.saveStateRegularly(kad_cache, 10)
server = internet.UDPServer(8468, kserver.protocol)
server.setServiceParent(application)
reactor.run()
示例6: Server
# 需要导入模块: from kademlia.network import Server [as 别名]
# 或者: from kademlia.network.Server import saveStateRegularly [as 别名]
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from twisted.internet import reactor
from kademlia.network import Server
if os.path.isfile("cache.pickle"):
kserver = Server.loadState("cache.pickle")
else:
kserver = Server()
kserver.bootstrap([("1.2.3.4", 8468)])
kserver.saveStateRegularly("cache.pickle", 10)
kserver.listen(8468)
reactor.run()