首页 文章

使用Python将登录数据包发送到Minecraft服务器的错误

提问于
浏览
0

我有以下脚本,我在网上找到,用Python . 它的作用是尝试连接到MineCraft服务器,首先发送'handshake',然后发送登录请求 . 协议规格可在此处找到:http://wiki.vg/Protocol

无论如何,python脚本工作正常 . 但是,我认为第二个数据包编码错误,因为在发送时,服务器控制台上没有任何内容 . 播放器没有连接或任何东西 . 由于“客户端”没有及时登录,它最终会超时并关闭连接 .

基本上,无论如何谁有struct.pack()的经验应该能够在这里帮助我 . 我已经评论了我不确定我是否已将所有内容编码正确的行 . 有关打包数据的详细信息,请参见上面的链接 .

任何帮助将不胜感激,我对编码/打包数据非常无能为力 . :(

这是代码

import struct

import socket

import time

import urllib

import urllib2


host = str(raw_input('What is the host ip: '))

port = int(raw_input('What is the server port: '))

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((host, port))


usrnm = str(raw_input('What is your username: '))

psswrd = str(raw_input('What is your password: '))


logindata = {'user':usrnm, 'password':psswrd, 'version':'12'}

data = urllib.urlencode(logindata)

print('Sending data to login.minecraft.net...')

req = urllib2.Request('https://login.minecraft.net', data)

response = urllib2.urlopen(req)

returndata = response.read() 

returndata = returndata.split(":")

mcsessionid = returndata[3]

del req

del returndata

print("Session ID: " + mcsessionid)

data = {'user':usrnm,'host':host,'port':port}


enc_user = data['user'].encode('utf-16BE')

packfmt = '>bih{}shiibBB'.format(len(enc_user))

packetbytes = struct.pack(packfmt, 1, 23, len(data['user']), enc_user, 0, 0, 0, 0, 0, 0)

stringfmt = u'%(user)s;%(host)s:%(port)d'

string = stringfmt % data

structfmt = '>bh'

packetbytes = struct.pack(structfmt, 2, len(string))+string.encode('utf-16BE')

s.send(packetbytes)

connhash = s.recv(1024)

print("Connection Hash: " + connhash)

print('Sending data to http://session.minecraft.net/game/joinserver.jsp?user=' + usrnm + '&sessionId=' + mcsessionid + '&serverId=' + connhash + '...')

req = urllib.urlopen('http://session.minecraft.net/game/joinserver.jsp?user=' + usrnm + '&sessionId=' + mcsessionid + '&serverId=' + connhash)

returndata = req.read()

if(returndata == 'OK'):

    print('session.minecraft.net says everything is okay, proceeding to send data to server.')

else:

    print('Oops, something went wrong.')

time.sleep(5)

# All above here works perfectly.

enc_user = data['user'].encode('utf-16BE')

packfmt = '>bih{}shiibBB'.format(len(enc_user))

packetbytes = struct.pack(packfmt, 1, 23, len(data['user']), enc_user, 0, 0, 0, 0, 0, 0)

#This line is probably where something's going wrong:

packetbytes = struct.pack('>bih', 1, 23, len(data['user'])) + data['user'].encode('utf-16BE') + struct.pack('>hiibBB', 2,0,0,0,0,0)

print(len(packetbytes))

print('Sending ' + packetbytes + ' to server.')

s.send(packetbytes)



while True:

    data = s.recv(1024)

    if data:

        print(data)

1 回答

  • 1

    是的,你要发送字符串的长度,即字符数 . 相反,您应该发送编码字符串中的字节数 . 另外,你应该使用“!”而不是为了清晰起见“>”,而不是“!”用于表示“网络顺序”,这是 . 所以这...

    structfmt = '>bh'
    
    packetbytes = struct.pack(structfmt, 2, len(string))+string.encode('utf-16BE')
    

    ......改成了......

    structfmt = '!bh'
    
    encoded = string.encode('utf-16BE')
    
    packetbytes = struct.pack(structfmt, 2, len(encoded))+encoded
    

相关问题