# Pastebin PeAzlswb import socket import ssl, time from select import select from cffi import FFI ffi = FFI() # SET VARIABLES packet = ["GET https://baroquesoftware.com/index.html HTTP/1.1", "Host: www.baroquesoftware.com", "Connection: Keep-Alive", "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)", ""] packet = "\r\n".join(packet) + "\r\n" HOST, PORT = 'vrsketch.eu', 443 #HOST, PORT = 'google.com', 443 total = [] context = ssl.create_default_context() glob_buf = ffi.buffer(ffi.new("char[8192]"), 8192) class SocketIO(object): def __init__(self, sock): self._s = sock self._buf = glob_buf # ffi.buffer(ffi.new("char[8192]"), 8192) self._cache = [] def readline(self): if self._cache: return self._cache.pop() try: self._s.recv_into(self._buf, 8192) except Exception as e: if "UNEXPECTED_EOF" in str(e): self._cache = [b''] return b'' raise self._cache.extend(bytes(self._buf).split(b"\n")) self._cache.reverse() def makefile(sock, mode): #return sock.makefile(mode) assert mode == "rb" return SocketIO(sock) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) wrappedSocket = context.wrap_socket(sock, server_hostname=HOST) wrappedSocket.setblocking(0) conn = wrappedSocket.connect_ex((HOST, PORT)) fp = makefile(wrappedSocket, "rb") def send_all(sock, v): cur = 0 while True: a, b, c = select([], [sock.fileno()], []) try: cur += sock.send(v[cur:]) except Exception as e: if isinstance(e, OSError) and e.errno == 2: continue else: raise if cur == len(v): break xx = [None] COUNT = 10000 for k in range(300): # CREATE SOCKET t0 = time.process_time() for i in range(COUNT): # WRAP SOCKET #wrappedSocket = sock # CONNECT AND PRINT REPLY while True: try: send_all(wrappedSocket, packet.encode("utf8")) except Exception as e: # reconnect? #print("reconnecting (%s)" % str(e)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) wrappedSocket = context.wrap_socket(sock, server_hostname=HOST) wrappedSocket.setblocking(0) conn = wrappedSocket.connect_ex((HOST, PORT)) fp = makefile(wrappedSocket, "rb") else: break #print(packet) while True: try: line = fp.readline() if line == b'\t\n': break except OSError as e: if e.errno == 2: continue raise if not line: break #l = [] #while True: # a, b, c = select([wrappedSocket.fileno()], [], [], 0.0) # if not a: # break # r = wrappedSocket.recv(8192) # if not r: # break # l.append(r.decode("utf8")) ##print("".join(l)) #xx[0] = "".join(l).split("\n") # CLOSE SOCKET CONNECTION t1 = time.process_time() total.append(t1 - t0) print("%.3f %d req/s" % (t1 - t0, COUNT/(t1 - t0))) wrappedSocket.close() print("Average: %.3f" % (sum(total)/len(total))) print("Average skipping 10: %.3f" % (sum(total[10:])/len(total[10:])))