You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.0 KiB
39 lines
1.0 KiB
#!flask/bin/python
|
|
import sqlite3 as sql
|
|
from flask import Flask,abort,request,jsonify
|
|
|
|
START_PORT = 8127
|
|
|
|
|
|
try:
|
|
with sql.connect("database.db") as con:
|
|
cur = con.cursor()
|
|
cur.execute('CREATE TABLE IF NOT EXISTS games (id INTEGER PRIMARY KEY, ip TEXT, running INTEGER, port INTEGER, player_count INTEGER)')
|
|
con.commit()
|
|
except:
|
|
con.rollback()
|
|
finally:
|
|
con.close()
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route('/create',methods = ['POST'])
|
|
def create():
|
|
try:
|
|
with sql.connect("database.db") as con:
|
|
port = START_PORT
|
|
cur = con.cursor()
|
|
rows = True
|
|
while rows:
|
|
port += 1
|
|
cur.execute("SELECT * FROM games WHERE port = ?", (port,))
|
|
rows = cur.fetchall()
|
|
cur.execute("INSERT INTO games (ip,port) VALUES (?,?)", (request.remote_addr, port))
|
|
except:
|
|
con.rollback()
|
|
finally:
|
|
con.close()
|
|
return jsonify({'ip' : request.remote_addr, 'port' : port})
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|