edit modelo Visita

registro request, metodo y codigo si hubiere.
This commit is contained in:
jp.av.dev 2022-05-18 01:41:55 -04:00
parent 3fabf8b593
commit 865b4a30ce
2 changed files with 140 additions and 28 deletions

View File

@ -86,30 +86,52 @@ def print_ipinfo(ip, tkn=True):
if contad < 1: if contad < 1:
for ind, val in enumerate(tupla): for ind, val in enumerate(tupla):
if ind == 0: if ind == 0:
print(f'{co_RedB}-------------------------' print(f'{co_BluD} _____________________________'
f'---------------------------{co_rst}') f'________________________________{co_rst}')
for dato in str(val).split(';'): for dato in str(val).split(';'):
print(f'{co_Blu}| {co_BluB}{dato.split("=")[0].ljust(12)}' print(f'{co_Blu}| {co_BluB}{dato.split("=")[0].ljust(12)}'
f'{co_Blu}| {co_rst}{dato.split("=")[1]}{co_rst}') f'{co_Blu}| {co_rst}{dato.split("=")[1]}{co_rst}')
print(f'{co_BluD}-------------------------' print(f'{co_BluD} ________________________ __'
f'---------------------------{co_rst}') f'_________ __________ _____________{co_rst}')
print(f'{co_Blu}|{co_YelB} Codigo html {co_Blu}|' print(f'{co_Blu}|{co_YelB} Fecha Visita {co_rst}'
f'{co_YelB} Fecha Visita{co_rst}') f'{co_Blu}|{co_YelB}Codigo html{co_Blu}|'
f'{co_YelB} Metodo {co_rst}'
f'{co_Blu}|{co_YelB} Request {co_rst}')
print(f'{co_Blu}|------------------------|--'
f'---------|----------|-------------{co_rst}')
else: else:
codig = str(val).split(',')[2].split('=')[1] # aqui modificar para representar las nuevas columnas de tabla visita
fecha = str(val).split(',')[3].split('=')[1] codig = str(val).split(',')[2].split('=')[1].center(11)
print(f'{co_Blu}|{co_GrnB} {codig} {co_Blu}|' fecha = str(val).split(',')[3].split('=')[1].ljust(24)
f'{co_Grn} {fecha}{co_rst}') metodo = (str(val).split(',')[4].split('=')[1])
metodo = '---'.center(10) if metodo == 'None' else metodo.center(10)
request = str(val).split(',')[5].split('=')[1]
request = request[:86]+'...' if len(request) > 90 else request
request = '---' if request == 'None' else request
#if len(request) > 90:
# request = request[:86]+'...'
print(f'{co_Blu}|{co_Yel}{fecha}{co_rst}'
f'{co_Blu}|{co_GrnB}{codig}'
f'{co_Blu}|{co_Grn}{metodo}{co_rst}'
f'{co_Blu}|{co_Grn}{request}{co_rst}')
else: else:
for ind, val in enumerate(tupla): for ind, val in enumerate(tupla):
if ind > 0: if ind > 0:
codig = str(val).split(',')[2].split('=')[1] # aqui modificar para representar las nuevas columnas de tabla visita
fecha = str(val).split(',')[3].split('=')[1] codig = str(val).split(',')[2].split('=')[1].center(11)
print(f'{co_Blu}|{co_GrnB} {codig} {co_Blu}|' fecha = str(val).split(',')[3].split('=')[1].ljust(24)
f'{co_Grn} {fecha}{co_rst}') metodo = (str(val).split(',')[4].split('=')[1])
metodo = '---'.center(10) if metodo == 'None' else metodo.center(10)
request = str(val).split(',')[5].split('=')[1]
request = request[:86]+'...' if len(request) > 90 else request
request = '---' if request == 'None' else request
print(f'{co_Blu}|{co_Yel}{fecha}{co_rst}'
f'{co_Blu}|{co_GrnB}{codig}'
f'{co_Blu}|{co_Grn}{metodo}{co_rst}'
f'{co_Blu}|{co_Grn}{request}{co_rst}')
contad+=1 contad+=1
print(f'{co_RedB}-------------------------' print(f'{co_RedB}-------------------------------'
f'---------------------------{co_rst}') f'-------------------------------{co_rst}')
else: else:
print('otra wea: ', type(ip_info)) print('otra wea: ', type(ip_info))
else: else:

View File

