通过python和xmpp模拟用户发消息

来源:互联网 发布:ip查询软件下载 编辑:程序博客网 时间:2024/05/09 13:12

写在这里,免得以后用的时候到处找。

import sys,os,timeimport threading,xmppdef run(username,w,x):print "============================================"print "my thread is: "+str(w)+" times is "+str(x-1)print "============================================"time.sleep(1)cl=xmpp.Client(server='192.168.5.231',port=5222,debug=[])con=cl.connect()if not con:print "//////////////////////////"print "could not connect!"print "//////////////////////////"sys.exit()auth=cl.auth("yivanus"+str(w),'123456')if not auth:print "//////////////////////////"print 'could not authenticate!'print "//////////////////////////"sys.exit()for i in range(1,x):print ""print "my thread is "+str(w)+" sending in "+str(i)+" times"print ""id=cl.send(xmpp.protocol.Message(str(username)+'@192.168.5.231',"hello how are you,can i help you? what can i do for you?"+str(i)))time.sleep(5)cl.disconnect()def err():print "=========================================="print "usrage: python test.py username num times "print "=========================================="sys.exit()if __name__=="__main__":if len(sys.argv)!=4:err()if sys.argv[1]=="":err()if int(sys.argv[2])<=0:err()if int(sys.argv[3])<=0:err()print "=========================================="print "user : "+str(sys.argv[1])print "every user send message to "+sys.argv[1]+" times is :" +str(sys.argv[2])print "=========================================="worker=[]for w in range(1,int(sys.argv[2])+1):t=threading.Thread(target=run,args=(str(sys.argv[2]),w,int(sys.argv[3])+1))t.start()worker.append(t)for i in worker:i.join()print "=========================================="print "test over."print "=========================================="



原创粉丝点击