Skip to content

Modifications for python3 compatibility. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions demo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/python
#!/usr/bin/python3
import mcquery
import time
from pprint import pprint

print 'Ctrl-C to exit'

host = raw_input('Host (localhost): ')
port = raw_input('Port (25565): ')
host = input('Host (localhost): ')
port = input('Port (25565): ')

if host == '':
host = 'localhost'
Expand All @@ -16,10 +15,10 @@



print "Connecting..."
print("Connecting...")
q = mcquery.MCQuery(host, port)
print "Connected."
print("Connected.")

while True:
print q.full_stat()
pprint(q.full_stat())
time.sleep(5)
43 changes: 27 additions & 16 deletions mcquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ def __init__(self, host, port, **kargs):
self.handshake()

def write_packet(self, type, payload):
o = '\xFE\xFD' + struct.pack('>B', type) + struct.pack('>l', self.id) + payload
o = b'\xFE\xFD' + struct.pack('>B', type) + struct.pack('>l', self.id) + payload
self.socket.sendto(o, self.addr)

def read_packet(self):
buff = self.socket.recvfrom(2048)[0]
type = struct.unpack('>B', buff[0])[0]
type = buff[0]
id = struct.unpack('>l', buff[1:5])[0]
return type, id, buff[5:]

def handshake(self):
self.id += 1
self.write_packet(9, '')
self.write_packet(9, b'')
try:
type, id, buff = self.read_packet()
except:
Expand All @@ -57,7 +57,7 @@ def basic_stat(self):
#buff = buff[4:]

#Grab the first 5 string fields
data['motd'], data['gametype'], data['map'], data['numplayers'], data['maxplayers'], buff = buff.split('\x00', 5)
data['motd'], data['gametype'], data['map'], data['numplayers'], data['maxplayers'], buff = buff.split(b'\x00', 5)

#Unpack a big-endian short for the port
data['hostport'] = struct.unpack('<h', buff[:2])[0]
Expand All @@ -73,7 +73,7 @@ def basic_stat(self):

def full_stat(self):
#Pad request to 8 bytes
self.write_packet(0, self.challenge + '\x00\x00\x00\x00')
self.write_packet(0, self.challenge + b'\x00\x00\x00\x00')
try:
type, id, buff = self.read_packet()
except:
Expand All @@ -84,33 +84,44 @@ def full_stat(self):
buff = buff[11:]

#Split around notch's silly token
items, players = buff.split('\x00\x00\x01player_\x00\x00')
items, players = buff.split(b'\x00\x00\x01player_\x00\x00')

#Notch wrote "hostname" where he meant to write "motd"
items = 'motd' + items[8:]
items = b'motd' + items[8:]

#Encode (k1, v1, k2, v2 ..) into a dict
items = items.split('\x00')
items = items.split(b'\x00')
data = dict(zip(items[::2], items[1::2]))

#Remove final two null bytes
players = players[:-2]

#Split player list
if players: data['players'] = players.split('\x00')
if players: data['players'] = players.split(b'\x00')
else: data['players'] = []

#Encode ints
for k in ('numplayers', 'maxplayers', 'hostport'):
data[k] = int(data[k])
for k in (b'numplayers', b'maxplayers', b'hostport'):
data[k.decode()] = int(data[k])

#Parse 'plugins'
s = data['plugins']
s = s.split(': ', 1)
#Parse 'plugins'`
s = data[b'plugins']
s = s.split(b': ', 1)
data['server_mod'] = s[0]
if len(s) == 1:
data['plugins'] = []
elif len(s) == 2:
data['plugins'] = s[1].split('; ')
data['plugins'] = list(map(bytes.decode, s[1].split(b'; ')))

return data
return dict(
motd = data[b'motd'],
gametype = data[b'gametype'].decode(),
game_id = data[b'game_id'].decode(),
version = data[b'version'].decode(),
plugins = data['plugins'],
map = data[b'map'].decode(),
numplayers = data['numplayers'],
maxplayers = data['maxplayers'],
hostip = data[b'hostip'].decode(),
hostport = data['hostport'],
server_mod = data['server_mod'].decode())