最近猛玩Minecraft,无聊之下写了这个东西,可以取到版本,在线人数,最大人数。

import socket
import codecs
class mcstatus:
    def __init__(self,hostname,port,timeout = 0.6):
        self.hostname = hostname
        self.timeout = timeout
        self.port = port
    def getserverinfo(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ip = socket.gethostbyname(self.hostname)
        try:
            s.settimeout(self.timeout)
            s.connect((ip,self.port))
            s.send(bytearray([0xFE, 0x01]))
            data_raw = s.recv(1024)
            s.close()
            #这个编码太神秘了
            data = data_raw.decode('cp437').split('\x00\x00\x00')
            #print codecs.utf_16_be_decode(data_raw[1:])[0] #(utf-16)这个可以看到MOTD和其他信息
            info = {}
            info['version'] = data[2].replace("\x00","")
            info['online_players'] = data[4].replace("\x00","")
            info['max_players'] = data[5].replace("\x00","")
            return (True,info)
        except socket.error:
            return (False)
if __name__ == '__main__':
    app = mcstatus('mc1.chutian.bid',25565)
    print app.getserverinfo()

欢迎来我的小服务器玩耍(已开启正版验证):

mc1.chutian.bid

mcserver.PNG

文章版权:远望江来 - 人的一生当然要靠自我奋斗,当然也要考虑历史的行程

本文链接:https://blog.chutian.bid/archives/66.html

转载请注明文章原始出处 !

添加新评论

返回顶部
Typecho 强力驱动. © Powered by Zhangtinghui. Theme by Postbird.