who-plays-ipv6-games/server.py
2019-12-16 21:41:13 +01:00

94 lines
2 KiB
Python

from flask import Flask, request, redirect, url_for
import os
import sys
app = Flask(__name__)
# where player are stored
dir= "./players"
REGULAR_PAGE = """
<html>
<head>
<title>Who plays IPv6 games?</title>
</head>
<body>
<h1>Who plays IPv6 games?</h1>
So who of us is able to use IPv6 <b>and</b> 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.
<h2>Register</h2>
<form action="/" method=post>
Your name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
<h2>Interested in playing IPv6 games are... </h2>
<ul>
{}
</ul>
<h2>Open Source</h2>
As usual this project is open source and
you can find the source code on
<a href="https://code.ungleich.ch/nico/who-plays-ipv6-games">code.ungleich.ch</a>.
</body>
</html>
"""
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("<li> {}@{}".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)