Last active
August 15, 2019 03:14
-
-
Save devinrader/d50234733b8e059b98b8 to your computer and use it in GitHub Desktop.
Receiving and Setting Cookies using Flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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