Skip to content

Instantly share code, notes, and snippets.

@wh1te909
Created October 15, 2018 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wh1te909/82f35f77e19d1cadef7fed2b8fd7dcf8 to your computer and use it in GitHub Desktop.
Save wh1te909/82f35f77e19d1cadef7fed2b8fd7dcf8 to your computer and use it in GitHub Desktop.
srcds check if server empty
import valve.rcon
import re
import signal
import time
ip = "10.0.0.1"
port = 27015
password = "rconpassword"
timeout = 20 # seconds
class Timeout():
class Timeout(Exception): pass
def __init__(self, sec):
self.sec = sec
def __enter__(self):
signal.signal(signal.SIGALRM, self.raise_timeout)
signal.alarm(self.sec)
def __exit__(self, *args):
signal.alarm(0)
def raise_timeout(self, *args):
raise Timeout.Timeout()
def isEmpty(address, password):
with valve.rcon.RCON(address, password) as rcon:
response = rcon.execute("status")
response_text = response.body.decode('ascii', 'ignore')
for line in response_text.splitlines():
if "players :" in line:
for words in ['0 humans']:
if re.search(r'\b' + words + r'\b', line):
return True
else:
return False
else:
pass
if __name__ == "__main__":
try:
with Timeout(timeout):
if isEmpty((ip, port), password):
print("server is empty")
# do stuff here
#
else:
print("server is NOT empty")
except Timeout.Timeout:
print("Error: Timeout after {} seconds".format(timeout))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment