Skip to content

Commit

Permalink
Merge pull request #176 from Dominique57/add-timeout-propagation
Browse files Browse the repository at this point in the history
Add timeout parameter propagation from lookup func
  • Loading branch information
kevinkjt2000 committed Jan 3, 2022
2 parents 2e4a755 + af3d88f commit 9ec29ee
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions mcstatus/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ class MinecraftServer:
:param str host: The host/address/ip of the Minecraft server.
:param int port: The port that the server is on.
:param float timeout: The timeout in seconds before failing to connect.
:attr host:
:attr port:
"""

def __init__(self, host: str, port: int = 25565):
def __init__(self, host: str, port: int = 25565, timeout: float = 3):
self.host = host
self.port = port
self.timeout = timeout

@classmethod
def lookup(cls, address: str):
def lookup(cls, address: str, timeout: float = 3):
"""Parses the given address and checks DNS records for an SRV record that points to the Minecraft server.
:param str address: The address of the Minecraft server, like `example.com:25565`.
:param float timeout: The timeout in seconds before failing to connect.
:return: A `MinecraftServer` instance.
:rtype: MinecraftServer
"""
Expand All @@ -47,7 +50,7 @@ def lookup(cls, address: str):
except Exception:
pass

return cls(host, port)
return cls(host, port, timeout)

def ping(self, tries: int = 3, **kwargs) -> float:
"""Checks the latency between a Minecraft Java Edition server and the client (you).
Expand All @@ -58,7 +61,7 @@ def ping(self, tries: int = 3, **kwargs) -> float:
:rtype: float
"""

connection = TCPSocketConnection((self.host, self.port))
connection = TCPSocketConnection((self.host, self.port), self.timeout)
exception_to_raise_after_giving_up: Exception
for _ in range(tries):
try:
Expand All @@ -80,7 +83,7 @@ async def async_ping(self, tries: int = 3, **kwargs) -> float:
"""

connection = TCPAsyncSocketConnection()
await connection.connect((self.host, self.port))
await connection.connect((self.host, self.port), self.timeout)
exception_to_raise_after_giving_up: Exception
for _ in range(tries):
try:
Expand All @@ -102,7 +105,7 @@ def status(self, tries: int = 3, **kwargs) -> PingResponse:
:rtype: PingResponse
"""

connection = TCPSocketConnection((self.host, self.port))
connection = TCPSocketConnection((self.host, self.port), self.timeout)
exception_to_raise_after_giving_up: Exception
for _ in range(tries):
try:
Expand All @@ -126,7 +129,7 @@ async def async_status(self, tries: int = 3, **kwargs) -> PingResponse:
"""

connection = TCPAsyncSocketConnection()
await connection.connect((self.host, self.port))
await connection.connect((self.host, self.port), self.timeout)
exception_to_raise_after_giving_up: Exception
for _ in range(tries):
try:
Expand Down Expand Up @@ -159,7 +162,7 @@ def query(self, tries: int = 3) -> QueryResponse:
exception_to_raise_after_giving_up: Exception
for _ in range(tries):
try:
connection = UDPSocketConnection((host, self.port))
connection = UDPSocketConnection((host, self.port), self.timeout)
querier = ServerQuerier(connection)
querier.handshake()
return querier.read_query()
Expand Down Expand Up @@ -188,7 +191,7 @@ async def async_query(self, tries: int = 3) -> QueryResponse:
for _ in range(tries):
try:
connection = UDPAsyncSocketConnection()
await connection.connect((host, self.port))
await connection.connect((host, self.port), self.timeout)
querier = AsyncServerQuerier(connection)
await querier.handshake()
return await querier.read_query()
Expand All @@ -203,13 +206,15 @@ class MinecraftBedrockServer:
:param str host: The host/address/ip of the Minecraft server.
:param int port: The port that the server is on.
:param float timeout: The timeout in seconds before failing to connect.
:attr host:
:attr port:
"""

def __init__(self, host: str, port: int = 19132):
def __init__(self, host: str, port: int = 19132, timeout: float = 3):
self.host = host
self.port = port
self.timeout = timeout

@classmethod
def lookup(cls, address: str):
Expand All @@ -233,7 +238,7 @@ def status(self, tries: int = 3, **kwargs) -> BedrockStatusResponse:

for _ in range(tries):
try:
resp = BedrockServerStatus(self.host, self.port, **kwargs).read_status()
resp = BedrockServerStatus(self.host, self.port, self.timeout, **kwargs).read_status()
break
except BaseException as e:
exception = e
Expand All @@ -255,7 +260,7 @@ async def async_status(self, tries: int = 3, **kwargs) -> BedrockStatusResponse:

for _ in range(tries):
try:
resp = await BedrockServerStatus(self.host, self.port, **kwargs).read_status_async()
resp = await BedrockServerStatus(self.host, self.port, self.timeout, **kwargs).read_status_async()
break
except BaseException as e:
exception = e
Expand Down

0 comments on commit 9ec29ee

Please sign in to comment.