Preface
The knowledge of the server has made me very confused. I believe there are a lot of Python developers and I have a similar experience. This article mainly introduces how to query CS Counter-Strike server information under Python, there is a need to refer to learning.
CS Counter Strike 1.5 version sample code
#!/bin/env python import urllib2, base64, SYS, getoptimport reimport socket def Usage (): print "Usage:hlds.py-h 127 .0.0.1-p 27015 " sys.exit (2) def main (): # Default values host =" localhost " port =" " If Len (sys.arg V) < 1: Usage () try: opts, args = Getopt.getopt (sys.argv[1:], "h:p:a:") except getopt. Getopterror: Usage () # Assign parameters as variables for opt, arg in opts: if opt = = "-H": hos t = arg if opt = = "-P": port = arg sock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM) port = Int (port) sock.settimeout (Sock.connect) ( (host, port)) sock.send (' \xff\ xff\xff\xff\x69\x6e\x66\x6f\x73\x74\x72\x69\x6e\x67\x0a\00 ') request=sock.recv (1400) Sock.close () server_info=request.replace (' \xff ', ') buffer = Re.findall (R ' \d+ ', server_info) print Buffer[6] If _ _name__ = = "__main__": Main ()
CS Counter Strike 1.6 version sample code
#!/bin/env python import urllib2, base64, SYS, getoptimport reimport socket def Usage (): print "Usage:hlds.py-h 127 .0.0.1-p 27015 " sys.exit (2) def main (): # Default values host =" localhost " port =" " If Len (sys.arg V) < 1: Usage () try: opts, args = Getopt.getopt (sys.argv[1:], "h:p:a:") except getopt. Getopterror: Usage () # Assign parameters as variables for opt, arg in opts: if opt = = "-H": hos t = arg if opt = = "-P": port = arg sock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM) port = Int (port) sock.settimeout (Sock.connect) ( (host, port)) sock.send (' \xff\ xff\xff\xff\x54\x53\x6f\x75\x72\x63\x65\x20\x45\x6e\x67\x69\x6e\x65\x20\x51\x75\x65\x72\x79\x00 ') request =SOCK.RECV (1400) sock.close () #server_info =request.replace (' \xff ', ') print request #buffer = Re.findall (R ' \d+ ', server_info) #print buffer[6] #print buffer if __name__ = = "__main__": Main ()