[root@localhost ~]# cat netbench.py
# encoding: utf-8
import sys
import socket
import select
import time
import argparse
def netbench():
s = socket.socket()
s.connect(('r.domob.cn', 80))
s.send('GET /bbb HTTP/1.1\r\nHost: r.domob.cn\r\n\r\n')
data = s.recv(2048)
s.close()
print data
def run(hostname, port, desiredConnection):
currentConnection = 0
epoll = select.epoll()
connections = {}
while True:
while currentConnection < desiredConnection:
s = socket.socket()
try:
s.connect((hostname, port))
currentConnection += 1
connections[s.fileno()] = s
epoll.register(s, select.POLLIN|select.EPOLLERR)
print "%d connections made, fileno=%d" % (currentConnection, s.fileno())
except Exception,e:
print e
time.sleep(1)
continue
fd_events = epoll.poll()
for sock, event in fd_events:
s = connections[sock]
s.close()
del connections[sock]
currentConnection -= 1
print "CLOSED --- fd=%d. with event %d, left %d connections" % (sock, event, currentConnection)
def parse_arg():
parser = argparse.ArgumentParser(description='yet another bench mark tool.')
parser.add_argument('--host', required=True,
help='target hostname or ip')
parser.add_argument('-p', '--port', required=True, type=int,
help='target port')
parser.add_argument('-c', '--connections', required=True, type=int,
help='Number of connections to make')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_arg()
print args.host
run(args.host, args.port, args.connections)