From 90a91c1ef8941b4978fb50d879f6645123210565 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Sun, 23 Feb 2014 13:30:24 +1300 Subject: [PATCH] Fixed issues @daboross - closes #208 --- plugins/minecraft_ping.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/plugins/minecraft_ping.py b/plugins/minecraft_ping.py index 6835867..617d88d 100644 --- a/plugins/minecraft_ping.py +++ b/plugins/minecraft_ping.py @@ -1,6 +1,7 @@ import socket import struct import json +import traceback from util import hook @@ -87,16 +88,15 @@ def mcping_modern(host, port): data = json.loads(d.decode('utf8')) try: version = data["version"]["name"] - if data["description"].get("text", None): + try: desc = u" ".join(data["description"]["text"].split()) - else: + except TypeError: desc = u" ".join(data["description"].split()) max_players = data["players"]["max"] online = data["players"]["online"] except Exception as e: - from pprint import pprint - pprint(data) - return "Invalid data - check console ({})".format(e) + traceback.print_exc(e) + raise PingError("Unknown Error: {}".format(e)) output = { "motd": format_colors(desc), @@ -111,12 +111,16 @@ def mcping_modern(host, port): def mcping_legacy(host, port): """ pings a server using the legacy (1.6 and older) protocol and returns data """ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((host, port)) - sock.send('\xfe\x01') - response = sock.recv(1) + try: + sock.connect((host, port)) + sock.send('\xfe\x01') + response = sock.recv(1) + except socket.timeout: + raise PingError("Request timed out") print response if response[0] != '\xff': - return "Server gave invalid response: " + repr(response) + raise PingError("Invalid response") + length = struct.unpack('!h', sock.recv(2))[0] values = sock.recv(length * 2).decode('utf-16be') data = values.split(u'\x00') # try to decode data using new format @@ -206,10 +210,11 @@ def mcping(inp): try: data = mcping_modern(host, port) - return format_output(data) except PingError: try: data = mcping_legacy(host, port) - return format_output(data) - except PingError: + except PingError as e: + print e return "The server {} ({}:{}) looks offline from here.".format(inp, host, port) + + return format_output(data)