1v1_tester.py
4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/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()