This repository has been archived on 2024-12-08. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-08/src/network/http_old.py

53 lines
1.4 KiB
Python
Raw Normal View History

import asyncore
import socket
import time
requestCount = 0
parallel = 50
duration = 60
class HTTPClient(asyncore.dispatcher):
2019-09-03 15:11:24 +02:00
"""An asyncore dispatcher"""
port = 12345
def __init__(self, host, path, connect=True):
if not hasattr(self, '_map'):
asyncore.dispatcher.__init__(self)
if connect:
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, HTTPClient.port))
self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
def handle_close(self):
2019-09-03 15:11:24 +02:00
# pylint: disable=global-statement
global requestCount
requestCount += 1
self.close()
def handle_read(self):
2019-09-03 11:04:17 +02:00
# print self.recv(8192)
self.recv(8192)
def writable(self):
2019-09-03 15:11:24 +02:00
return len(self.buffer) > 0
def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
2019-09-03 11:04:17 +02:00
if __name__ == "__main__":
# initial fill
for i in range(parallel):
HTTPClient('127.0.0.1', '/')
start = time.time()
2019-09-03 15:11:24 +02:00
while time.time() - start < duration:
if len(asyncore.socket_map) < parallel:
for i in range(parallel - len(asyncore.socket_map)):
HTTPClient('127.0.0.1', '/')
print "Active connections: %i" % (len(asyncore.socket_map))
2019-09-03 11:04:17 +02:00
asyncore.loop(count=len(asyncore.socket_map) / 2)
if requestCount % 100 == 0:
print "Processed %i total messages" % (requestCount)