def main(): try: query = MinecraftQuery("server.url.here", 25565, timeout=10, retries=0) server_data = query.get_rules() except socket.error as e: print "socket exception caught:", e.message print "Server is down or unreachable." sys.exit(1) print(server_data['motd']) print(server_data['version']) NumPlayers = "Players " NumPlayers += str(server_data['numplayers']) + "/" NumPlayers += str(server_data['maxplayers']) + " :" print(NumPlayers) Players = server_data['players'] for player in Players: print("\t"+player) sys.exit(0)
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 ServerStatsPlayer(): try: query = MinecraftQuery(str(e.get()), int(e2.get())) status = query.get_status() po.config(text = "Player Count: " + str(status['numplayers'])) except socket.error as ee: po.config(text = "Player Count: Cannot connect to server!")
def getStatus(): query = MinecraftQuery("dev.eddiezane.com", 25565) rules = query.get_rules() print rules print rules['players'] retVal = '' for name in rules['players']: retVal = retVal + name + ', ' return render_template('results.html', names=rules['players'], rule=rules['motd'], number=rules['numplayers'])
def main(): parser = ArgumentParser( description="Monitor a Minecraft server for activity" ) parser.add_argument( "host", help="hostname of server to monitor" ) parser.add_argument( "-n", "--notify", help="trigger a Mac OS X notification (using terminal-notifier)", default=False, action='store_true' ) parser.add_argument( "-l", "--led", help="toggle an LED for activity", default=False, action='store_true' ) parser.add_argument( "-sd", "--serial-dev", help="Serial device to use for LED", default="/dev/tty.usbserial-A8007qt3" ) parser.add_argument( "-sb", "--serial-baud", help="Serial baudrate to use for LED", default=115200 ) options = parser.parse_args() notifyState = None if options.led: arduino = serial.Serial( options.serial_dev, options.serial_baud, stopbits=serial.STOPBITS_TWO ) while 1: try: query = MinecraftQuery(options.host, 25565, timeout=10, retries=3) response = query.get_rules() print_players( response ) if options.notify: notifyState = notify( response, notifyState ) if options.led: ledState = led( response, arduino ) except socket.error as e: print "socket exception caught:", e.message time.sleep(10)
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 serverQuery(): host = request.args.get("host") port = request.args.get("port") query_type = request.args.get("type") query_type = str(query_type) host = str(host) port = int(port) query = MinecraftQuery(host, port) if query_type == "simple": basic_info = query.get_status() return jsonify(basic_info) else: info = query.get_rules() return jsonify(info)
def poll(server): print 'checking on %s:%s...'%(server['host'], server['port']) config = parse_config(server['path']); try: query = MinecraftQuery(server['host'], server['port'], timeout=TIMEOUT, retries=RETRIES) server_data = query.get_rules() except socket.error as e: print 'socket exception caught:', e.message print 'Server is down or unreachable.' server_data = {} server_data['id'] = server['id'] return server_data
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
def query(self): query = MinecraftQuery(self.host, self.query_port) full_info = query.get_rules() query.socket.close() return full_info
def check_minecraft(address, port=25565, timeout=3): try: query = MinecraftQuery(address, port, timeout=timeout) return query.get_status() except: return None
#!/usr/bin/env python2 from minecraft_query import MinecraftQuery query = MinecraftQuery("localhost", 25565) data = query.get_rules() string =str((data['map']))+"|" string +=str((data['motd']))+"|" string +=str((data['hostport']))+"|" string +=str((data['numplayers']))+"|" string +=str((data['gametype']))+"|" string +=str((data['players']))+"|" string +=str((data['version']))+"|" string +=str((data['maxplayers']))+"|" string +=str((data['plugins']))+"|" string +=str((data['raw_plugins']))+"|" string +=str((data['game_id']))+"|" string +=str((data['hostip']))+"|" string +=str((data['software'])) print string
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=3, help='retry timeout in seconds [10]') args = parser.parse_args() def assertcrit(cond, text): if not cond: exit('CRITICAL',text) def exit(state, text): print '%s - %s' % (state, text) sys.exit({ 'OK': 0, 'WARNING': 1, 'CRITICAL': 2}.get(state, 3)) try: query = MinecraftQuery(args.host, args.port, timeout=args.timeout, retries=args.retries) server_data = query.get_rules() except socket.error as e: exit('CRITICAL', 'Socket error: '+e.message) try: version = server_data['version'] motd = server_data['motd'] players = server_data['numplayers'] playermax = server_data['maxplayers'] except: exit('CRITICAL', 'Invalid data') state = 'OK' playerfraction = players*1.0/playermax
def returnStatus(): query = MinecraftQuery("mc.voltaire.sh", 25565) basic_status = query.get_status() all_status = query.get_rules() vanillaPlayers = MinecraftQuery("mc.voltaire.sh",25565).get_rules()['players'] return render_template('derp.html', avatars=vanillaPlayers, numplayers=basic_status['numplayers'], maxplayers=basic_status['maxplayers'])
sys.path.append(os.path.join(SCRIPT_DIR, 'mcstatus')) from minecraft_query import MinecraftQuery from query_config import QueryConfig as config do_db_init = False if not os.path.exists(config.db_file): do_db_init = True conn = sqlite3.connect(config.db_file) c = conn.cursor() if do_db_init: c.execute('CREATE TABLE players_online (host text, player_name text, online_at text)') for host, port in config.servers: try: mc_query = MinecraftQuery(host, port, timeout=config.timeout) server_data = mc_query.get_rules() for player in server_data['players']: c.execute('INSERT INTO players_online VALUES ("%s:%d", "%s", datetime("now"))' % (host, port, player)) #print "%s:%d %s" % (host, port, server_data['players']) except socket.error as e: print >>sys.stderr, '%s: Minecraft server "%s:%d" is unavailable' % (os.path.realpath(sys.argv[0]), host, port) conn.commit() conn.close()
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)