Msg_Sender.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env python
  2. # -*- coding: cp936 -*-
  3. import string
  4. import struct
  5. import socket,time
  6. import random
  7. import xlrd
  8. import os
  9. class Byte:
  10. length=0
  11. byte=''
  12. byte1=''
  13. def reset(self):
  14. self.length=0
  15. self.byte=''
  16. def get_byte(self):
  17. return self.byte
  18. def get_length(self):
  19. return self.length
  20. def write_char(self,string,len1=None):
  21. slen=len(string)
  22. self.length+=slen
  23. Ascii_list=map(ord,string)
  24. #print Ascii_list
  25. for i in Ascii_list:
  26. #print i
  27. self.byte+=struct.pack('b',i)
  28. if type(len1)==int and len1>slen:
  29. temp_len=len1-slen
  30. for i in range(0,temp_len):
  31. self.byte+=struct.pack('b',0)
  32. self.length+=1
  33. self.byte+=struct.pack('b',0)
  34. self.length+=1
  35. def write_int32(self,integer):
  36. self.length+=4
  37. self.byte+=struct.pack('I',integer)
  38. def write_int16(self,integer):
  39. self.length+=2
  40. self.byte+=struct.pack('H',integer)
  41. def write_int8(self,integer):
  42. self.length+=1
  43. self.byte+=struct.pack('L',integer)
  44. class Msg_rpc(Byte):
  45. def __init__(self):
  46. self._socket = 0
  47. self._port = 11010
  48. self._host = '192.168.0.10'
  49. self._session_id = 0 #登录后的会话ID
  50. self._uid=random.randrange(100000000,1000000000)
  51. self._code = 0
  52. self.cmd=()
  53. self._header_len = 8
  54. self._last_action=''
  55. self.CODE_LOGIN = 901 #登录请求码
  56. self.CODE_LOGIN_REPLY = 951 #登录请求返回码
  57. self.CODE_MESSAGE = 912 #发送数据消息请求
  58. self.CODE_RPC_SEND = 913 #R
  59. self.CODE_RPC_REPLY = 914 #RPC协议回复
  60. #self.Setip('192.168.0.10',11010)
  61. #self.Setip('192.168.5.4',11001)
  62. #self.Mysocket()
  63. self.Login(self._uid,"username"+str(self._uid),"username"+str(self._uid))
  64. #self.Login(self._uid,"username"+str(self._uid),"username"+str(self._uid))
  65. #def Setip(self,host,port):
  66. #self._host=host
  67. #self._port=port
  68. def Mysocket(self):
  69. self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  70. self._socket.setblocking(10)
  71. socket.setdefaulttimeout(20)
  72. result=self._socket.connect((self._host, self._port))
  73. def Close(self):
  74. self._socket.close()
  75. def Send(self,is_reply=False):
  76. if not self._socket:
  77. print "socket wei connection\r\n"
  78. #if self._code != self.CODE_LOGIN and not self._session_id:
  79. # print "socket Error\r\n"
  80. self.write_int16(0)
  81. result=self._socket.send(self.get_header()+self.get_byte())
  82. self.reset()
  83. if not result:
  84. print "发送信息失败\r\n"
  85. if is_reply==True:
  86. return self.Read()
  87. else:
  88. return True
  89. def Read(self):
  90. #if not self._socket:
  91. # print "socket连接中断\r\n"
  92. if self._socket:
  93. data=self._socket.recv(4)
  94. if data:
  95. ll=struct.unpack('I',data)
  96. le=ll[0]
  97. return self.Pack_doing(self._socket.recv(le))
  98. def Pack_doing(self,data):
  99. self.cmd=()
  100. code=struct.unpack('H',data[4:6])
  101. if code[0]==self.CODE_LOGIN_REPLY:
  102. temp=struct.unpack('Q',data[8:])
  103. self._session_id=temp[0]
  104. #print "session_id:"+str(self._session_id)
  105. if code[0]==self.CODE_RPC_REPLY:
  106. fmt='%ds' %(len(data[8:])-struct.calcsize('3I'))
  107. #self.cmd=struct.unpack('3I',data[8:20])
  108. temp=struct.unpack(fmt,data[20:])
  109. #print "\r\n cmd:"+str(self.cmd)+"\r\n"
  110. #print "\r\n temp:"+temp[0]+"\r\n"
  111. return self.decode(temp[0])
  112. def code(self,code):
  113. self._code=int(code)
  114. def get_login(self):
  115. return self._session_id
  116. def get_header(self):
  117. self.length+=8
  118. self.byte1=struct.pack('I',self.length)+struct.pack('I',self.length)+struct.pack('H',self._code)+struct.pack('H',0)
  119. return self.byte1
  120. def Login(self,uid, username,password):
  121. self.code(self.CODE_LOGIN)
  122. self.write_int32(uid)
  123. self.write_char(username,32)
  124. self.write_char(username,32)
  125. self.Mysocket()
  126. self.Send(True)
  127. return self._session_id
  128. def Rpc(self,func_name,data,server_id=0):
  129. tag=random.randrange(100000,1000000000)
  130. self.code(self.CODE_RPC_SEND)
  131. self.write_int32(self._uid)
  132. self.write_int32(tag)
  133. self.write_int32(server_id)
  134. self.write_char(func_name,127)
  135. if data:
  136. #print "\r\ndata"+self.encode(data)
  137. self.write_char(self.encode(data))
  138. self._last_action=func_name
  139. return self.Send(True)
  140. def encode(self,dic,type=2):
  141. if type==1:
  142. return self._encode1(dic)
  143. else:
  144. return self._encode2(dic)
  145. def _encode1(self,dic):
  146. st=''
  147. comm=''
  148. for v in dic.values():
  149. if type(v)==dict:
  150. v['value']='/'.join(v.values())
  151. st+=comm
  152. st+=(self.filter1(v['key'])+'@=') if v['key']!=None else ''
  153. st+=self.filter1(v['value'])
  154. def _encode2(self,dic):
  155. st=''
  156. comm=''
  157. for (k,v) in dic.items():
  158. if type(v)==dict:
  159. v='/'.join(v.values())
  160. st+=comm
  161. st+= (self.filter1(k)+'@=') if k!=None else ''
  162. st+=self.filter1(v)
  163. comm='/'
  164. return st
  165. def filter1(self,strr,encode=True):
  166. strr=strr.strip()
  167. if strr=='':
  168. return strr
  169. if encode==True:
  170. strr1=strr.replace('@','@A')
  171. strr2=strr1.replace('/','@S')
  172. return strr2
  173. else:
  174. strr1=strr.replace('@S','/')
  175. strr2=strr1.replace('@A','@')
  176. return strr2
  177. def decode(self,strr,type=1):
  178. strr=strr.strip()
  179. if strr=='':
  180. return strr
  181. st_list=strr.split('/')
  182. result={}
  183. for v in st_list:
  184. if v.count('@=')!=0:
  185. st_list2=v.split('@=')
  186. key=self.filter1(st_list2[0],False)
  187. value=self.filter1(st_list2[1],False)
  188. result[key]=value
  189. else:
  190. key=None
  191. i=0
  192. for vv in self.cmd:
  193. result[i]=vv
  194. i+=1
  195. return result
  196. def send_phone_msg(self, phone, msg):
  197. if not phone and not msg:
  198. return False
  199. data=self.Rpc('send_phone_message',{'phone':phone,'msg':msg},48001)
  200. #print "\r\ndonate_room data:"+str(data)
  201. return data