M
M
Maxim Tarabrin2018-04-01 12:48:07
Python
Maxim Tarabrin, 2018-04-01 12:48:07

How to inflate Source-Query in Python 3+?

Hello everyone, help me inflate Source-Query. I want to poll the CS 1.6 server I

wrote a couple of lines, but it doesn't even respond. The firewall is disabled on the server.

import socket
 
IP = '195.62.53.149'
PORT = 27015
 
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((IP, PORT))
sock.send('TSource Engine Query'.encode('utf-8'))
sock.settimeout(120)
text = sock.recv(1024)
print('res ->' + text)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2018-04-01
@padr1no

Your code is working, except for the last line, where you add bytes to the line. You don't get an answer because the request is made incorrectly, but this is no longer a question of the programming language or sockets, but of the format of requests and responses. The request must start with four 'FF' bytes.

import socket
 
IP = '195.62.53.149'
PORT = 27015
 
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((IP, PORT))
sock.send(b'\xff\xff\xff\xffTSource Engine Query')
sock.settimeout(120)
text = sock.recv(1024)
print(b'res ->' + text)

> python cs_udp.py 
b'recv -> \xff\xff\xff\xffI0Euro-Cs.Ru | Public | 1000FPS\x00de_dust2_2x2\x00cstrike\x00Counter-Strike\x00\n\x00\x03\x14\x00dl\x00\x011.1.2.7/Stdio\x00\x80\x87i'

UPD:
I have never worked with this format. There is a python-valve library. The very first example from its documentation works perfectly with your server without unnecessary gestures.
import valve.source.a2s

SERVER_ADDRESS = ('195.62.53.149', 27015)

with valve.source.a2s.ServerQuerier(SERVER_ADDRESS) as server:
    info = server.info()
    players = server.players()

print("{player_count}/{max_players} {server_name}".format(**info))
for player in sorted(players["players"],
                     key=lambda p: p["score"], reverse=True):
    print("{score} {name}".format(**player))

> python udp_client.py 
4/20 Euro-Cs.Ru | Public | 1000FPS
39 1UP|mam?a
31 zakeman4
16 Cosmodesant
0 (А.У.Е.ШПАНА) хуй

Link to Documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question