Napolean

bloom-server.py

Feb 7th, 2023 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. import selectors
  2. import socket
  3. import time
  4. from multiprocessing import Process, RawValue, Lock
  5. from bloom_filter2 import BloomFilter
  6. from spamcro.counter import Counter
  7.  
  8. bind_address = "127.0.0.1"
  9. bind_port = 4500
  10.  
  11. try:
  12.     import mmap as mmap_mod
  13. except ImportError:
  14.     # Jython lacks mmap()
  15.     HAVE_MMAP = False
  16. else:
  17.     HAVE_MMAP = True
  18.  
  19. print(f"blf.mmap_available: {HAVE_MMAP}")
  20.  
  21. class BloomCount:
  22.     added = Counter()
  23.     filtered = Counter()
  24.     reqs = Counter()
  25.  
  26. blf = BloomFilter(max_elements=150000, error_rate=0.1, filename="./data/blf.bin")
  27. blfc = BloomCount()
  28. _lock = Lock()
  29.  
  30. sel = selectors.DefaultSelector()
  31.  
  32. def blf_add(value):
  33.     global blf, blfc, _lock
  34.     blfc.reqs.increment()
  35.     if value in blf:
  36.         blfc.filtered.increment()
  37.         return False
  38.     else:
  39.         with _lock:
  40.             blf.add(value)
  41.         blfc.added.increment()
  42.         return True
  43.  
  44. def blf_stat():
  45.     global blfc
  46.     return f"c:{str(blfc.added.value())}:f:{str(blfc.filtered.value())}:r:{str(blfc.reqs.value())}"
  47.    
  48. def accept(sock, mask):
  49.     global blfc
  50.     conn, addr = sock.accept()  # Should be ready
  51.     print('accepted', conn, 'from', addr)
  52.     conn.setblocking(False)
  53.     sel.register(conn, selectors.EVENT_READ, read)
  54.  
  55. def read(conn, mask):
  56.     global blfc
  57.     data = False
  58.     ttl = time.time() + 20
  59.  
  60.     while time.time() < ttl:
  61.         try:
  62.             data = conn.recv(1000)  # Should be ready
  63.             print("C:", data)
  64.  
  65.             if data[0] == 255:
  66.                 try:
  67.                     conn.send(f"blf ({blfc.reqs.value()} reqs, {blfc.added.value()} added, {blfc.filtered.value()} filtered)\r\n".encode('utf'))
  68.                     data = conn.recv(1000)  # Should be ready
  69.                     print("C:", data)
  70.                 except:
  71.                     data = False
  72.                 pass
  73.         except:
  74.             data = False
  75.             pass
  76.  
  77.         if data:
  78.             added = 0
  79.             count = 0
  80.             arr = data.decode('utf-8').split("\r\n")
  81.             for line in arr:
  82.                 if len(line) > 0:
  83.                     count = count + 1
  84.                     if blf_add(line):
  85.                         added = added + 1
  86.                         print(f"~ blf add({len(line)}) '{line}' | +1")
  87.                     else:
  88.                         print(f"~ blf add({len(line)}) '{line}' | -0")
  89.             try:
  90.                 if count > 0:
  91.                     conn.send(f"+{added}\r\n".encode('utf-8'))  # Hope it won't block
  92.             except:
  93.                 pass
  94.     print('closing', conn)
  95.     sel.unregister(conn)
  96.     conn.close()
  97.  
  98. def handle_packet(conn, data):
  99.     request_type = zfg.get_request_type(data[0])
  100.    
  101.     print('zfg.request:', request_type)
  102.    
  103.     return None
  104.  
  105. sock = socket.socket()
  106. sock.bind((bind_address, int(bind_port)))
  107. sock.listen(100)
  108. sock.setblocking(False)
  109. sel.register(sock, selectors.EVENT_READ, accept)
  110.  
  111. while True:
  112.     events = sel.select()
  113.     for key, mask in events:
  114.         callback = key.data
  115.         callback(key.fileobj, mask)
  116.  
Advertisement
Add Comment
Please, Sign In to add comment