Skip to content

Instantly share code, notes, and snippets.

@vsecoder-old-account
Created October 26, 2020 16:41
Show Gist options
  • Save vsecoder-old-account/abb51975ac4c307b312d805418b1cd04 to your computer and use it in GitHub Desktop.
Save vsecoder-old-account/abb51975ac4c307b312d805418b1cd04 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## Import Python Libs
## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
from flask import Flask, render_template, request, redirect
import os, requests, json
from flask import request, jsonify
app = Flask(__name__)
app.config['SECRET_KEY'] = 'vsevolodhtml.ru'
@app.route("/", methods=['GET', 'POST'])
def index():
try:
f = "Ку, создай чат введя <адрес>/<код комнаты>"
except:
print('Error')
return render_template('f.html', f=f)
@app.route("/<chatid>", methods=['GET', 'POST'])
def idchat(chatid):
try:
f = open(chatid + '.txt', 'x')
f.close()
except:
print('Error')
return render_template('f.html', f=chatid)
@app.route("/<chatid>/write/<user>/<msg>", methods=['GET', 'POST'])
def idchat1(chatid, user, msg):
try:
f = open(chatid + '.txt', 'a')
f.write(str(user) + ";" + str(msg) + "\n")
f.close()
except:
print('Error')
return render_template('f.html')
@app.route("/<chatid>/read", methods=['GET', 'POST'])
def idchat2(chatid):
try:
if flask.request.method == 'POST':
f = open(chatid + '.txt', 'r')
chattext = f.read()
f.close()
render_template('f.html', f=chattext)
else:
f = open(chatid + '.txt', 'r')
chattext = f.read()
f.close()
jsonify(chattext)
except:
print('Error')
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment