我在Python中有以下脚本 . 它的作用是尝试连接到MineCraft服务器,首先发送'handshake',然后发送登录请求 . 协议规格可以在这里找到:http://wiki.vg/Protocol
无论如何,python脚本工作正常,没有错误 . 但是,我相当使用我编码第二个数据包错误,因为它发送时,服务器控制台上没有任何内容 . 播放器没有连接或任何东西 . 由于“客户端”没有及时登录,它最终会超时并关闭连接 .
基本上,无论如何谁有struct.pack()的经验应该能够在这里帮助我 . 我已经评论了我不确定我是否已将所有内容编码正确的行 . 有关打包数据的详细信息,请参见上面的链接 .
任何帮助将不胜感激,我对编码/打包数据非常无能为力 . :(
import struct
import socket
import time
import urllib
import urllib2
host = "127.0.0.1"
port = 25566
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
logindata = {'user':'JackBeePee', 'password':'*******', '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':u'JackBeePee','host':u'127.0.0.1','port':25566}
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=JackBeePee&sessionId=' + mcsessionid + '&serverId=' + connhash + '...')
req = urllib.urlopen('http://session.minecraft.net/game/joinserver.jsp?user=JackBeePee&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')
#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 回答
看来你没有添加字符串或空字段;
关于包装格式的第二个
h
,Minecraft字符串的长度大于或等于2个字节 . 所以我假设一个空字符串只是short
,其值为0 .Edit: 上述方法不起作用;
s
需要一个长度;