def main(): parser = ArgumentParser( description="Query status of Minecraft multiplayer server", epilog="Exit status: 0 if the server can be reached, otherwise nonzero." ) parser.add_argument("host", help="target hostname") parser.add_argument( "-q", "--quiet", action='store_true', default=False, help='don\'t print anything, just check if the server is running') parser.add_argument("-p", "--port", type=int, default=25565, help='UDP port of server\'s "query" service [25565]') parser.add_argument("-r", "--retries", type=int, default=3, help='retry query at most this number of times [3]') parser.add_argument("-t", "--timeout", type=int, default=10, help='retry timeout in seconds [10]') options = parser.parse_args() try: query = MinecraftQuery(options.host, options.port, timeout=options.timeout, retries=options.retries) server_data = query.get_rules() except socket.error as e: if not options.quiet: print "socket exception caught:", e.message print "Server is down or unreachable." sys.exit(1) if not options.quiet: print "Server response data:" pprint(server_data) sys.exit(0)
def parse(self, parent): # get server s = [s for s in self.servers if s in self.query] if len(s): # make query query = MinecraftQuery(self.servers[ s[0] ]["host"], self.servers[ s[0] ]["port"]) basic_status = query.get_status() self.resp["text"] = "%s out of %s players: %s" % (basic_status["numplayers"], basic_status["maxplayers"], ", ".join(basic_status["players"])) self.resp["status"] = STATUS_OK else: # Error! self.resp["status"] = STATUS_ERR # return self.resp["type"] = "mcstats" return self.resp
try: port = int(request.args.get('port', default=25565)) except Exception, e: port = 25565 test = (host == 'test.mc.glassmoon.ru') if test: return jsonify(players=['doge', 'such', 'wow', 'diamonds']) result = None if redis: key = "%s:%s" % (host, str(port)) result = redis.hgetall(key) if not result: try: result = MinecraftQuery(host, port).get_rules() except Exception as e: result = {'message': 'No response from the Minecraft server'} if redis: redis.hmset(key, result) redis.pexpire(key, 30000) # 30s return jsonify(**result) if __name__ == '__main__': app.run(debug=options.debug, port=options.port)
def check_minecraft(address, port=25565, timeout=3): try: query = MinecraftQuery(address, port, timeout=timeout) return query.get_status() except: return None