port generator

This commit is contained in:
Adolfo Delorenzo 2023-12-26 11:48:13 -06:00
parent 8d7cd75bed
commit 34901c2a31
2 changed files with 39 additions and 0 deletions

39
gen_ports.py Normal file
View File

@ -0,0 +1,39 @@
import sqlite3
import random
import uuid
# Create the SQLite database if it doesn't exist
conn = sqlite3.connect("numbers_db.sqlite")
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS numbers (id text, number integer)''')
def generate_unique_random():
# Generate a random number between 32768 and 65535
rand_num = random.randint(32768, 65535)
# Generate a unique ID using UUID
new_id = str(uuid.uuid4())
# Check if the number is already in the database
c.execute("SELECT * FROM numbers WHERE number = ?", (rand_num,))
results = c.fetchall()
# If the number doesn't exist in the database, add it to the table
if not results:
c.execute("INSERT INTO numbers VALUES (?, ?)", (new_id, rand_num))
conn.commit()
else:
# If the number already exists, keep generating new random numbers until a unique one is found
while True:
rand_num = random.randint(32768, 65535)
c.execute("SELECT * FROM numbers WHERE number = ?", (rand_num,))
results = c.fetchall()
if not results:
new_id = str(uuid.uuid4())
c.execute("INSERT INTO numbers VALUES (?, ?)", (new_id, rand_num))
conn.commit()
break
return rand_num
if __name__ == "__main__":
print("Unique random number: " + str(generate_unique_random()))

BIN
numbers_db.sqlite Normal file

Binary file not shown.