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...

ModuleNotFoundError: No module named 'distutils.util' on Ubuntu

Not on Ubuntu? If you’re using Ubuntu 16.04 or newer and want to use Python 3.7 and newer with pip you have probably seen this error message ModuleNotFoundError: No module named 'distutils.util'. On later versions of Ubuntu the error message will be: ModuleNotFoundError: No module named 'distutils.cmd'. This fix will still work. bmcculley@hakk:~$ pip3 Traceback (most recent call last): File "/usr/local/bin/pip3", line 5, in <module> from pip._internal.cli.main import main File "/usr/local/lib/python3. Read more...

How to Install Python 3.8 on Ubuntu 16.04 and 18.04

Page Navigation Building Python Install with Apt Update the default Python Troubleshooting The Zen of Python Python is one of the most popular and most widely used programming languages in the world at this time. The philosophy behind the Python programming language is most likely the reason. It’s very easy to learn with its simple syntax, that same syntax makes the language very readable, therefore easy to understand what someone else wrote. Read more...