Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
LarsBergqvist / requires_authorization.py
Created September 3, 2016 17:25
Authorization decorator with Python Flask
def ok_user_and_password(username, password):
return username == app.config['USERNAME'] and password == app.config['PASSWORD']
def authenticate():
message = {'message': "Authenticate."}
resp = jsonify(message)
resp.status_code = 401
resp.headers['WWW-Authenticate'] = 'Basic realm="Main"'
@LarsBergqvist
LarsBergqvist / flask_with_authorization.py
Created September 3, 2016 19:45
Flask routes with authorization
@app.route('/api/secret')
@requires_authorization
def api_secret():
return jsonify({'message':get_secret_message()})
@app.route('/')
@requires_authorization
def index():
return render_template('index.html', message=get_secret_message())
@LarsBergqvist
LarsBergqvist / generate_key_and_cert.sh
Created September 3, 2016 19:49
Generate a key and a certificate
openssl genrsa 1024 > ssl.key
openssl req -new -x509 -nodes -sha1 -days 365 -key ssl.key > ssl.cert
@LarsBergqvist
LarsBergqvist / flask_with_ssl.py
Created September 3, 2016 19:50
Starting flask with SSL
context = ('ssl.cert', 'ssl.key')
app.run(host='0.0.0.0', port=80, ssl_context=context)
@LarsBergqvist
LarsBergqvist / flask_getopt_example.py
Created September 3, 2016 19:55
Example on parsing the arguments to the start script for Python Flask
import sys, getopt
from basic_auth import app
def main(argv):
user = ''
password = ''
try:
opts, args = getopt.getopt(argv,"u:p:")
except getopt.GetoptError:
@LarsBergqvist
LarsBergqvist / SRController.js
Last active September 7, 2016 07:20
An AngularJS controller that makes requests to api.sr.se
var myApp = angular.module('myApp', []);
myApp.controller('SRChannelsController', ['$scope', '$http', function($scope, $http) {
$scope.header = 'Radio channels on Sveriges Radio';
$scope.filter = "Riks";
$scope.filterChanged = function() {
getChannelsInfo($scope.filter);
};
@LarsBergqvist
LarsBergqvist / index.html
Created September 7, 2016 07:20
View for the SRChannelsController
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script data-require="angular.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
<script src="SRController.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
@LarsBergqvist
LarsBergqvist / neopixel_micropython_with_stop_button.py
Last active January 16, 2017 06:44
While loop with stop button criteria
#
# A script for an Adafruit Huzzah (ESP8266) with a NeoPixel ring attached
# With a button connected to a GPIO pin so that we can exit the infinite while loop
# (needed if the script is used as main.py)
#
from machine import Pin
from neopixel import NeoPixel
import time
OUTPUTPIN = 14 # use GPIO14 as output to NeoPixel ring
@LarsBergqvist
LarsBergqvist / ArduinoAnalogInput.pde
Last active October 7, 2016 07:34
Connecting an Arduino to a Processing sketch
int maxBarHeight = 600;
int barWidth = 400;
//IArduinoIntegration arduino = new ArduinoFakeIntegration();
IArduinoIntegration arduino = new ArduinoIntegration(this);
// A list with bars to draw
// Specify the matching Arduino analog pin number and a color for the bar that should represent the input voltage
BarSpec[] barSpecs = { new BarSpec(0, color(150,150,20)),
new BarSpec(1, color(20,100,20)),
@LarsBergqvist
LarsBergqvist / ArduinoIntegration.pde
Last active October 6, 2016 10:43
Wrapper class for using the Arduino Firmata library in Processing
import processing.core.PApplet;
import processing.serial.*;
import cc.arduino.*;
public class ArduinoIntegration implements IArduinoIntegration {
// Change this so that it matches the usb device connected to the Arduino
String arduinoUSBDeviceName = "/dev/tty.usbmodem1421";
Arduino arduino;