def gameserver_status(): try: gameserver_address = (app.config['GAMESERVER_IP'], app.config['GAMESERVER_PORT']) with a2s.ServerQuerier(gameserver_address, timeout=1) as server: info = server.info() gameserver = { 'map': info.values['map'], 'slots': f'{info.values["player_count"]}/{info.values["max_players"]}', 'status': True } except: gameserver = {'status': False} return gameserver
def get_server_info(server): """Gets server info using python-valve.""" # set port to 27960 if no port address = (server.split(":") + [27960])[:2] try: address[1] = int(address[1]) server = a2s.ServerQuerier(address, 1) # 1 second timeout info = server.get_info() return info['server_name'], [ info["player_count"], info["max_players"] ], info["map"] except ValueError: return "Error: Invalid port", [] except socket.gaierror: return "Error: Invalid/nonexistent address", [] except a2s.NoResponseError: return "Error: Timed out", []
def info_server(): try: app = current_app server = a2s.ServerQuerier( (app.config['SERVER_IP'], app.config['SERVER_PORT'])) players = server.get_players() info = server.get_info() rules = server.get_rules() info.connect = '{}:{}'.format(server.host, rules['rules']['HOSTPORT']) except a2s.NoResponseError: players = {} info = {} rules = {} return render_template('info_server.html', players=players, info=info, rules=rules)
def ark_users_online(): q = a2s.ServerQuerier((ARK_SERVER_ADDR, ARK_SERVER_PORT)) return [p['name'] for p in q.get_players()['players'] if p['name']]
def loop(servers, path_to_steam): """ Runs through the server list, seeds servers as necessary. """ current_server = servers[0] current_thread = None while True: try: # Grab the server object from Valve's master query server, or timeout server = volvo.ServerQuerier(current_server, timeout=5.0) player_count = server.get_info()["player_count"] # Do we need to seed this server? if player_count < 2: print("_" * 100) print("%s: Joining %s:%d" % (current_time(), current_server[0], current_server[1])) print("%s: Waiting for players for %s seconds" % (current_time(), stale_server_time)) current_thread = launch_game(current_server, path_to_steam) # Start a timer timer_start = time.clock() # Check every 20 seconds to see if at least one human has joined while player_count < 2: try: time.sleep(20) server = volvo.ServerQuerier(current_server, timeout=5.0) new_player_count = server.get_info()["player_count"] # Check if the new_player_count is actually a number due to Volvo shenanigans. if isinstance(new_player_count, int): player_count = new_player_count # After X seconds, just disconnect and move on to prevent the server becoming 'stale' if time.clock() - timer_start > stale_server_time: print( "%s: %d seconds elapsed, cycling to next server." % (current_time(), stale_server_time)) increment_stale() decrease_seed() break # If client fails to connect move on if player_count == 0: print("%s: Connection lost, moving on." % (current_time())) increment_timeout() decrease_seed() break except volvo.NoResponseError: print("%s: Server timed out." % (current_time())) print("%s: Server seeded: %s:%d (%d players)" % (current_time(), current_server[0], current_server[1], player_count)) increment_seed() print("_" * 100) print( "Totals: Servers seeded: %s / Connection fails: %d / Stale servers: %d" % (seeded, timedout, stale)) destroy_instances() restart_steam() # This server is already seeded. else: print("%s: Server %s:%d was already seeded. (%d players)" % (current_time(), current_server[0], current_server[1], player_count)) # Cycle to the next server in the list servers.append(servers.pop(0)) current_server = servers[0] except volvo.NoResponseError: print( "%s: Master server request timed out! Volvo pls. Server: %s:%d" % (current_time(), current_server[0], current_server[1])) # Make sure that when the script is canceled it closes running instances of L4D2 except (KeyboardInterrupt, SystemExit): print("Exit: Closing all instances of L4D2.") destroy_instances() sys.exit(0) time.sleep(5)
def get_server_info(port): try: return a2s.ServerQuerier(("localhost", port), 1).get_info() except a2s.NoResponseError: return
#!/usr/bin/python3 import valve.source.a2s as a2 s=a2.ServerQuerier("atlantishq.de",27015) print(s.players()["player_count"]) s.close()