Browse Source

Cleanup

- PEP-8: spaces should be used for indentation
- "is" should be used for comparing with "None"
- corrected indentation of "self.connected = False"
pull/1128/head
Marvin Lehmann 9 years ago
parent
commit
e4c09f6f8f
  1. 97
      lgsm/functions/gsquery.py

97
lgsm/functions/gsquery.py

@ -9,8 +9,9 @@ import optparse
import socket import socket
import sys import sys
class GameServer: class GameServer:
def __init__( self, options, arguments ): def __init__(self, options, arguments):
self.option = options self.option = options
self.argument = arguments self.argument = arguments
# #
@ -19,7 +20,6 @@ class GameServer:
# #
if self.option.engine == 'avalanche': if self.option.engine == 'avalanche':
self.query_prompt_string = b'\xFE\xFD\x09\x10\x20\x30\x40' self.query_prompt_string = b'\xFE\xFD\x09\x10\x20\x30\x40'
elif self.option.engine == 'goldsource': elif self.option.engine == 'goldsource':
self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0' self.query_prompt_string = b'\xFF\xFF\xFF\xFFTSource Engine Query\0'
elif self.option.engine == 'idtech3': elif self.option.engine == 'idtech3':
@ -44,87 +44,88 @@ class GameServer:
self.response = None self.response = None
self.sanity_checks() self.sanity_checks()
def fatal_error( self, error_message, error_code=1 ): def fatal_error(self, error_message, error_code=1):
sys.stderr.write( 'ERROR: ' + str(error_message) + '\n' ) sys.stderr.write('ERROR: ' + str(error_message) + '\n')
sys.exit( error_code ) sys.exit(error_code)
def exit_success( self, success_message='' ): def exit_success(self, success_message=''):
sys.stdout.write( 'OK: ' + str(success_message) + '\n' ) sys.stdout.write('OK: ' + str(success_message) + '\n')
sys.exit( 0 ) sys.exit(0)
def responding( self ): def responding(self):
# Connect. # Connect.
connection = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) connection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
connection.settimeout( self.server_response_timeout ) connection.settimeout(self.server_response_timeout)
try: try:
self.connected = connection.connect( ( self.option.address, int(self.option.port) ) ) self.connected = connection.connect((self.option.address, int(self.option.port)))
except socket.timeout: except socket.timeout:
self.fatal_error( 'Request timed out', 1 ) self.fatal_error('Request timed out', 1)
except: except:
self.fatal_error( 'Unable to connect', 1 ) self.fatal_error('Unable to connect', 1)
# Send. # Send.
connection.send( self.query_prompt_string ) connection.send(self.query_prompt_string)
# Receive. # Receive.
try: try:
self.response = connection.recv( self.default_buffer_length ) self.response = connection.recv(self.default_buffer_length)
except socket.error: except socket.error:
self.fatal_error( 'Unable to receive', 2 ) self.fatal_error('Unable to receive', 2)
connection.close() connection.close()
# Response. # Response.
if self.response == None: if self.response is None:
self.fatal_error( 'No response', 3 ) self.fatal_error('No response', 3)
if len( self.response ) < 10 : if len(self.response) < 10:
sys.exit( 'Short response.', 3 ) sys.exit('Short response.', 3)
else: else:
self.exit_success( str( self.response ) ) self.exit_success(str(self.response))
def sanity_checks( self ): def sanity_checks(self):
if not self.option.address: if not self.option.address:
self.fatal_error( 'No IPv4 address supplied.', 4 ) self.fatal_error('No IPv4 address supplied.', 4)
if not self.option.port: if not self.option.port:
self.fatal_error( 'No port supplied.', 4 ) self.fatal_error('No port supplied.', 4)
if __name__ == '__main__': if __name__ == '__main__':
parser = optparse.OptionParser( parser = optparse.OptionParser(
usage = 'usage: python %prog [options]', usage='usage: python %prog [options]',
version = '%prog 0.0.1' version='%prog 0.0.1'
) )
parser.add_option( parser.add_option(
'-a', '--address', '-a', '--address',
action = 'store', action='store',
dest = 'address', dest='address',
default = False, default=False,
help = 'The IPv4 address of the server.' help='The IPv4 address of the server.'
) )
parser.add_option( parser.add_option(
'-p', '--port', '-p', '--port',
action = 'store', action='store',
dest = 'port', dest='port',
default = False, default=False,
help = 'The IPv4 port of the server.' help='The IPv4 port of the server.'
) )
parser.add_option( parser.add_option(
'-e', '--engine', '-e', '--engine',
action = 'store', action='store',
dest = 'engine', dest='engine',
default = False, default=False,
help = 'Engine type: avalanche, goldsource, idtech3, realvirtuality, quakelive, refractor, spark, source, unity3d, unreal, unreal2.' help='Engine type: avalanche, goldsource, idtech3, realvirtuality, quakelive, refractor, spark, source, unity3d, unreal, unreal2.'
) )
parser.add_option( parser.add_option(
'-v', '--verbose', '-v', '--verbose',
action = 'store_true', action='store_true',
dest = 'verbose', dest='verbose',
default = False, default=False,
help = 'Display verbose output.' help='Display verbose output.'
) )
parser.add_option( parser.add_option(
'-d', '--debug', '-d', '--debug',
action = 'store_true', action='store_true',
dest = 'debug', dest='debug',
default = False, default=False,
help = 'Display debugging output.' help='Display debugging output.'
) )
options, arguments = parser.parse_args() options, arguments = parser.parse_args()
# #
server = GameServer( options, arguments ) server = GameServer(options, arguments)
server.responding() server.responding()

Loading…
Cancel
Save