当前位置: 首页>>代码示例>>Python>>正文


Python API.registerEvent方法代码示例

本文整理汇总了Python中api.API.registerEvent方法的典型用法代码示例。如果您正苦于以下问题:Python API.registerEvent方法的具体用法?Python API.registerEvent怎么用?Python API.registerEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在api.API的用法示例。


在下文中一共展示了API.registerEvent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from api import API [as 别名]
# 或者: from api.API import registerEvent [as 别名]
class Web:
	def __init__(self, wrapper):
		self.wrapper = wrapper
		self.api = API(wrapper, "Web")
		self.log = log.PluginLog(self.wrapper.log, "Web")
		self.config = wrapper.config
		self.socket = False
		self.data = storage.Storage("web", self.log)
		if "keys" not in self.data: self.data["keys"] = []
		
		self.api.registerEvent("server.consoleMessage", self.onServerConsole)
		self.consoleScrollback = []
		self.loginAttempts = 0
		self.lastAttempt = 0
		self.disableLogins = 0
	def onServerConsole(self, payload):
		while len(self.consoleScrollback) > 30:
			del self.consoleScrollback[0]
		self.consoleScrollback.append(payload["message"])
	def makeKey(self):
		a = ""; z = "[email protected]-_"
		for i in range(32):
			a += z[random.randrange(0, len(z))]
#			a += chr(random.randrange(97, 122))
		self.data["keys"].append([a, time.time()])
		return a
	def validateKey(self, key):
		if time.time() - self.disableLogins < 2000: return False # Threshold for logins
		for i in self.data["keys"]:
			if i[0] == key and time.time() - i[1] < 604800: # Validate key and ensure it's under a week old
				self.loginAttempts = 0
				return True
		self.loginAttempts += 1
		if self.loginAttempts > 10 and time.time() - self.lastAttempt < 30:
			self.disableLogins = time.time()
		self.lastAttempt = time.time()
		return False
	def removeKey(self, key):
		for i,v in enumerate(self.data["keys"]):
			if v[0] == key:
				del self.data["keys"][i]
	def wrap(self):
		while not self.wrapper.halt:
			try:
				if self.bind():
					self.listen()
				else:
					self.log.error("Could not bind web to %s:%d - retrying in 5 seconds" % (self.config["Web"]["web-bind"], self.config["Web"]["web-port"]))
			except:
				for line in traceback.format_exc().split("\n"):
					self.log.error(line)
			time.sleep(5)
	def bind(self):
		if self.socket is not False:
			self.socket.close()
		try:
			self.socket = socket.socket()
			self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
			self.socket.bind((self.config["Web"]["web-bind"], self.config["Web"]["web-port"]))
			self.socket.listen(5)
			return True
		except:
			return False
	def listen(self):
		self.log.info("Web Interface bound to %s:%d" % (self.config["Web"]["web-bind"], self.config["Web"]["web-port"]))
		while not self.wrapper.halt:
			sock, addr = self.socket.accept()
#			self.log.debug("(WEB) Connection %s started" % str(addr))
			client = Client(self.wrapper, sock, addr, self)
			t = threading.Thread(target=client.wrap, args=())
			t.daemon = True
			t.start()
开发者ID:eristoddle,项目名称:minecraft-wrapper,代码行数:74,代码来源:web.py

示例2: __init__

# 需要导入模块: from api import API [as 别名]
# 或者: from api.API import registerEvent [as 别名]
class Web:
	def __init__(self, wrapper):
		self.wrapper = wrapper
		self.api = API(wrapper, "Web", internal=True)
		self.log = log.PluginLog(self.wrapper.log, "Web")
		self.config = wrapper.config
		self.socket = False
		self.data = storage.Storage("web", self.log)
		if "keys" not in self.data: self.data["keys"] = []
		#if not self.config["Web"]["web-password"] == None:
