- 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)
- sLen = sLen.encode('hex-codec')
- sOpcode = sOpcode[len(sOpcode)-2:] + sOpcode[:2]
- sOpcode = sOpcode.decode('hex-codec')
- 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.
- 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.upper(), 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)
- pri
Python script to receive packets from nuConnector.
By: SpoT^ | Date: Jan 14 2008 19:14 | Format: None | Expires: never | Size: 3.14 KB | Hits: 1371
Latest pastes
1 hours ago
11 hours ago
1 days ago
2 days ago
2 days ago