But, push this for now

This commit is contained in:
Luke Rogers 2014-02-23 19:35:51 +13:00
parent 90a91c1ef8
commit 44cb335bc0

View file

@ -63,7 +63,13 @@ def mcping_modern(host, port):
""" pings a server using the modern (1.7+) protocol and returns data """ """ pings a server using the modern (1.7+) protocol and returns data """
# connect to the server # connect to the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
try:
s.connect((host, port))
except socket.gaierror:
raise PingError("Invalid hostname")
except socket.timeout:
raise PingError("Request timed out")
# send handshake + status request # send handshake + status request
s.send(pack_data("\x00\x00" + pack_data(host.encode('utf8')) + pack_port(port) + "\x01")) s.send(pack_data("\x00\x00" + pack_data(host.encode('utf8')) + pack_port(port) + "\x01"))
@ -75,7 +81,7 @@ def mcping_modern(host, port):
l = unpack_varint(s) # String length l = unpack_varint(s) # String length
if not l > 1: if not l > 1:
raise Exception raise PingError("Invalid response")
d = "" d = ""
while len(d) < l: while len(d) < l:
@ -111,13 +117,16 @@ def mcping_modern(host, port):
def mcping_legacy(host, port): def mcping_legacy(host, port):
""" pings a server using the legacy (1.6 and older) protocol and returns data """ """ pings a server using the legacy (1.6 and older) protocol and returns data """
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try: try:
sock.connect((host, port)) sock.connect((host, port))
sock.send('\xfe\x01') sock.send('\xfe\x01')
response = sock.recv(1) response = sock.recv(1)
except socket.gaierror:
raise PingError("Invalid hostname")
except socket.timeout: except socket.timeout:
raise PingError("Request timed out") raise PingError("Request timed out")
print response
if response[0] != '\xff': if response[0] != '\xff':
raise PingError("Invalid response") raise PingError("Invalid response")
@ -206,7 +215,7 @@ def mcping(inp):
try: try:
host, port = parse_input(inp) host, port = parse_input(inp)
except ParseError as e: except ParseError as e:
return "Could not parse input: {}".format(e) return "Could not parse input ({})".format(e)
try: try:
data = mcping_modern(host, port) data = mcping_modern(host, port)
@ -214,7 +223,6 @@ def mcping(inp):
try: try:
data = mcping_legacy(host, port) data = mcping_legacy(host, port)
except PingError as e: except PingError as e:
print e return "Could not ping server, is it offline? ({})".format(e)
return "The server {} ({}:{}) looks offline from here.".format(inp, host, port)
return format_output(data) return format_output(data)