#			self.log.info("Changing web-mode password because web-password was changed in wrapper.properties")
#			self.data["password"] = md5.md5(self.config["Web"]["web-password"]).hexdigest()
#			self.config["Web"]["web-password"] = None
#			self.wrapper.configManager.save()
		
		self.api.registerEvent("server.consoleMessage", self.onServerConsole)
		self.api.registerEvent("player.message", self.onPlayerMessage)
		self.api.registerEvent("player.join", self.onPlayerJoin)
		self.api.registerEvent("player.leave", self.onPlayerLeave)
		self.api.registerEvent("irc.message", self.onChannelMessage)
		self.consoleScrollback = []
		self.chatScrollback = []
		self.memoryGraph = []
		self.loginAttempts = 0
		self.lastAttempt = 0
		self.disableLogins = 0
		
	#	t = threading.Thread(target=self.updateGraph, args=())
#		t.daemon = True
#		t.start()
	def onServerConsole(self, payload):
		while len(self.consoleScrollback) > 1000:
			try:
				del self.consoleScrollback[0]
			except: break
		self.consoleScrollback.append((time.time(), payload["message"]))
	def onPlayerMessage(self, payload):
		while len(self.chatScrollback) > 200:
			try:
				del self.chatScrollback[0]
			except: break
		self.chatScrollback.append((time.time(), {"type": "player", "payload": {"player": payload["player"].username, "message": payload["message"]}}))
	def onPlayerJoin(self, payload):
		while len(self.chatScrollback) > 200:
			try:
				del self.chatScrollback[0]
			except: break
		self.chatScrollback.append((time.time(), {"type": "playerJoin", "payload": {"player": payload["player"].username}}))
	def onPlayerLeave(self, payload):
		while len(self.chatScrollback) > 200:
			try: del self.chatScrollback[0]
			except: break
		self.chatScrollback.append((time.time(), {"type": "playerLeave", "payload": {"player": payload["player"].username}}))
	def onChannelMessage(self, payload):
		while len(self.chatScrollback) > 200:
			try: del self.chatScrollback[0]
			except: break
		self.chatScrollback.append((time.time(), {"type": "irc", "payload": payload}))
	def updateGraph(self):
		while not self.wrapper.halt:
			while len(self.memoryGraph) > 200:
				del self.memoryGraph[0]
			if self.wrapper.server.getMemoryUsage():
				self.memoryGraph.append([time.time(), self.wrapper.server.getMemoryUsage()])
			time.sleep(1)
	def checkLogin(self, password):
		if time.time() - self.disableLogins < 60: return False # Threshold for logins
		if password == self.wrapper.config["Web"]["web-password"]: return True
		self.loginAttempts += 1
		if self.loginAttempts > 10 and time.time() - self.lastAttempt < 60:
			self.disableLogins = time.time()
			self.log.warn("Disabled login attempts for one minute")
		self.lastAttempt = time.time()
	def makeKey(self, rememberMe):
		a = ""; z = "[email protected]-_"
		for i in range(64):
			a += z[random.randrange(0, len(z))]
#			a += chr(random.randrange(97, 122))
		self.data["keys"].append([a, time.time(), rememberMe])
		return a
	def validateKey(self, key):
		for i in self.data["keys"]:
			expireTime = 2592000
			if len(i) > 2:
				if i[2]: expireTime = 21600
			if i[0] == key and time.time() - i[1] < expireTime: # Validate key and ensure it's under a week old
				self.loginAttempts = 0
				return True
		return False
	def removeKey(self, key):
		for i,v in enumerate(self.data["keys"]):
			if v[0] == key:
				del self.data["keys"][i]
	def wrap(self):
		while not self.wrapper.halt:
			try:
				if self.bind():
					#cProfile.run("self.listen()", "cProfile-debug")
					self.listen()
				else:
#.........这里部分代码省略.........
开发者ID:gitter-badger,项目名称:minecraft-wrapper,代码行数:103,代码来源:web.py


注:本文中的api.API.registerEvent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。