Public paste
Python script to receive packets from nuConnector.
By: SpoT^ | Date: Jan 14 2008 19:14 | Format: None | Expires: never | Size: 3.14 KB | Hits: 672

  1. import socket
  2. import struct
  3. class Network(object):
  4.     def __init__(self, sock=None):
  5.         if sock is None:
  6.             self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7.         else :
  8.             self.sock = sock
  9.  
  10.         self.NetworkBuffer = ''
  11.         self.SpawnBuffer1 = None
  12.         self.SpawnBuffer2 = None
  13.        
  14.     def Connect(self, host, port):
  15.         self.sock.connect((host, port))
  16.  
  17.     def Send(self, Message):
  18.         TotalSent = 0
  19.         MessageLength = len(Message)
  20.         while TotalSent < MessageLength:
  21.             Sent = self.sock.send(Message[TotalSent:])
  22.             if Sent == 0:
  23.                 raise RuntimeError, "socket connection broken"
  24.             TotalSent = TotalSent + Sent
  25.    
  26.     def InjectPacket (self, sOpcode, sData, blnToServer, blnEncrypted):
  27.         sLen = struct.pack('<h', len(sData)/2)
  28.         sLen = sLen.encode('hex-codec')
  29.         sOpcode = sOpcode[len(sOpcode)-2:] + sOpcode[:2]
  30.         sOpcode = sOpcode.decode('hex-codec')
  31.         if blnEncrypted == True:
  32.             if blnToServer == True:
  33.                 sPacket = sLen + sOpcode + "0300" + sData
  34.             else :
  35.                 sPacket = sLen + sOpcode + "0400" + sData
  36.         else :
  37.             if blnToServer == True:
  38.                 sPacket = sLen + sOpcode + "0100" + sData
  39.             else :
  40.                 sPacket = sLen + sOpcode + "0200" + sData
  41.         self.Send(sPacket.decode('hex-codec'))
  42.  
  43.     def PeekLength(self, sData):
  44.         try:
  45.             iLength = long(ord(sData[1:2])) * 256 + long(ord(sData[0:1]))
  46.             iLength = iLength + 6
  47.         except:
  48.             iLength = 0
  49.         return iLength
  50.                        
  51.     def EmptyBuffer(self):
  52.         rData = self.sock.recv(4096)
  53.         if rData == 0:
  54.             return 1
  55.         rData = self.NetworkBuffer + rData
  56.         while True:
  57.             iLength = self.PeekLength(rData)
  58.             sData = rData[:iLength]
  59.             if len(sData) == iLength:
  60.                 if not sData == '':
  61.                     self.rcvPacket(sData)
  62.                     rData = rData[iLength:]
  63.             if iLength == 0:
  64.                 break
  65.         self.NetworkBuffer = rData
  66.  
  67.     def rcvPacket(self, sData):
  68.         if ord(sData[4:5]) == 1:
  69.             bDirection = False
  70.         else :
  71.             bDirection = True
  72.  
  73.         sOpcode = self.StringToHex(sData[2:4])
  74.         sOpcode = sOpcode[2:] + sOpcode[:2]
  75.         sData = self.StringToHex(sData[6:])
  76.         sOpcode = sData[3:4] + sData[2:3]
  77.         sData = sData[6:]
  78.         #Here you can process the received packets.
  79.  
  80.  
  81.     def InjectNotice(self, sMessage):
  82.         sData = struct.pack('<b h', 7, len(sMessage))
  83.         sText = ''
  84.         for letter in sMessage:
  85.             sText += letter.encode('hex-codec') + "00"
  86.         sPacket = sData.encode('hex-codec') + sText
  87.         self.InjectPacket('3667', sPacket.upper(), False, True)
  88.  
  89. network = Network()
  90. network.Connect("127.0.0.1", 22580) #connect to nuConnector
  91. # while loop to keep emptying the network buffers.
  92. while 1:
  93.     Message = raw_input("Please enter a Notice.")
  94.     network.InjectNotice(Message)
  95.     pri
Latest pastes
14 hours ago
16 hours ago
16 hours ago
1 days ago
1 days ago