2012-07-05 75 views
0

我使用Python在線發現了以下腳本。它所做的是嘗試連接到MineCraft服務器,首先通過發送「握手」,然後發送登錄請求。協議規格可以在這裏找到:http://wiki.vg/Protocol在Python中發送登錄數據包到Minecraft服務器時的錯誤

無論如何,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

。相反,你應該發送編碼字符串中的字節數。此外,你應該爲清晰起見,用「!」而不是「>」,如「!」是用來表示「網絡秩序」,這這是。所以這...

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 
+0

謝謝,但這提供了以下錯誤: '回溯(最近通話最後一個): 文件 「C:\ Documents和Settings \馬修\桌面\ Minecraft_Connect.py」,線111,在 數據= S .recv(1024) 錯誤:[Errno 10053]建立的連接被您的主機中的軟件' – user1504469 2012-07-05 23:33:16

+0

中止。啊,很抱歉因爲離開了五天。扁桃體切除術:/它每次都會拋出這個異常嗎? – 2012-07-10 22:21:00

相關問題