diff --git a/server.py b/server.py index dd22107..4354886 100644 --- a/server.py +++ b/server.py @@ -1,6 +1,89 @@ -from flask import Flask +from flask import Flask, request, redirect, url_for +import os +import sys + app = Flask(__name__) -@app.route('/') -def hello_world(): - return 'Hello, World! - post html page!' +# where player are stored +dir= "./players" + +REGULAR_PAGE = """ + + +Who plays IPv6 games? + + +

Who plays IPv6 games?

+So who of us is able to use IPv6 and is interested in playing +IPv6 games? + +If you want to register, simply submit your name below. +Obviously, your name and IPv6 address will be publicly recorded. + +

Register

+
+Your name: + +
+ + +

Interested in playing IPv6 games are...

+ + + + +""" + + +def addresses(): + address_list = [] + + if os.path.isdir(dir): + address_list = os.listdir(dir) + + return address_list + +def get_player_name(address): + fname = os.path.join(dir, address) + with open(fname, "r") as fd: + name = fd.readlines() + + print(name) + return name[0].strip() + +def save_player(name, address): + fname = os.path.join(dir, address) + + with open(fname, "w+") as fd: + fd.write("{}\n".format(name)) + + +@app.route('/', methods=['GET', 'POST' ]) +def main(): + if request.method == 'GET': + html = [] + + for address in addresses(): + name = get_player_name(address) + html.append("
  • {}@{}".format(name,address)) + + print(html) + return REGULAR_PAGE.format("\n".join(html)) + + else: + name = request.form['name'] + address = request.remote_addr + save_player(name, address) + return redirect(url_for('main')) + + # process post data + + + +if __name__ == '__main__': + if not os.path.isdir(dir): + print("Please create player directory {} before starting".format(dir)) + sys.exit(1) + app.run(host="::", debug=True)