@ -58,8 +58,10 @@ class Visita(Base):
__tablename__ = 'visita' __tablename__ = 'visita'
id = Column(Integer, Sequence('visita_id_seq'), primary_key=True) id = Column(Integer, Sequence('visita_id_seq'), primary_key=True)
ip = Column(String, ForeignKey('registro.ip')) ip = Column(String, ForeignKey('registro.ip'))
html = Column(Integer) cod_html = Column(Integer)
fecha = Column(Integer) fecha = Column(Integer)
metodo = Column(String, default='---')
consulta = Column(String, default='---')
registro = Column(Integer, default=0) registro = Column(Integer, default=0)
visita_ip = relationship("Registro", back_populates="visitas") visita_ip = relationship("Registro", back_populates="visitas")
@ -72,7 +74,8 @@ class Visita(Base):
def __repr__(self) -> str: def __repr__(self) -> str:
try: try:
rep = f'id={self.id},ip={self.ip},html={self.html},fecha={self.get_fecha()}' rep = f'id={self.id},ip={self.ip},html={self.cod_html},'\
f'fecha={self.get_fecha()},metodo={self.metodo},request={self.consulta}'
return rep return rep
except Exception as ex: except Exception as ex:
print('Exception :', ex) print('Exception :', ex)
@ -142,12 +145,48 @@ def carga_access_log(log):
for linea in lista: for linea in lista:
ip = linea.split(' ')[0] ip = linea.split(' ')[0]
if ipl.filtro_ip_propia(ip): if ipl.filtro_ip_propia(ip):
fecha = fecha_access_to_epoch(linea.split(' ')[3][1:]) try:
codigo= int(linea.split('"')[2].split(' ')[1]) ip = linea.split(' ')[0]
except Exception as ex:
ip = None
print('Exception split IP', ex)
try:
metodo = linea.split('"')[1].split(' ')[0]
if len(metodo) > 10 or len(metodo) < 2:
metodo = '---'
except Exception as ex:
metodo = '---'
try:
url = linea.split('"')[1].split(' ')[1]
if len(url) > 254:
url = url[:252]+'...'
except Exception as ex:
url = '---'
try:
codigo = int(linea.split('"')[2].split(' ')[1])
if len(str(codigo)) != 3:
codigo = 0
except Exception as ex:
codigo = 0
try:
fecha = linea.split(' ')[3][1:]
fecha = fecha_access_to_epoch(fecha)
except Exception as ex:
fecha = None
print('Exception split Fecha:', ex)
if ip_registrada(ip): if ip_registrada(ip):
session.add(Visita(ip=ip, html=codigo, fecha=fecha, registro=1)) session.add(Visita(ip=ip,
cod_html=codigo,
fecha=fecha,
metodo=metodo,
consulta=url,
registro=1))
else: else:
session.add(Visita(ip=ip, html=codigo, fecha=fecha)) session.add(Visita(ip=ip,
cod_html=codigo,
fecha=fecha,
metodo=metodo,
consulta=url))
session.commit() session.commit()
print(f'{ipl.co_Grn}Carga completa.. borrando log{ipl.co_rst}\n') print(f'{ipl.co_Grn}Carga completa.. borrando log{ipl.co_rst}\n')
os.remove(log) os.remove(log)
@ -168,14 +207,65 @@ def carga_error_logs(log):
try: try:
with open(log, 'r') as lista: with open(log, 'r') as lista:
for linea in lista: for linea in lista:
ip = linea.split('client: ')[1].split(',')[0] linea = linea.split('\n')[0]
if (linea.rfind('[notice]') > 0 or linea.rfind('[crit]') > 0):
if linea.find('[crit]') > 0:
try:
ip = linea.split('client: ')[1].split(',')[0]
except Exception as ex:
print('Exception Ip error_log: ', ex)
ip = None
try:
fecha = ' '.join(linea.split(' ')[0:2])
except Exception:
fecha = None
try:
url = linea.split('"')[1].split(' ')[1]
if len(url) > 254:
url = url[:252]+'...'
except Exception:
url = ' '.join(linea.split(' ')[5:])
#url = '---'
try:
metodo = linea.split('"')[1].split(' ')[0]
except Exception:
metodo = '---'
else:
try:
ip = linea.split('client: ')[1].split(',')[0]
except Exception as ex:
print('Exception Ip error_log: ', ex)
ip = None
try:
fecha = ' '.join(linea.split(' ')[0:2])
except Exception:
fecha = None
try:
metodo = linea.split('request: "')[1].split(' ')[0]
except Exception:
metodo = '---'
try:
url = linea.split('"')[1].split(' ')[0]
if len(url) > 254:
url = url[:252]+'...'
except Exception:
url = '---'
if ipl.filtro_ip_propia(ip): if ipl.filtro_ip_propia(ip):
fecha = fecha_error_to_epoch(' '.join(linea.split()[0:2])) fecha = int(fecha_error_to_epoch(fecha))
codigo = 300 codigo = 0
if ip_registrada(ip): if ip_registrada(ip):
session.add(Visita(ip=ip, html=codigo, fecha=fecha, registro=1)) session.add(Visita(ip=ip,
cod_html=codigo,
fecha=fecha,
consulta=url,
metodo=metodo,
registro=1))
else: else:
session.add(Visita(ip=ip, html=codigo, fecha=fecha)) session.add(Visita(ip=ip,
cod_html=codigo,
fecha=fecha,
consulta=url,
metodo=metodo))
session.commit() session.commit()
print(f'{ipl.co_Grn}Carga completa.. borrando log{ipl.co_rst}\n') print(f'{ipl.co_Grn}Carga completa.. borrando log{ipl.co_rst}\n')
os.remove(log) os.remove(log)
@ -247,7 +337,7 @@ def consulta_db(ip):
def test_db(): def test_db():
try: try:
session.add(Visita(ip='dummy_ip', html=000, fecha=int(time.mktime(time.localtime())))) session.add(Visita(ip='dummy_ip', cod_html=000, fecha=int(time.mktime(time.localtime()))))
session.commit() session.commit()
session.add(Registro(ip ='dummy_ip')) session.add(Registro(ip ='dummy_ip'))
session.commit() session.commit()