Chinaunix首页 | 论坛 | 博客
  • 博客访问: 534719
  • 博文数量: 71
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 159
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-13 12:37
个人简介

aaaaaaaaa

文章分类

全部博文(71)

文章存档

2013年(71)

我的朋友

分类: Python/Ruby

2013-07-24 12:41:05

原文地址:python构造网络包 作者:yyguzhou


点击(此处)折叠或打开

  1. #!/usr/bin/python

  2. from struct import *
  3. from socket import *
  4. from sys import *

  5. class DBPkgHead:
  6.     def __init__(self):
  7.         self.Length = 47
  8.     def SetLength(self, length):
  9.         self.Length = length
  10.     def GetLength(self):
  11.         return self.Lenght
  12.     def GetStr(self):
  13.         self.s = pack('!H', self.Length)
  14.         self.s += pack('!B', 0)
  15.         self.s += pack('!H', 0)
  16.         self.s += pack('!I', 0)
  17.         self.s += pack('!H', 0)
  18.         self.s += pack('!I', 0)
  19.         self.s += pack('!H', 0)
  20.         self.s += pack('!I', 0)
  21.         self.s += pack('!H', 0)
  22.         self.s += pack('!B', 0)
  23.         self.s += pack('!B', 0)
  24.         self.s += pack('!H', 0)
  25.         return self.s

  26. class RelayPkgHead:
  27.     def __init__(self):
  28.         pass
  29.     def GetStr(self):
  30.         self.s = pack('!H', 0)
  31.         self.s += pack('!H', 0)
  32.         self.s += pack('!H', 0)
  33.         self.s += pack('!H', 0)
  34.         return self.s

  35. class CldPkgHead:
  36.     def __init__(self):
  37.         self.Command = 0
  38.     def SetCommand(self, cmd):
  39.         self.Command = cmd
  40.     def GetCommand(self):
  41.         return self.Command
  42.     def GetStr(self):
  43.         self.s = pack('!H', 0)
  44.         self.s += pack('!H', self.Command)
  45.         self.s += pack('!H', 0)
  46.         self.s += pack('!I', 0)
  47.         return self.s

  48. class ServerPkg:
  49.     def __init__(self):
  50.         self.Head = 0x0A
  51.         self.DBPkgHead = DBPkgHead()
  52.         self.RelayPkgHead = RelayPkgHead()
  53.         self.CldPkgHead = CldPkgHead()
  54.         self.Body = ''
  55.         self.Tail = 0x03
  56.     def SetBody(self, body):
  57.         self.Body = body
  58.         self.DBPkgHead.SetLength(47 + len(body))
  59.     def SetCommand(self, cmd):
  60.         self.CldPkgHead.SetCommand(cmd)
  61.     def GetPkg(self):
  62.         return pack('!B', self.Head) + self.DBPkgHead.GetStr() + self.RelayPkgHead.GetStr() \
  63.                 + self.CldPkgHead.GetStr() + self.Body + pack('!B', self.Tail)

  64. dest = ('172.27.12.141', 35000)

  65. def Useage(prog):
  66.     print 'Room enter times proto tool'
  67.     print 'Useage: %s CmdID Uin RoomID' % prog
  68.     print ' CmdID'
  69.     print ' 1: Get Uin\'s RoomID enter times'
  70.     print ' 2: Increase Uin\'s RoomID enter times'

  71. def main():
  72.     if len(argv) != 4:
  73.         Useage(argv[0])
  74.         exit(-1)
  75.     pkg = ServerPkg()
  76.     pkg.SetCommand(0x018C)
  77.     cmd = int(argv[1])
  78.     uin = int(argv[2])
  79.     room = int(argv[3])
  80.     body = pack('!BII', cmd, uin, room)
  81.     pkg.SetBody(body)
  82.     sock = socket(AF_INET, SOCK_DGRAM)
  83.     sock.sendto(pkg.GetPkg(), dest)
  84.     if cmd == 2:
  85.         exit(0)
  86.     s, src = sock.recvfrom(2048)
  87.     res = s[46:len(s)-1]
  88.     cmd, result, uin, room, times, = unpack('!BBIIH', res)
  89.     print 'Cmd\t%u' % cmd
  90.     print 'Result\t%u' % result
  91.     print 'Uin\t%u' % uin
  92.     print 'Room\t%u' % room
  93.     print 'Times\t%u' % times

  94. if __name__ == "__main__":
  95.     main()

阅读(463) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~