Skip to content

Instantly share code, notes, and snippets.

@barneygale
Last active July 7, 2026 13:37
Show Gist options
  • Select an option

  • Save barneygale/1209061 to your computer and use it in GitHub Desktop.

Select an option

Save barneygale/1209061 to your computer and use it in GitHub Desktop.
import socket
import struct
import json
def unpack_varint(s):
d = 0
for i in range(5):
b = ord(s.recv(1))
d |= (b & 0x7F) << 7*i
if not b & 0x80:
break
return d
def pack_varint(d):
o = ""
while True:
b = d & 0x7F
d >>= 7
o += struct.pack("B", b | (0x80 if d > 0 else 0))
if d == 0:
break
return o
def pack_data(d):
return pack_varint(len(d)) + d
def pack_port(i):
return struct.pack('>H', i)
def get_info(host='localhost', port=25565):
# Connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
# Send handshake + status request
s.send(pack_data("\x00\x00" + pack_data(host.encode('utf8')) + pack_port(port) + "\x01"))
s.send(pack_data("\x00"))
# Read response
unpack_varint(s) # Packet length
unpack_varint(s) # Packet ID
l = unpack_varint(s) # String length
d = ""
while len(d) < l:
d += s.recv(1024)
# Close our socket
s.close()
# Load json and return
return json.loads(d.decode('utf8'))
@Miniontoby

Miniontoby commented Jun 6, 2022

Copy link
Copy Markdown

There is an error for me: Traceback (most recent call last): File "C:\GetInfo.py", line 55, in <module> print(get_info(host='play.hypixel.net',port=25565)) File "C:\GetInfo.py", line 37, in get_info s.send(pack_data("\x00\x00" + pack_data(host.encode('utf8')) + pack_port(port) + "\x01")) File "C:\GetInfo.py", line 25, in pack_data return pack_varint(len(d)) + d File "C:\GetInfo.py", line 19, in pack_varint o += struct.pack("B", b | (0x80 if d > 0 else 0)) TypeError: can only concatenate str (not "bytes") to str

While using: print(get_info(host='play.hypixel.net',port=25565))

Help

The script is written for python2, so it doesn't work with python3.

I cannot really edit the code so it will work at python3 for some reason... so yeah

@clarence112

Copy link
Copy Markdown

I've updated this to be compatible with python3

https://gist.github.com/clarence112/9a3e971283d7f4052a0c33f11de9b7c5

@Noah-Holmen

Copy link
Copy Markdown

There is an error for me: Traceback (most recent call last): File "C:\GetInfo.py", line 55, in <module> print(get_info(host='play.hypixel.net',port=25565)) File "C:\GetInfo.py", line 37, in get_info s.send(pack_data("\x00\x00" + pack_data(host.encode('utf8')) + pack_port(port) + "\x01")) File "C:\GetInfo.py", line 25, in pack_data return pack_varint(len(d)) + d File "C:\GetInfo.py", line 19, in pack_varint o += struct.pack("B", b | (0x80 if d > 0 else 0)) TypeError: can only concatenate str (not "bytes") to str

While using: print(get_info(host='play.hypixel.net',port=25565))

Help

i think i fixed the code:
import socket
import struct
import json

def unpack_varint(s):
d = 0
for i in range(5):
r=s.recv(1)
while len(r)<1:
r=s.recv(1)
b=r[0]
d |= (b & 0x7F) << 7*i
if not b & 0x80:
break
return d

def pack_varint(d):
o = b""
while True:
b = d & 0x7F
d >>= 7
o += struct.pack("B", b | (0x80 if d > 0 else 0))
if d == 0:
break
return o

def pack_data(d):
return pack_varint(len(d)) + d

def pack_port(i):
return struct.pack('>H', i)

def get_info(host='localhost', port=25565):

# Connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

# Send handshake + status request
s.send(pack_data("\x00\x00".encode("utf8") + pack_data(host.encode('utf8')) + (pack_port(port)) + "\x01".encode("utf8")))
s.send(pack_data("\x00".encode("utf8")))

# Read response
unpack_varint(s)     # Packet length
unpack_varint(s)     # Packet ID
l = unpack_varint(s) # String length

d = b""
while len(d) < l:
    d += s.recv(1024)

# Close our socket
s.close()

# Load json and return
return json.loads(d.decode("utf8"))

print(get_info(host="play.hypixel.net"))

@Noah-Holmen

Noah-Holmen commented Jul 7, 2026

Copy link
Copy Markdown

the code for python3 is:
import socket
import struct
import json

def unpack_varint(s):
d = 0
for i in range(5):
r=s.recv(1)
while len(r)<1:
r=s.recv(1)
b=r[0]
d |= (b & 0x7F) << 7*i
if not b & 0x80:
break
return d

def pack_varint(d):
o = b""
while True:
b = d & 0x7F
d >>= 7
o += struct.pack("B", b | (0x80 if d > 0 else 0))
if d == 0:
break
return o

def pack_data(d):
return pack_varint(len(d)) + d

def pack_port(i):
return struct.pack('>H', i)

def get_info(host='localhost', port=25565):

# Connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

# Send handshake + status request
s.send(pack_data("\x00\x00".encode("utf8") + pack_data(host.encode('utf8')) + (pack_port(port)) + "\x01".encode("utf8")))
s.send(pack_data("\x00".encode("utf8")))

# Read response
unpack_varint(s)     # Packet length
unpack_varint(s)     # Packet ID
l = unpack_varint(s) # String length

d = b""
while len(d) < l:
    d += s.recv(1024)

# Close our socket
s.close()

# Load json and return
return json.loads(d.decode("utf8"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment