py-mine / mcstatus

A Python library for checking the status of Minecraft servers
https://mcstatus.readthedocs.io
Apache License 2.0
465 stars 36 forks source link

TimeoutError when running code with mcstatus #454

Closed ghost closed 1 year ago

ghost commented 1 year ago

This is my full code without the db user and password

import mcstatus
import pymongo

# Connect to the MongoDB client
client = pymongo.MongoClient("mongodb+srv://user:pass@cluster0.2sc72th.mongodb.net/?retryWrites=true&w=majority")
print('connected to db')
# Get the "minecraft" database and the "ips" collection
db = client.minecraft
ips_collection = db.ips
print('got the databases')

# Get the "servers" collection
servers_collection = db.servers
print('got the servers collection')

# Find all the documents in the "ips" collection
for ip_doc in ips_collection.find():
    # Get the IP address and port from the document
    ip = ip_doc["IpAddr"]
    port = ip_doc["Port"]
    print('checking ip:', f"{ip}:{port}")
    # Use mcstatus to get the server status
    server = mcstatus.JavaServer.lookup(f"{ip}:{port}")
    status = server.status()

    # Create a new document with the server status data
    server_doc = {
    "ip": ip,
    "description": status.description,
    "players": status.players.online,
    "max_players": status.players.max,
    "motd": status.motd,
    "players_list": [player.name for player in status.players.sample],
    "player_ids": [player.id for player in status.player.sample]
}
    print('created server doc')

    # Insert the server status document into the "servers" collection
    servers_collection.insert_one(server_doc)
    print('inserted into collection')

This is what i get in my console after running the code

Markuss-MacBook-Air minecraft masscan % python3 checker.py
connected to db
got the databases
got the servers collection
checking ip: 43.248.138.92:25565
Traceback (most recent call last):
  File "/Users/user1/programeerimine/lih lambised asjad/minecraft masscan/checker.py", line 24, in <module>
    status = server.status()
             ^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/mcstatus/server.py", line 129, in status
    return self._retry_status(connection, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/mcstatus/utils.py", line 66, in sync_wrapper
    raise last_exc  # type: ignore # (This won't actually be unbound)
    ^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/mcstatus/utils.py", line 62, in sync_wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/mcstatus/server.py", line 135, in _retry_status
    result = pinger.read_status()
             ^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/mcstatus/pinger.py", line 114, in read_status
    response = self.connection.read_buffer()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/mcstatus/protocol/connection.py", line 313, in read_buffer
    length = self.read_varint()
             ^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/mcstatus/protocol/connection.py", line 254, in read_varint
    part = self.read(1)[0]
           ^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/mcstatus/protocol/connection.py", line 538, in read
    new = self.socket.recv(length - len(result))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: timed out
ItsDrike commented 1 year ago

Resolved in this discord conversation.

(Server was offline.)