Dinnerbone / mcstatus

A Python class for checking the status of an enabled Minecraft server
http://dinnerbone.com/minecraft/tools/status/
1.11k stars 146 forks source link

TypeError: an integer is required (got type NoneType) #189

Closed EpicEfeathers closed 2 years ago

EpicEfeathers commented 2 years ago

Using this code:

bedrock_server = mcstatus.MinecraftBedrockServer.lookup('play.lbsg.net')
response = bedrock_server.status()

Gives me the error:

  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/mcstatus/server.py", line 242, in status
    raise exception
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/mcstatus/server.py", line 236, in status
    resp = BedrockServerStatus(self.host, self.port, **kwargs).read_status()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/mcstatus/bedrock_status.py", line 51, in read_status
    s.sendto(self.request_status_data, (self.host, self.port))
TypeError: an integer is required (got type NoneType)

If I try adding a port, for example:

bedrock_server = mcstatus.MinecraftBedrockServer.lookup('play.lbsg.net', 25565)
response = bedrock_server.status()

I get the error TypeError: lookup() takes 2 positional arguments but 3 were given What am I doing wrong?

ItsDrike commented 2 years ago

With the lookup function, you're expected to also specify a port in the address, so the IP should look like: play.lbsg.net:25565. Not doing so will fail to obtain the port and instead sets the port to None. You could also use the regular constructor rather than calling the lookup method and just doing: mcstatus.MinecraftBedrockServer("play.lbsg.net", 25565).

That out of the way, I do think that there should be an error raised directly on initiation of the server instance if the port number wasn't valid (outside of the allowed port range, or non-integer, additionally, it may be good to also ensure that the address is actually a string and perhaps that it matches some regex pattern to avoid non-sensical IPs). Implementing these checks within __init__ will cause an error on initiation, rather than on usage which can then lead to really confusing errors like these.

EpicEfeathers commented 2 years ago

@ItsDrike Thank you for the response! This fixes my problem.