-
-
Save ewized/97814f57ac85af7128bf to your computer and use it in GitHub Desktop.
#!/usr/bin/python3 | |
# This is free and unencumbered software released into the public domain. | |
# | |
# Anyone is free to copy, modify, publish, use, compile, sell, or | |
# distribute this software, either in source code form or as a compiled | |
# binary, for any purpose, commercial or non-commercial, and by any | |
# means. | |
# | |
# In jurisdictions that recognize copyright laws, the author or authors | |
# of this software dedicate any and all copyright interest in the | |
# software to the public domain. We make this dedication for the benefit | |
# of the public at large and to the detriment of our heirs and | |
# successors. We intend this dedication to be an overt act of | |
# relinquishment in perpetuity of all present and future rights to this | |
# software under copyright law. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
# OTHER DEALINGS IN THE SOFTWARE. | |
# | |
# For more information, please refer to <http://unlicense.org/> | |
import socket | |
import struct | |
import json | |
import time | |
class StatusPing: | |
""" Get the ping status for the Minecraft server """ | |
def __init__(self, host='localhost', port=25565, timeout=5): | |
""" Init the hostname and the port """ | |
self._host = host | |
self._port = port | |
self._timeout = timeout | |
def _unpack_varint(self, sock): | |
""" Unpack the varint """ | |
data = 0 | |
for i in range(5): | |
ordinal = sock.recv(1) | |
if len(ordinal) == 0: | |
break | |
byte = ord(ordinal) | |
data |= (byte & 0x7F) << 7*i | |
if not byte & 0x80: | |
break | |
return data | |
def _pack_varint(self, data): | |
""" Pack the var int """ | |
ordinal = b'' | |
while True: | |
byte = data & 0x7F | |
data >>= 7 | |
ordinal += struct.pack('B', byte | (0x80 if data > 0 else 0)) | |
if data == 0: | |
break | |
return ordinal | |
def _pack_data(self, data): | |
""" Page the data """ | |
if type(data) is str: | |
data = data.encode('utf8') | |
return self._pack_varint(len(data)) + data | |
elif type(data) is int: | |
return struct.pack('H', data) | |
elif type(data) is float: | |
return struct.pack('L', int(data)) | |
else: | |
return data | |
def _send_data(self, connection, *args): | |
""" Send the data on the connection """ | |
data = b'' | |
for arg in args: | |
data += self._pack_data(arg) | |
connection.send(self._pack_varint(len(data)) + data) | |
def _read_fully(self, connection, extra_varint=False): | |
""" Read the connection and return the bytes """ | |
packet_length = self._unpack_varint(connection) | |
packet_id = self._unpack_varint(connection) | |
byte = b'' | |
if extra_varint: | |
# Packet contained netty header offset for this | |
if packet_id > packet_length: | |
self._unpack_varint(connection) | |
extra_length = self._unpack_varint(connection) | |
while len(byte) < extra_length: | |
byte += connection.recv(extra_length) | |
else: | |
byte = connection.recv(packet_length) | |
return byte | |
def get_status(self): | |
""" Get the status response """ | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as connection: | |
connection.settimeout(self._timeout) | |
connection.connect((self._host, self._port)) | |
# Send handshake + status request | |
self._send_data(connection, b'\x00\x00', self._host, self._port, b'\x01') | |
self._send_data(connection, b'\x00') | |
# Read response, offset for string length | |
data = self._read_fully(connection, extra_varint=True) | |
# Send and read unix time | |
self._send_data(connection, b'\x01', time.time() * 1000) | |
unix = self._read_fully(connection) | |
# Load json and return | |
response = json.loads(data.decode('utf8')) | |
response['ping'] = int(time.time() * 1000) - struct.unpack('L', unix)[0] | |
return response |
Stuck this in an AWS Lambda function and it works perfectly.
Tested this and it exits with no output. I edited 'localhost' to the server IP. The Minecraft client is able to see it without issue. Server is 1.15.2.
Tested this and it exits with no output. I edited 'localhost' to the server IP. The Minecraft client is able to see it without issue. Server is 1.15.2.
You use this script as part of another program it does nothing by itself.
If you want it to print things out you have add this at the bottom of the script.
status_ping = StatusPing('mc.hypixel.net'')
print(status_ping.get_status())
Hey, thanks for providing this class! It works well for me. I wonder what licence you published this? Would I be allowed to reuse your code on my project (non commercial, plan to make it FOSS code)?
This script is unlicensed but that means you can use it in FOSS, just give credit plz thanks
@ewized
That's not what having unlicensed code means. If there's no license, it means there is no legal way for us to take your script, because once you write it, you instantly own a copyright to it which is bound to you, unless you have a license that allows some freedoms for that work, it is legally treated as your unique work and anybody using it is in violation of that copyright. Licenses are here to extend this limitation and grant some further rights to others under them.
If you really don't want care about who is using this, you can use the "unlicense" license, which allows the code to be relicensed under any other license and doesn't require any obligrations from anyone using it. There's also something like Apache or MIT, if you want to be credited for the work, or even GPL, if you really want this code and any project that would be using this code open-sourced, however with GPL or other copyleft licenses, nobody would be able to relicense under any other license so all projects using this code would need to also be under GPL, which is why some people prefer the permissive licenses such as the MIT or even unlicense. However with these permissive licenses, the code isn't guaranteed to always stay open-sourced, which means I can relicense under some proprietary license as well and only follow the license terms such as mentioning the source somewhere in my readme.
Ideally, you should clarify this by updating the gist and adding a license header (or footer though that's a bit uncommon) with the full license text included.
There's no __repr__
... you need to call status_ping.get_status()
. Also btw, I'd suggest using mcstatus instead of this snippet since it's more complete and supports some other neat things too. But if you just want something minimal to purely just get the ping response, this is great!
struct.error: argument out of range
Fix https://gist.github.com/MarshalX/40861e1d02cbbc6f23acd3eced9db1a0