31 lines
908 B
Python
31 lines
908 B
Python
# -*- coding:utf-8 -*-
|
|
import os
|
|
from tinydb import TinyDB, where
|
|
from tinydb.storages import JSONStorage
|
|
from tinydb.middlewares import CachingMiddleware
|
|
from collections import namedtuple
|
|
|
|
Port = namedtuple("Port", ["name", "port", "protocol", "description"])
|
|
|
|
__BASE_PATH__ = os.path.dirname(os.path.abspath(__file__))
|
|
__DATABASE_PATH__ = os.path.join(__BASE_PATH__, 'ports.json')
|
|
__DB__ = TinyDB(__DATABASE_PATH__, storage=CachingMiddleware(JSONStorage))
|
|
|
|
|
|
def get_ports(port, like=False):
|
|
"""
|
|
传入端口号即可
|
|
端口号为字符串类型
|
|
"""
|
|
where_field = "port" if port.isdigit() else "name"
|
|
if like:
|
|
ports = __DB__.search(where(where_field).search(port))
|
|
else:
|
|
ports = __DB__.search(where(where_field) == port)
|
|
try:
|
|
return ports[0] # flake8: noqa (F812)
|
|
except:
|
|
return []
|
|
#print get_ports('3306')
|
|
#print get_ports('222222')
|