Adding network/address to db

This commit is contained in:
Nico Schottelius 2019-05-12 02:11:31 +02:00
parent 5d408d4a14
commit b408e161c4
3 changed files with 111 additions and 8 deletions

View file

@ -8,6 +8,7 @@ flask = "*"
flask-jsonpify = "*"
flask-sqlalchemy = "*"
flask-restful = "*"
psycopg2 = "*"
[dev-packages]

29
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "7feeea3eb83feed49cc045bdd236bc50b3a190689a269134b97696eacf0b5b1b"
"sha256": "e480f6e4921aea429b583cddf2491e5c7b9dd7872be4663dddc917e3fdaf9249"
},
"pipfile-spec": 6,
"requires": {
@ -55,11 +55,11 @@
},
"flask-sqlalchemy": {
"hashes": [
"sha256:3bc0fac969dd8c0ace01b32060f0c729565293302f0c4269beed154b46bec50b",
"sha256:5971b9852b5888655f11db634e87725a9031e170f37c0ce7851cf83497f56e53"
"sha256:0c9609b0d72871c540a7945ea559c8fdf5455192d2db67219509aed680a3d45a",
"sha256:8631bbea987bc3eb0f72b1f691d47bd37ceb795e73b59ab48586d76d75a7c605"
],
"index": "pypi",
"version": "==2.3.2"
"version": "==2.4.0"
},
"itsdangerous": {
"hashes": [
@ -108,6 +108,23 @@
],
"version": "==1.1.1"
},
"psycopg2": {
"hashes": [
"sha256:00cfecb3f3db6eb76dcc763e71777da56d12b6d61db6a2c6ccbbb0bff5421f8f",
"sha256:076501fc24ae13b2609ba2303d88d4db79072562f0b8cc87ec1667dedff99dc1",
"sha256:4e2b34e4c0ddfeddf770d7df93e269700b080a4d2ec514fec668d71895f56782",
"sha256:5cacf21b6f813c239f100ef78a4132056f93a5940219ec25d2ef833cbeb05588",
"sha256:61f58e9ecb9e4dc7e30be56b562f8fc10ae3addcfcef51b588eed10a5a66100d",
"sha256:8954ff6e47247bdd134db602fcadfc21662835bd92ce0760f3842eacfeb6e0f3",
"sha256:b6e8c854cdc623028e558a409b06ea2f16d13438335941c7765d0a42b5bedd33",
"sha256:baca21c0f7344576346e260454d0007313ccca8c170684707a63946b27a56c8f",
"sha256:bb1735378770fb95dbe392d29e71405d45c8bdcfa064f916504833a92ab03c55",
"sha256:de3d3c46c1ee18f996db42d1eb44cf1565cc9e38fb1dbd9b773ff6b3fa8035d7",
"sha256:dee885602bb200bdcb1d30f6da6c7bb207360bc786d0a364fe1540dd14af0bab"
],
"index": "pypi",
"version": "==2.8.2"
},
"pytz": {
"hashes": [
"sha256:303879e36b721603cc54604edcac9d20401bdbe31e1e4fdee5b9f98d5d31dfda",
@ -124,9 +141,9 @@
},
"sqlalchemy": {
"hashes": [
"sha256:d5432832f91d200c3d8b473a266d59442d825f9ea744c467e68c5d9a9479fbce"
"sha256:91c54ca8345008fceaec987e10924bf07dcab36c442925357e5a467b36a38319"
],
"version": "==1.3.2"
"version": "==1.3.3"
},
"werkzeug": {
"hashes": [

View file

@ -1,16 +1,75 @@
#!/usr/bin/env python3
USERLENGTH = 50
import ipaddress
import random
import sys
from flask import Flask
from flask_restful import Resource, Api
from flask_restful import reqparse
# ORM support -- should be in database.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine('postgresql://localhost:5432/ungleichgame?host=/var/run/postgresql/')
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
Base.metadata.create_all(bind=engine)
class Network(Base):
__tablename__ = 'network'
username = Column(String(USERLENGTH), primary_key=True)
network = Column(postgresql.CIDR)
def __init__(self, username, network):
self.username = username
self.network = network
class Address(Base):
__tablename__ = 'address'
username = Column(String(USERLENGTH), primary_key=True)
address = Column(postgresql.INET)
def __init__(self, username, address):
self.username = username
self.address = address
app = Flask(__name__)
api = Api(app)
class Level(Resource):
points = 0
def test(self):
pass
class Ping6(Level):
points = 10
def test(self):
"""
ping6 -c3
"""
class Game(Resource):
def get(self):
return {'hello': 'world'}
return {'hello': 'list of levels'}
api.add_resource(Game, '/game')
@ -21,4 +80,30 @@ def high_score():
if __name__ == '__main__':
app.run(port='5002')
init_db()
#net = ipaddress.IPv6Network("2a0a:e5c1:101::/64")
# net_base = ipaddress.IPv6Network("2a0a:e5c1::/64")
# net_offset = random.randrange(2**80, 2**95)
net_base = "2a0a:e5c1:{}::/64"
net_offset = random.randrange(0, 9999)
net = ipaddress.IPv6Network(net_base.format(net_offset))
print(net)
n = Network('nico{}'.format(net_offset), str(net))
print(n)
addr_offset = random.randrange(0, 2**64)
addr = net[0] + addr_offset
a = Address('nico{}'.format(net_offset), str(addr))
print(a)
db_session.add(n)
db_session.add(a)
db_session.commit()
sys.exit(0)
app.run(port='5002')