Skip to content

Instantly share code, notes, and snippets.

@sbeam
Created July 30, 2019 11:58
Show Gist options
  • Save sbeam/272d424313bcd324677127de69d21fe2 to your computer and use it in GitHub Desktop.
Save sbeam/272d424313bcd324677127de69d21fe2 to your computer and use it in GitHub Desktop.
python chat
#!/usr/bin/env python
import datetime
import flask
import redis
import os
app = flask.Flask(__name__)
app.secret_key = 'asdf'
red = redis.Redis(host=os.environ['REDIS_HOST'], port=6379, db=0)
def event_stream():
pubsub = red.pubsub()
pubsub.subscribe('chat')
# TODO: handle client disconnection.
for message in pubsub.listen():
print message
yield 'data: %s\n\n' % message['data']
@app.route('/health', methods=['GET', 'POST'])
def health():
print '/health'
return 'ok'
@app.route('/login', methods=['GET', 'POST'])
def login():
if flask.request.method == 'POST':
flask.session['user'] = flask.request.form['user']
return flask.redirect('/')
return '<form action="" method="post">user: <input name="user">'
@app.route('/post', methods=['POST'])
def post():
message = flask.request.form['message']
user = flask.session.get('user', 'anonymous')
now = datetime.datetime.now().replace(microsecond=0).time()
red.publish('chat', u'[%s] %s: %s' % (now.isoformat(), user, message))
return flask.Response(status=204)
@app.route('/stream')
def stream():
return flask.Response(event_stream(),
mimetype="text/event-stream")
@app.route('/')
def home():
if 'user' not in flask.session:
return flask.redirect('/login')
return """
<!doctype html>
<title>chat</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style>body { max-width: 500px; margin: auto; padding: 1em; background: black; color: #fff; font: 16px/1.6 menlo, monospace; }</style>
<p><b>hi, %s!</b></p>
<p>Message: <input id="in" /></p>
<pre id="out"></pre>
<script>
function sse() {
var source = new EventSource('/stream');
var out = document.getElementById('out');
source.onmessage = function(e) {
// XSS in chat is fun
out.innerHTML = e.data + '\\n' + out.innerHTML;
};
}
$('#in').keyup(function(e){
if (e.keyCode == 13) {
$.post('/post', {'message': $(this).val()});
$(this).val('');
}
});
sse();
</script>
""" % flask.session['user']
if __name__ == '__main__':
app.debug = True
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment