1v1_tester.py 4.52 KB
#!/usr/bin/python
#-*-coding:utf-8-*-
import httplib,urllib;
import json
import os
import sys
import time
import random
#host = "127.0.0.1:2123"
host = "180.150.184.115:29100"
roomid = 1000
tid = 101
sid = 102

def create_room(conn, headers, roomid):
  body = {}
  body["roomid"] = roomid
  body["tid"] = tid
  body["uid"] = sid
  
  pa = json.dumps(body) 
  print "----create room post: ", pa
  conn.request(method="POST", url="/createRoom", body=pa, headers=headers);
  response = conn.getresponse();
  print "resp:",response.status
  data = response.read()
  if response.status == 200:
      print "成功!";
  else:
      print "失败";
  print "data:",data
  d = eval(data)
  print d

def connect_room(conn, headers, roomid, uid):
  body = {}

  body["roomid"] = roomid
  body["uid"] = uid
  body["data"] = ""
  
  pa = json.dumps(body) 

  print "---connect room post: ", pa
  conn.request(method="POST", url="/connectRoom", body=pa, headers=headers);
  response = conn.getresponse();
  print "resp:",response.status
  data = response.read()
  if response.status == 200:
      print "成功!";
  else:
      print "失败";
  print "data:",data
  d = eval(data)
  print d

def destroy_room(conn, headers, roomid):
  body = {}

  body["roomid"] = roomid
  
  pa = json.dumps(body) 

  print "---------destroy room post: ", pa
  conn.request(method="POST", url="/destroyRoom", body=pa, headers=headers);
  response = conn.getresponse();
  print "resp:",response.status
  data = response.read()
  if response.status == 200:
      print "成功!";
  else:
      print "失败";
  print "data:",data
  d = eval(data)
  print d

def get_room_info(conn, headers, roomid):
  body = {}  
  body["roomid"] = roomid
  pa = json.dumps(body) 

  print "----get room post: ", pa
  conn.request(method="POST", url="/getRoomInfo", body=pa, headers=headers);
  response = conn.getresponse();
  print "resp:",response.status
  data = response.read()
  if response.status == 200:
      print "成功!";
  else:
      print "失败";
  print "data:",data
  d = eval(data)
  print d

def send_im(conn, headers, roomid, dst, content):
  body = {}  
  body["roomid"] = roomid
  body["dst_uid"] = dst
  body["content"] = content
  pa = json.dumps(body) 

  print "------ send im post: ", pa
  conn.request(method="POST", url="/sendMessage", body=pa, headers=headers);
  response = conn.getresponse();
  print "resp:",response.status
  data = response.read()
  if response.status == 200:
      print "成功!";
  else:
      print "失败";
  print "data:",data
  d = eval(data)
  print d

def tester(argv1, argv2, argv3, argv4):
  me = argv1
  headers = {"Content-Type":"application/json;charset=utf-8", "Connection":"Keep-Alive"}
  conn = httplib.HTTPConnection(host)
  if me == "create":
    roomid = int(argv2)
    create_room(conn, headers, roomid)
  elif me == "connect":
    roomid = int(argv2)
    uid = int(argv3)
    connect_room(conn, headers, roomid, uid)
  elif me == "get":
    roomid = int(argv2)
    get_room_info(conn, headers, roomid)
  elif me == "destroy":
    roomid = int(argv2)
    destroy_room(conn, headers, roomid)
  elif me == "im":
    roomid = int(argv2)
    dst = argv3
    content = argv4 
    send_im(conn, headers, roomid, dst, content)

def looper():
  random.seed(time.time())
  headers = {"Content-Type":"application/json;charset=utf-8", "Connection":"Keep-Alive"}
  conn = httplib.HTTPConnection(host)

  open_dict = {}
  min_roomid = 11230
  max_roomid = 11240
  status = ["lock", "unlock","close"]
  rid = 0
  while True:
    time.sleep(2)
    rid = random.randint(min_roomid, max_roomid)
    r = random.randint(0,100)
    if r < 30:
      tester("create", str(rid),"","")
    elif r < 50:
      rr = random.randint(0,100)
      uid = random.randint(0,100)
      if rr < 30:
        uid = tid
      elif rr < 70:
        uid = sid
      tester("connect", str(rid), str(uid),"")
    elif r < 75:
      tester("destroy", str(rid),"","")
    elif r < 90:
      tester("get", str(rid),"","")
    else:
      rr = random.randint(0,100)
      dst = "1212,3232"
      if rr < 30:
        dst = str(sid)
      elif rr < 70:
        dst = str(sid) + "," + str(tid)
      content = "random content" + str(rr)
      tester("im", str(rid), dst, content)

if __name__ == "__main__":

	do_loop = True
	if not do_loop:
		argv1 = ""
		if len(sys.argv) >= 2:
			argv1 = sys.argv[1]
			argv2 = ""
		if len(sys.argv) > 2:
			argv2 = sys.argv[2]
			argv3 = ""
		if len(sys.argv) > 3:
			argv3 = sys.argv[3]
			argv4 = ""
		if len(sys.argv) > 4:
			argv4 = sys.argv[4]
    		tester(argv1, argv2, argv3, argv4)
	else:
		looper()