+ the game
This commit is contained in:
parent
add7388ddc
commit
115313e5f4
1 changed files with 87 additions and 4 deletions
91
server.py
91
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 = Flask(__name__)
|
||||||
|
|
||||||
@app.route('/')
|
# where player are stored
|
||||||
def hello_world():
|
dir= "./players"
|
||||||
return 'Hello, World! - post html page!'
|
|
||||||
|
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>
|
||||||
|
|
||||||
|
</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)
|
||||||
|
|
Loading…
Reference in a new issue