Skip to content

Instantly share code, notes, and snippets.

@jramseygreen
jramseygreen / api.py
Last active March 26, 2024 15:21
Implement SSE for flask
from flask import Blueprint, jsonify
from sse import sse
# required line at the top of every blueprint file
api = Blueprint("api", __name__) # match variable name and first arg to file name
# register more blueprints here to further split up the api
# e.g.
# api.register_blueprint(blueprint, url_prefix='/users')
# would cascade through /api/users
@jramseygreen
jramseygreen / api.py
Last active November 8, 2023 16:04
flask + vue spa + cheroot webserver
from flask import Blueprint, jsonify
# required line at the top of every blueprint file
api = Blueprint("api", __name__) # match variable name and first arg to file name
# register more blueprints here to further split up the api
# e.g.
# api.register_blueprint(blueprint, url_prefix='/users')
# would cascade through /api/users
# api routes when hitting /api
@jramseygreen
jramseygreen / crypt.py
Last active May 15, 2022 16:34
websocket connection python <--> javascript with RSA encrypted tunnel
import base64
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
# pass in the public key of the client to communicate with
class Crypt:
def __init__(self):
# 1024 means the keysize will be 1024 bits
@jramseygreen
jramseygreen / Watcher.py
Created February 24, 2022 12:11
class to watch directory for changes
import sys
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class Watcher:
def __init__(self, path="", on_created=None, on_deleted=None, on_modified=None, on_moved=None):
self.event_handler = FileSystemEventHandler()
self.observer = Observer()
@jramseygreen
jramseygreen / crypto.py
Last active December 18, 2021 13:58
AES CBC python -> javascript, javascript -> python (w/ strings)
from Crypto import Random
from Crypto.Cipher import AES
import base64
from hashlib import md5
BLOCK_SIZE = 16
class CryptoWrapper:
@jramseygreen
jramseygreen / crypto.py
Last active December 7, 2021 15:50
python encryption / decryption in string form with AES
from base64 import b64encode, b64decode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
class CryptoWrapper:
def __init__(self, key):
self.key = (key * 2).encode()
@jramseygreen
jramseygreen / index.html
Last active December 18, 2021 13:57
Bare bones websocket server in python
<script>
window.onload = function() {
var connection = new WebSocket("ws://localhost:9876/");
connection.onopen = function () {
connection.send('hello world');
};
connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
};