示例#1
0
    async def async_status(self, tries: int = 3, **kwargs):
        """Asynchronously checks the status of a Minecraft Java Edition server via the ping protocol.

        :param int tries: How many times to retry if it fails.
        :param type **kwargs: Passed to a `AsyncServerPinger` instance.
        :return: Status information in a `PingResponse` instance.
        :rtype: PingResponse
        """

        connection = TCPAsyncSocketConnection()
        await connection.connect((self.host, self.port))
        exception = None
        for attempt in range(tries):
            try:
                pinger = AsyncServerPinger(connection,
                                           host=self.host,
                                           port=self.port,
                                           **kwargs)
                pinger.handshake()
                result = await pinger.read_status()
                result.latency = await pinger.test_ping()
                return result
            except Exception as e:
                exception = e
        else:
            raise exception
示例#2
0
 async def async_ping(self, tries=3, **kwargs):
     connection = await TCPAsyncSocketConnection((self.host, self.port))
     exception = None
     for attempt in range(tries):
         try:
             pinger = AsyncServerPinger(connection, host=self.host, port=self.port, **kwargs)
             pinger.handshake()
             return await pinger.test_ping()
         except Exception as e:
             exception = e
     else:
         raise exception
示例#3
0
 async def async_status(self, tries=3, **kwargs):
     connection = TCPAsyncSocketConnection()
     await connection.connect((self.host, self.port))
     exception = None
     for attempt in range(tries):
         try:
             pinger = AsyncServerPinger(connection, host=self.host, port=self.port, **kwargs)
             pinger.handshake()
             result = await pinger.read_status()
             result.latency = await pinger.test_ping()
             return result
         except Exception as e:
             exception = e
     else:
         raise exception
示例#4
0
    async def async_ping(self, tries: int = 3, **kwargs):
        """Asynchronously checks the latency between a Minecraft Java Edition server and the client (you).

        :param int tries: How many times to retry if it fails.
        :param type **kwargs: Passed to a `AsyncServerPinger` instance.
        :return: The latency between the Minecraft Server and you.
        :rtype: float
        """

        connection = TCPAsyncSocketConnection()
        await connection.connect((self.host, self.port))
        exception = None
        for attempt in range(tries):
            try:
                pinger = AsyncServerPinger(connection, host=self.host, port=self.port, **kwargs)
                pinger.handshake()
                return await pinger.test_ping()
            except Exception as e:
                exception = e
        else:
            raise exception
示例#5
0
 def setUp(self):
     self.pinger = AsyncServerPinger(FakeAsyncConnection(),
                                     host="localhost",
                                     port=25565,
                                     version=44)
示例#6
0
class TestAsyncServerPinger(TestCase):
    def setUp(self):
        self.pinger = AsyncServerPinger(FakeAsyncConnection(),
                                        host="localhost",
                                        port=25565,
                                        version=44)

    def test_handshake(self):
        self.pinger.handshake()

        self.assertEqual(self.pinger.connection.flush(),
                         bytearray.fromhex("0F002C096C6F63616C686F737463DD01"))

    def test_read_status(self):
        self.pinger.connection.receive(
            bytearray.fromhex(
                "7200707B226465736372697074696F6E223A2241204D696E65637261667420536572766572222C22706C6179657273223A7B226D6178223A32302C226F6E6C696E65223A307D2C2276657273696F6E223A7B226E616D65223A22312E382D70726531222C2270726F746F636F6C223A34347D7D"
            ))
        status = async_decorator(self.pinger.read_status)()

        self.assertEqual(
            status.raw, {
                "description": "A Minecraft Server",
                "players": {
                    "max": 20,
                    "online": 0
                },
                "version": {
                    "name": "1.8-pre1",
                    "protocol": 44
                }
            })
        self.assertEqual(self.pinger.connection.flush(),
                         bytearray.fromhex("0100"))

    def test_read_status_invalid_json(self):
        self.pinger.connection.receive(bytearray.fromhex("0300017B"))
        self.assertRaises(IOError, async_decorator(self.pinger.test_ping))

    def test_read_status_invalid_reply(self):
        self.pinger.connection.receive(
            bytearray.fromhex(
                "4F004D7B22706C6179657273223A7B226D6178223A32302C226F6E6C696E65223A307D2C2276657273696F6E223A7B226E616D65223A22312E382D70726531222C2270726F746F636F6C223A34347D7D"
            ))

        self.assertRaises(IOError, async_decorator(self.pinger.test_ping))

    def test_read_status_invalid_status(self):
        self.pinger.connection.receive(bytearray.fromhex("0105"))

        self.assertRaises(IOError, async_decorator(self.pinger.test_ping))

    def test_test_ping(self):
        self.pinger.connection.receive(
            bytearray.fromhex("09010000000000DD7D1C"))
        self.pinger.ping_token = 14515484

        self.assertTrue(async_decorator(self.pinger.test_ping)() >= 0)
        self.assertEqual(self.pinger.connection.flush(),
                         bytearray.fromhex("09010000000000DD7D1C"))

    def test_test_ping_invalid(self):
        self.pinger.connection.receive(bytearray.fromhex("011F"))
        self.pinger.ping_token = 14515484

        self.assertRaises(IOError, async_decorator(self.pinger.test_ping))

    def test_test_ping_wrong_token(self):
        self.pinger.connection.receive(
            bytearray.fromhex("09010000000000DD7D1C"))
        self.pinger.ping_token = 12345

        self.assertRaises(IOError, async_decorator(self.pinger.test_ping))