|
发表于 2005-9-15 14:37:45
|
显示全部楼层
- from socket import *
- import sys
- import os
- def prompt():
- print """
- only users with blank password can use it,because i
- have not added the password validation yet.
- chinese message seems invalid.
-
- """
- def TransferMsg(msg):
- try:
- if msg[0] == '[' and msg[-1] == ']':
- temp_msg = msg[1:-1]
- pos = temp_msg.find('*')
- if pos >= 0:
- stimes = temp_msg[pos + 1:]
- ntimes = int(stimes)
- return temp_msg[0 : pos] * ntimes
- except:
- return msg
-
- def SendMsg(msg,recv_buf=2048):
- s.send(msg+'\r\n')
- print 'SEND:',msg
- #print 'RECV:',s.recv(recv_buf)
-
- def ShowRecvMsg():
- s.setblocking(0)
- try:
- while(1):
- recv_msg = s.recv(1024)
- if recv_msg.strip() <> '':
- print 'RECV:', recv_msg
- except:
- pass
- s.setblocking(1)
-
- def InitJabber(server,resource,user):
- SendMsg('<stream:stream to="'+server+'" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">')
- SendMsg('<iq type="get" id="JCOM_0"><query xmlns="jabber:iq:auth"><username>'+user+'</username></query></iq>')
- SendMsg('<iq type="set" id="JCOM_1"><query xmlns="jabber:iq:auth"><username>'+user+'</username><digest/><resource>'+resource+'</resource></query></iq>')
- def JbSendTo(to_user,msg):
- SendMsg('<message id="JCOM_4" to="'+to_user+'" type="unsavechat"><body>'+msg+'</body></message>')
- if (__name__ == '__main__'):
- try:
- if sys.platform == 'win32':
- os.system('cls')
- else:
- os.system('clear')
- prompt() # give some infomations.
- svr_name = raw_input(r'enter server name(e.g. oraserver):')
- #svr_ip = raw_input(r'enter server ip(e.g. 192.0.0.2):')
- while(1):
- try:
- svr_ports = raw_input(r'enter server port(5222):')
- if svr_ports == '':
- svr_ports = '5222'
- svr_porti = int(svr_ports)
- break
- except:
- print 'please enter a valid number.'
- #print svr_porti
- res = raw_input(r'enter resource name(e.g. EIM_Work):')
- usr_name = raw_input(r'enter user name(you logon name):')
- s = socket()
- s.connect((svr_name,svr_porti))
- print 'connected...\r\n'
- except Exception,e:
- print 'connect error :' ,e
- else:
- try:
- InitJabber(svr_name,res,usr_name)
- ShowRecvMsg()
- print 'init jabber finished...'
- print 'ready...'
-
- while(1):
- sendto = raw_input(r'recv user(e.g. fjg@nsoa blank to exit):')
- if sendto == '':
- break
- while(1):
- sendmsg = raw_input(r'msg to send:')
- sendmsg = TransferMsg(sendmsg)
- if sendmsg == '':
- break;
-
-
- JbSendTo(sendto,sendmsg)
- print 'msg: '+ sendmsg + ' sended...'
- ShowRecvMsg()
-
-
- except Exception,e:
- print 'error :',e
- s.close()
-
复制代码
偶直接用TCP和我们公司的服务器连接. |
|