Added JSONStorage to keep results, fixed trouble with IpGenerator

This commit is contained in:
Zloooy 2019-01-28 21:45:21 +03:00
parent ee2de0decf
commit acbfb6607b
3 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,14 @@
from abc import ABC, abstractmethod
class AbstractStorage(ABC):
@classmethod
@abstractmethod
def put_responce(self, address, responce):
pass
@classmethod
@abstractmethod
def save(self):
pass

23
storage/JSONStorage.py Normal file
View File

@ -0,0 +1,23 @@
from AbstractStorage import AbstractStorage
import json
from threading import RLock
class JSONStorage(AbstractStorage):
def __init__(self, path):
self.path = path
self.respdict = dict()
self.lock = RLock()
def put_responce(self, address, responce):
ip, port = address
if ip not in self.respdict.keys():
self.respdict[ip] = {"open": [], "close": []}
self.respdict[ip]["open" if responce != 0 else "close"].append(port)
def save(self):
print("saving")
with open(self.path, "w") as f:
json.dump(self.respdict, f)
self.respdict = {}

6
storage/__init__.py Normal file
View File

@ -0,0 +1,6 @@
import sys
import os
fil = __file__[:__file__.rindex(os.sep)]
sys.path.insert(0,fil)