diff --git a/gen_ports.py b/gen_ports.py old mode 100644 new mode 100755 index 056453c..f579070 --- a/gen_ports.py +++ b/gen_ports.py @@ -1,3 +1,5 @@ +#!/usr/bin/python3 + import sqlite3 import random import uuid diff --git a/numbers_db.sqlite b/numbers_db.sqlite index 6b9f7f9..5517958 100644 Binary files a/numbers_db.sqlite and b/numbers_db.sqlite differ diff --git a/ports.py b/ports.py new file mode 100644 index 0000000..1326d3d --- /dev/null +++ b/ports.py @@ -0,0 +1,35 @@ +import sqlite3 +import random +from time import sleep + +# Create a SQLite database and table if not exists +conn = sqlite3.connect('random_numbers.db') +c = conn.cursor() + +try: + c.execute("""CREATE TABLE IF NOT EXISTS random_numbers(id INTEGER PRIMARY KEY, number INT)""") + conn.commit() +except: + pass + +def generate_and_update(): + random_number = random.randint(32768, 65535) + c.execute("SELECT COUNT(*) FROM random_numbers WHERE number=?", (random_number,)) + count = c.fetchone()[0] + + if count == 0: + c.execute("INSERT INTO random_numbers(number) VALUES(?)", (random_number,)) + conn.commit() + print(f"{random_number}") + else: + raise RuntimeError("No unique number found.") + +def main(): + try: + generate_and_update() + except RuntimeError as e: + print(e) + print("Try running the script again to get a new unique number.") + +if __name__ == "__main__": + main() diff --git a/random_numbers.db b/random_numbers.db new file mode 100644 index 0000000..c9ac354 Binary files /dev/null and b/random_numbers.db differ