hakk

software development, devops, and other drivel
Tree lined path

Python

Example of adding CORS headers Flask

An example of adding CORS headers to a Flask app without using Flask-CORS. from flask import Flask, request, jsonify, make_response from werkzeug.datastructures import Headers app = Flask(__name__) @app.route('/', methods=['OPTIONS', 'GET', 'POST']) def index(): # output all request headers to terminal print(request.headers) if request.method == 'POST': # handle json post data here... print(request.get_json()) headers = Headers() headers.add('Access-Control-Allow-Origin', '*') headers.add('Access-Control-Allow-Methods', 'OPTIONS', 'GET', 'POST') headers.add('Access-Control-Allow-Headers', 'Content-Type') headers.add('Access-Control-Max-Age', '300') resp = make_response( jsonify( {'msg':'nailed it! Read more...

Python date string to date object

If you have a date string and need to convert it into a date object, how can it be converted to a date object using Python? Python includes within the datetime package a strptime function, here’s an example usage: >>> import datetime >>> datetime.datetime.strptime('2022-10-30T14:32:41Z', "%Y-%m-%dT%H:%M:%S%z") datetime.datetime(2022, 10, 30, 14, 32, 41, tzinfo=datetime.timezone.utc) Now that it’s a date object and can be manipulated as needed. If the date string you have doesn’t match this format, check out the format codes table to find the necessary directives. Read more...