pyCraft/PacketManager.py

63 lines
1.8 KiB
Python
Raw Normal View History

2012-04-05 04:56:21 +02:00
import socket
import struct
import sys
def sendString( packetid, string, socket):
length = struct.pack('!h', string.__len__())
socket.send(packetid)
socket.send(length)
socket.send(string.encode('utf-16be','strict'))
def readStringFromSocket(socket):
response = socket.recv(256)
packetid = response[0]
response = response[3:]
response = response.decode("utf-16be")
return {'packetid' : packetid, 'string' : response}
def sendLoginRequest(socket, username):
socket.send("\x01")
socket.send(struct.pack('!i', 29))
socket.send(struct.pack('!h', username.__len__()))
socket.send(username.encode('utf-16be','strict'))
socket.send(struct.pack('!h', "hello".__len__()))
socket.send("hello".encode('utf-16be','strict'))
socket.send(struct.pack('!i', 0))
socket.send(struct.pack('!i', 1))
socket.send(struct.pack('!b', 0))
socket.send(struct.pack('!B', 1))
socket.send(struct.pack('!B', 0))
def ReceiveLoginRequest(socket):
response = socket.recv(256)
packetid = response[0]
if(packetid != "\x01"):
return ""
response = response[1:]
2012-04-10 21:14:32 +02:00
def handle00(socket):
print "Sending keep alive response"
2012-04-05 04:56:21 +02:00
socket.send("\x00")
2012-04-10 21:14:32 +02:00
socket.send(struct.pack('!i', 0))
2012-04-05 04:56:21 +02:00
2012-04-10 21:14:32 +02:00
def handleFF(socket, response):
response = response[3:]
print "Length: " + str(response.__len__())
print response
response = response.decode("utf-16be", 'strict')
print response
return response
2012-04-05 04:56:21 +02:00
def handleIncomingPacket(socket):
response = socket.recv(256)
if not response:
handleIncomingPacket(socket)
packetid = response[0]
print str(ord(packetid))
if(packetid == "\x00"):
handle00(socket, response)
elif(packetid == "\xFF"):
handleFF(socket, response)