Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion mcstatus/server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from mcstatus.pinger import PingResponse, ServerPinger, AsyncServerPinger
from mcstatus.protocol.connection import (
TCPSocketConnection,
Expand All @@ -13,6 +15,17 @@
__all__ = ["MinecraftServer", "MinecraftBedrockServer"]


def ensure_valid_ip(host: object, port: object):
if not isinstance(host, str):
raise TypeError(f"Host must be a string address, got {type(host)} ({host!r})")
if not isinstance(port, int):
raise TypeError(f"Port must be an integer port number, got {type(port)} ({port})")
if port > 65535 or port < 0:
raise ValueError(f"Port must be within the allowed range (0-2^16), got {port}")
if not re.fullmatch(r"(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])", host):
raise ValueError(f"Invalid host address, {host!r} (doesn't match the required pattern)")


class MinecraftServer:
"""Base class for a Minecraft Java Edition server.

Expand All @@ -24,6 +37,7 @@ class MinecraftServer:
"""

def __init__(self, host: str, port: int = 25565, timeout: float = 3):
ensure_valid_ip(host, port)
self.host = host
self.port = port
self.timeout = timeout
Expand Down Expand Up @@ -212,6 +226,7 @@ class MinecraftBedrockServer:
"""

def __init__(self, host: str, port: int = 19132, timeout: float = 3):
ensure_valid_ip(host, port)
self.host = host
self.port = port
self.timeout = timeout
Expand All @@ -224,7 +239,11 @@ def lookup(cls, address: str):
:return: A `MinecraftBedrockServer` instance.
:rtype: MinecraftBedrockServer
"""
return cls(*parse_address(address))
host, port = parse_address(address)
# If the address didn't contain port, fall back to constructor's default
if port is None:
return cls(host)
return cls(host, port)

def status(self, tries: int = 3, **kwargs) -> BedrockStatusResponse:
"""Checks the status of a Minecraft Bedrock Edition server.
Expand Down