Sat Dec 30 01:10:44 AM CET 2023

This commit is contained in:
Adolfo Delorenzo 2023-12-30 01:10:44 +01:00
parent 5de46c126c
commit ea7834d719
4 changed files with 37 additions and 0 deletions

2
gen_ports.py Normal file → Executable file
View File

@ -1,3 +1,5 @@
#!/usr/bin/python3
import sqlite3
import random
import uuid

Binary file not shown.

35
ports.py Normal file
View File

@ -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()

BIN
random_numbers.db Normal file

Binary file not shown.