Skip to content

Instantly share code, notes, and snippets.

@devinrader
Last active August 15, 2019 03:14
Show Gist options
  • Save devinrader/d50234733b8e059b98b8 to your computer and use it in GitHub Desktop.
Save devinrader/d50234733b8e059b98b8 to your computer and use it in GitHub Desktop.
Receiving and Setting Cookies using Flask
from flask import Flask, request, make_response
from datetime import datetime, timedelta
from twilio import twiml
app = Flask(__name__)
@app.route("/sms")
def sms():
#get the cookie value, or default to zero
messagecount = int(request.cookies.get('messagecount',0))
messagecount += 1
twml = twiml.Response()
twml.sms("You've sent " + str(messagecount) + " messages in this conversation so far")
resp = make_response(str(twml))
expires=datetime.utcnow() + timedelta(hours=4)
resp.set_cookie('messagecount',value=str(messagecount),expires=expires.strftime('%a, %d %b %Y %H:%M:%S GMT'))
return resp
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