好的,这基本上是我的代码:
class Server:
def __init__(self, addr, port, mc):
self.mc = mc
data = {'user': mc.user, 'password': mc.pswd, 'version': '12'}
data = urllib.urlencode(data)
dta = urllib.urlopen("https://login.minecraft.net", data=data).read()
dta = dta.split(":")
self.sessionid = dta[3]
self.socket = socket.socket()
self.socket.connect((addr, port))
data = {"user": mc.user, "host": addr, "port": port}
enc = mc.user.encode('utf-16BE')
structfmt = '!bh'
bytes = struct.pack(structfmt, 2, len(enc))
bytes = bytes + enc
self.socket.send(self.get_login_packet(mc.user, mc.pswd, addr, port))
self.random = False
self.world = World()
def test(self):
self.socket.send(self.get_chat_packet(raw_input("Enter a message: ")))
self.disconnect(raw_input("Enter the disconnect message: "))
def keep_alive(self):
if self.random:
self.socket.send(struct.pack("!bi", 0, self.random))
def get_login_packet(self, user, password, addr, port):
data = {"user": mc.user, "host": addr, "port": port}
enc = mc.user.encode('utf-16BE')
structfmt = '!bh'
bytes = struct.pack(structfmt, 2, len(enc))
bytes = bytes + enc
return bytes
def get_chat_packet(self, message):
if len(message) > 100:
message = message[:100]
return struct.pack("!bs", 3, message)
def get_disconnect_packet(self, message):
return struct.pack("!bs", 0xFF, message)
def disconnect(self, message):
self.socket.send(self.get_disconnect_packet(message))
time.sleep(0.1)
self.socket.close()
mc 是 python 中的一个 Minecraft 实例,它基本上有 4 个属性,server(上面的类)、user、pswd 和 world(现在是 None 类型)。
我不是在尝试连接到 Minecraft Classic,而是在尝试连接到付费版本。我在 Windows XP 上使用 Python 2.5。当我尝试在服务器上连接时它给我的错误是
[INFO] Disconnecting /127.0.0.1:2292: Took too long to log in
[INFO] /127.0.0.1:2295 lost connection
Python上的错误是
self.socket.send(self.get_chat_packet(raw_input("Enter a message: ")))
socket.error: (10053, 'Software caused connection abort')
请告诉我为什么会发生这种情况,最好也告诉我如何解决它。谢谢!