So I have script as part of my backend for a game. On occasion, I may have a user who is not registered but is still able to connect to my server.
What this script does is that it first looks through all active connections line by line, some of which may include an id. So if it finds the id, the server then compares it to a list of registered users. After looping once through all registered users and it finds no matches, the server kicks the player. Otherwise if a match is found, the server continues on to the next line in the connection list.
Is there a better way to write this? I am open to feedback. It works generally, but I find it janky to use found variable to make the determination whether there is a match or not. By default, it is false so unless there is a match, the server will kick the user.
def kickUnknown():
connLines = os.popen('sudo docker exec game --traffic').readlines()
for connLine in connLines:
conn = re.findall("\d+[abc]\d+", connLine)
if len(conn) > 0:
certLines = os.popen('sudo docker exec game --regClients').readlines()
found = False
for certLine in certLines:
if certLine.__contains__(conn[0]):
print('match b/w db and conn list')
found = True
if found == True:
# conn found in registered db, do nothing
elif found == False:
id = re.findall("(?<=')[^']+(?=')", connLine)
if len(id) > 0:
res = os.popen("sudo docker exec game --kick --id " + "'" + id[0] + "'")
print('kicked unregistered user:' + conn[0])