當前位置: 首頁>>代碼示例>>Python>>正文


Python MinecraftServer.ping方法代碼示例

本文整理匯總了Python中mcstatus.MinecraftServer.ping方法的典型用法代碼示例。如果您正苦於以下問題:Python MinecraftServer.ping方法的具體用法?Python MinecraftServer.ping怎麽用?Python MinecraftServer.ping使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在mcstatus.MinecraftServer的用法示例。


在下文中一共展示了MinecraftServer.ping方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: index

# 需要導入模塊: from mcstatus import MinecraftServer [as 別名]
# 或者: from mcstatus.MinecraftServer import ping [as 別名]
def index():
    """Index (and only) page of site."""
    render_obj = []
    message_objects = Message.query.filter_by(display=True).all()
    server_objects = Server.query.all()

    for server in server_objects:
        address = server.address
        port = server.port
        conn = MinecraftServer(address, port)

        try:
            conn.ping()
        # except ConnectionRefusedError:
        # Commented out because for some reason on the production server,
        # attempting to catch this error causes the site to break and display
        # 'Internal Server Error'. Logs from such events say that
        # 'ConnectionRefusedError' is undefined, although there seem to be no
        # issues when testing locally.
        except:
            render_obj.append({
                "address": address,
                "port": port,
                "modpack_version": server.modpack_version,
                "client_config": server.client_config,
                "status": "Offline",
            })
        else:
            add_to_render = {}
            q = conn.query()
            motd = q.motd
            players = {
                "max": q.players.max,
                "online": q.players.online,
                "names": q.players.names,
            }
            jar_version = q.software.version

            add_to_render.update({
                "address": address,
                "port": port,
                "modpack_version": server.modpack_version,
                "client_config": server.client_config,
                "status": "Online",
                "motd": motd,
                "players": players,
                "jar_version": jar_version,
            })

            render_obj.append(add_to_render)

    return render_template(
        "index.html",
        servers=render_obj,
        messages=message_objects,
    )
開發者ID:tlake,項目名稱:mc_site,代碼行數:58,代碼來源:mc_site.py

示例2: query

# 需要導入模塊: from mcstatus import MinecraftServer [as 別名]
# 或者: from mcstatus.MinecraftServer import ping [as 別名]
def query():
    address = request.args.get("address")
    port = int(request.args.get("port"))
    conn = MinecraftServer(address, port)

    try:
        conn.ping()
    # except ConnectionRefusedError:
    # Commented out because for some reason on the production server,
    # attempting to catch this error causes the site to break and display
    # 'Internal Server Error'. Logs from such events say that
    # 'ConnectionRefusedError' is undefined, although there seem to be no
    # issues when testing locally.
    except:
        response = jsonify({
            "status": "Offline",
            "players_online": 0,
            "player_names": None,
        })
    else:
        q = conn.query()
        response = jsonify({
            "status": "Online",
            "players_online": q.players.online,
            "players_max": q.players.max,
            "player_names": q.players.names,
        })

    return response
開發者ID:tlake,項目名稱:mc_site,代碼行數:31,代碼來源:mc_site.py

示例3: MinecraftServer

# 需要導入模塊: from mcstatus import MinecraftServer [as 別名]
# 或者: from mcstatus.MinecraftServer import ping [as 別名]
from mcstatus import MinecraftServer

# If you know the host and port, you may skip this and use MinecraftServer("example.org", 1234)
# server = MinecraftServer.lookup("bcsn.us:25565")
server = MinecraftServer("184.18.202.133:25565")

# 'status' is supported by all Minecraft servers that are version 1.7 or higher.
status = server.status()
print("The server has {0} players and replied in {1} ms".format(status.players.online, status.latency))

# 'ping' is supported by all Minecraft servers that are version 1.7 or higher.
# It is included in a 'status' call, but is exposed separate if you do not require the additional info.
latency = server.ping()
print("The server replied in {0} ms".format(latency))

# 'query' has to be enabled in a servers' server.properties file.
# It may give more information than a ping, such as a full player list or mod information.
query = server.query()
print("The server has the following players online: {0}".format(", ".join(query.players.names)))
開發者ID:SimpleAOB,項目名稱:MCSZ,代碼行數:21,代碼來源:ping.py


注:本文中的mcstatus.MinecraftServer.ping方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。