- import socket
- import struct
- class Network(object):
- def __init__(self, sock=None):
- if sock is None:
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- else :
- self.sock = sock
- self.NetworkBuffer = ''
- self.SpawnBuffer1 = None
- self.SpawnBuffer2 = None
- def Connect(self, host, port):
- self.sock.connect((host, port))
- def Send(self, Message):
- TotalSent = 0
- MessageLength = len(Message)
- while TotalSent < MessageLength:
- Sent = self.sock.send(Message[TotalSent:])
- if Sent == 0:
- raise RuntimeError, "socket connection broken"
- TotalSent = TotalSent + Sent
- def InjectPacket (self, sOpcode, sData, blnToServer, blnEncrypted):
- sLen = struct.pack('<h',len(sData)/2).encode('hex-codec')
- sOpcode = sOpcode[len(sOpcode)-2:] + sOpcode[:2]
- print "sOpcode: " + sOpcode
- if blnEncrypted == True:
- if blnToServer == True:
- sPacket = sLen + sOpcode + "0300" + sData
- else :
- sPacket = sLen + sOpcode + "0400" + sData
- else :
- if blnToServer == True:
- sPacket = sLen + sOpcode + "0100" + sData
- else :
- sPacket = sLen + sOpcode + "0200" + sData
- self.Send(sPacket.decode('hex-codec'))
- def PeekLength(self, sData):
- try:
- iLength = long(ord(sData[1:2])) * 256 + long(ord(sData[0:1]))
- iLength = iLength + 6
- except:
- iLength = 0
- return iLength
- def EmptyBuffer(self):
- rData = self.sock.recv(4096)
- if rData == 0:
- return 1
- rData = self.NetworkBuffer + rData
- while True:
- iLength = self.PeekLength(rData)
- sData = rData[:iLength]
- if len(sData) == iLength:
- if not sData == '':
- self.rcvPacket(sData)
- rData = rData[iLength:]
- if iLength == 0:
- break
- self.NetworkBuffer = rData
- def rcvPacket(self, sData):
- if ord(sData[4:5]) == 1:
- bDirection = False
- else :
- bDirection = True
- sOpcode = self.StringToHex(sData[2:4])
- sOpcode = sOpcode[2:] + sOpcode[:2]
- sData = self.StringToHex(sData[6:])
- sOpcode = sData[3:4] + sData[2:3]
- sData = sData[6:]
- #Here you can process the received packets
- #The opcode is hex encoded, but the sData isn't.
- #If you want sData to be hex encoded, just uncomment the next line
- #sData = sData.encode('hex-codec')
- def InjectNotice(self, sMessage):
- sData = struct.pack('<b h', 7, len(sMessage))
- sText = ''
- for letter in sMessage:
- sText += letter.encode('hex-codec') + "00"
- sPacket = sData.encode('hex-codec') + sText
- self.InjectPacket('3667', sPacket, False, True)
- network = Network()
- network.Connect("127.0.0.1", 22580) #connect to nuConnector
- # while loop to keep emptying the network buffers.
- while 1:
- Message = raw_input("Please enter a Notice.")
- network.InjectNotice(Message)
lol
By: lal | Date: Jan 14 2008 19:52 | Format: None | Expires: never | Size: 3.26 KB | Hits: 1330
Latest pastes
51 minutes ago
10 hours ago
1 days ago
2 days ago
2 days ago