Esempio n. 1
0
 def __init__(self, username, data, host, port, database):
     self.username = username
     self.host = host
     self.port = port
     self.database = database
     self.db = Database(self.host, self.port, database=database)
     self.data = data
Esempio n. 2
0
def main():
    directory = ''
    match = False
    inject = False
    db_name = 'certs.db'
    output = False
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hd:midb:-o',
                                   ['help', 'directory=', 'match', 'inject',
                                    'database=', '--output'])
    except:
        usage()
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            print('printing hep')
            usage()
        elif opt in ('-d', '--directory'):
            directory = arg
            if(not directory.endswith('/')):
                directory += '/'
        elif opt in ('-m', '--match'):
            match = True
        elif opt in ('-i', '--inject'):
            inject = True
        elif opt in ('-b', '--database'):
            db = arg
        elif opt in ('-o', '--output'):
            output = True
            outputfile = arg
            if(outputfile == ''):
                outputfile = 'matches.txt'
    if directory:
        print("Grabbing data...")
        grabber = Grabber(directory)
        classifier = Classifier(grabber)
        print("Classifying...")
        classifier.classify()
        certs, keys = classifier.get_data()
    db = Database(db_name)
    if inject:
        try:
            print("Creating the database")
            db.create_db()
        except:
            print("Database already exists.")
        print("Injecting data into the database...")
        db.insert_keys(keys)
        db.insert_certs(certs)
    if match:
        print("Matching data...")
        db.match_cert_key()
    if output:
        db.export_matches(outputfile)
Esempio n. 3
0
from src.db import Database
from src.query import Query
from time import process_time
from random import choice, randrange

# Student Id and 4 grades
db = Database()
grades_table = db.create_table('Grades', 0, 5)
query = Query(grades_table)
keys = []

# Measuring Insert Performance
insert_time_0 = process_time()
for i in range(0, 10000):
    query.insert(906659671 + i, 93, 0, 0, 0)
    keys.append(906659671 + i)
insert_time_1 = process_time()

print("Inserting 10k records took:  \t\t\t", insert_time_1 - insert_time_0)

# Measuring update Performance
update_cols = [
    [randrange(0, 100), None, None, None, None],
    [None, randrange(0, 100), None, None, None],
    [None, None, randrange(0, 100), None, None],
    [None, None, None, randrange(0, 100), None],
    [None, None, None, None, randrange(0, 100)],
]

update_time_0 = process_time()
for i in range(0, 10000):
Esempio n. 4
0
 def __init__(self, username):
     scope = 'user-library-read'
     self.en = Echonest()
     self.token = util.prompt_for_user_token(username, scope)
     self.db = Database()
Esempio n. 5
0
def run(host, port, db_path):

    server = socket(AF_INET, SOCK_STREAM)
    server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)

    # Database
    database = Database(db_path)
    # database.save()

    try:
        server.bind((host, int(port)))
        server.listen(5)

        # Greetings
        print_line(
            f'<nl><green>JSON Server</green><nl><cyan>http://{host}:{port}/</cyan><nl>'
        )

        while True:
            headers = {}
            client, address = server.accept()

            # todo: Work around timeout
            # client.settimeout(60)
            # except socket.timeout

            td = datetime.now().strftime('%H:%M:%S')
            rd = client.recv(PACKET_SIZE).decode()

            # while True:
            #     rd = client.recv(PACKET_SIZE)
            #     if not rd:
            #         break

            # Check received data
            if not rd:
                print_line('<yellow>EMPTY !</yellow><nl><nl>')
                client.close()
                continue

            pieces = rd.split(CRLF)
            method, url = pieces[0].split()[0:2]
            body = None
            status_code = 200

            # Favicon - just fun
            if url == '/favicon.ico':
                with open('favicon.ico', 'rb') as fp:
                    output = get_response(
                        status_code,
                        fp.readlines()[0],
                        {'Content-type': 'image/vnd.microsoft.icon'})
                    client.sendall(output)
                    client.close()

                # print_line('<green>/favicon.ico</green>')
                continue

            print_line(
                f'<nl><cyan>************ {td} **************</cyan><nl>{rd}')

            if method not in METHODS:
                status_code = 501
            elif method not in ALLOWED_METHODS:
                status_code = 405
                headers['Allow'] = ', '.join(ALLOWED_METHODS)
            else:
                parts = url.strip('/').split('/')
                pk = None
                if len(parts) > 1:
                    pk = parts[-1]
                    alias = '/'.join(parts[0:-1])
                else:
                    alias = parts[0]

                # Search by route_name and primary_key
                body = database.select(alias, pk)

                if method in ('POST', 'PUT'):
                    print_line(
                        f'<nl><yellow>{method} not implemented</yellow>')

                if not body:
                    status_code = 404

            output = get_response(status_code, body, headers)

            client.sendall(output)
            client.shutdown(SHUT_RDWR)
            client.close()

    except KeyboardInterrupt:
        print_line('<nl><gray>Shutting down...</gray><nl>')

    finally:
        server.close()
Esempio n. 6
0
def setup_db():
    global db
    db = Database(USER, PASSWORD, DB, HOST, PORT)
Esempio n. 7
0
 def __init__(self):
     self.db = Database()
Esempio n. 8
0
 def __init__(self):
     self.db = Database()
     self.labels = LabelModel()
     self.notes = NoteModel()