Last active
September 13, 2019 13:10
-
-
Save dwurf/2f1f174fc7c5b84e2f9a6bc5bff6af95 to your computer and use it in GitHub Desktop.
Query Valve servers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: run | |
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) | |
pth := $(notdir $(patsubst %/,%,$(dir $(mkfile_path)))) | |
run: $(pth)/venv | |
$(pth)/venv/bin/python3 $(pth)/query.py | |
$(pth)/venv: | |
python3 -m venv $(pth)/venv | |
$(pth)/venv/bin/pip install -r $(pth)/requirements.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Query a valve server""" | |
import argparse | |
import valve.source | |
import valve.source.a2s | |
import valve.source.master_server | |
def server_deets(address, port): | |
"""Get server details""" | |
with valve.source.a2s.ServerQuerier((address, port)) as server: | |
info = server.info() | |
players = server.players() | |
return dict(info), players | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='describe a server') | |
parser.add_argument('attributes', metavar='attributes', help='Server attributes to display', nargs="*") | |
parser.add_argument('-s', '--server', default='localhost',) | |
parser.add_argument('-p', '--port', default=27015,) | |
args = parser.parse_args() | |
try: | |
deets, players = server_deets(args.server, args.port) | |
except valve.source.NoResponseError: | |
print("Server {}:{} timed out!".format(args.server, args.port)) | |
else: | |
if deets and len(args.attributes) == 0: | |
for k in deets.keys(): | |
print(k, deets[k]) | |
elif deets and len(args.attributes) == 1: | |
key = args.attributes[0] | |
print(deets.get(key, 'unrecognised attribute')) | |
elif deets and len(args.attributes) > 1: | |
for key in args.attributes: | |
if key in deets: | |
print(key, deets[key]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python-valve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment