mirror of
https://github.com/Dinnerbone/mcstatus.git
synced 2026-04-06 12:01:24 +08:00
Expose query through server.query_server()
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import json
|
||||
|
||||
from mcstatus.pinger import ServerPinger
|
||||
from mcstatus.protocol.connection import TCPSocketConnection
|
||||
from mcstatus.protocol.connection import TCPSocketConnection, UDPSocketConnection
|
||||
from mcstatus.querier import ServerQuerier
|
||||
|
||||
|
||||
class MinecraftServer:
|
||||
@@ -14,3 +15,9 @@ class MinecraftServer:
|
||||
pinger = ServerPinger(connection, host=self.host, port=self.port, **kwargs)
|
||||
pinger.handshake()
|
||||
return pinger.read_status(), pinger.test_ping()
|
||||
|
||||
def query_server(self):
|
||||
connection = UDPSocketConnection((self.host, self.port))
|
||||
querier = ServerQuerier(connection)
|
||||
querier.handshake()
|
||||
return querier.read_query()
|
||||
@@ -1,6 +1,6 @@
|
||||
from unittest import TestCase
|
||||
|
||||
from mock import patch
|
||||
from mock import patch, Mock
|
||||
|
||||
from mcstatus.protocol.connection import Connection
|
||||
from mcstatus.server import MinecraftServer
|
||||
@@ -22,3 +22,28 @@ class TestMinecraftServer(TestCase):
|
||||
self.assertEqual(self.socket.remaining(), 0, msg="Data is pending to be read, but should be empty")
|
||||
self.assertEqual(info.raw, {"description":"A Minecraft Server","players":{"max":20,"online":0},"version":{"name":"1.8","protocol":47}})
|
||||
self.assertTrue(latency >= 0)
|
||||
|
||||
def test_query_server(self):
|
||||
self.socket.receive(bytearray.fromhex("090000000035373033353037373800"))
|
||||
self.socket.receive(bytearray.fromhex("00000000000000000000000000000000686f73746e616d650041204d696e656372616674205365727665720067616d657479706500534d500067616d655f6964004d494e4543524146540076657273696f6e00312e3800706c7567696e7300006d617000776f726c64006e756d706c61796572730033006d6178706c617965727300323000686f7374706f727400323535363500686f73746970003139322e3136382e35362e31000001706c617965725f000044696e6e6572626f6e6500446a696e6e69626f6e650053746576650000"))
|
||||
|
||||
self.socket.remaining = Mock()
|
||||
self.socket.remaining.side_effect = [15, 208]
|
||||
|
||||
with patch("mcstatus.server.UDPSocketConnection") as connection:
|
||||
connection.return_value = self.socket
|
||||
info = self.server.query_server()
|
||||
|
||||
self.assertEqual(self.socket.flush(), bytearray.fromhex("FEFD090000000000000000FEFD000000000021FEDCBA00000000"))
|
||||
self.assertEqual(info.raw, {
|
||||
"hostname": "A Minecraft Server",
|
||||
"gametype": "SMP",
|
||||
"game_id": "MINECRAFT",
|
||||
"version": "1.8",
|
||||
"plugins": "",
|
||||
"map": "world",
|
||||
"numplayers": "3",
|
||||
"maxplayers": "20",
|
||||
"hostport": "25565",
|
||||
"hostip": "192.168.56.1",
|
||||
})
|
||||
Reference in New Issue
Block a user