Skip to content

Instantly share code, notes, and snippets.

@claws
claws / create_server.py
Created May 4, 2016 12:12
Demonstrates a minor difference between the asyncio and uvloop create_server handling of the port argument.
#!/usr/bin/env python3.5
'''
This script can be used to demonstrate a minor difference
between acceptable port argument passed to the `create_server`
method on the `asyncio` and `uvloop` event loop.
When the script is run without any args it uses `asyncio` to
create a server socket using some candidate `port` tokens that
instruct the event loop to create a server on an ephemeral port.
@claws
claws / README.rst
Last active July 16, 2022 13:30
Python asyncio SO_REUSEPORT OSX issue

Python asyncio SO_REUSEPORT OSX issue

Overview

My simple service discovery mechanism is built upon Python 3.4 and the asyncio module. It uses a UDP socket bound to a specified discovery port to allow applications to discover network services. System applications running on the same node bind to the same discovery port in order to receive discovery messages broadcast onto the network.

It is not working on OSX. The current asyncio implementation does not provide a convenient developer access point for setting the SO_REUSEPORT socket option prior to the call to bind.

@claws
claws / zmq.sls
Created July 12, 2014 12:10
This gist shows a method for using Saltstack to automate the build and install of libsodium, libzmq, czmq and pyzmq. This state can be used to keep the ZMQ stack up to date.
#
# Author: Chris Laws
#
# This saltstack state automates the build and install of the
# current version of the Github master branch for libsodium,
# libzmq, czmq and pyzmq (for both python2 and python3).
#
# This state is part of a suite of states I use to recreate my
# development machine and keep it up to date. It contains some
# configuration instructions that slightly beyond the common
@claws
claws / mqtt-ws-proxy
Created February 3, 2014 23:39
Simple WebSockets Proxy for a MQTT broker
# This gist demonstrates a simple method to create a WebSockets proxy for a MQTT broker.
# I use this method to provide a WebSockets interface for the Mosquitto MQTT broker.
# This lets my browser based MQTT applications to access my MQTT broker.
# I consider this approach simpler than the common alternative which is to run lighttpd
# with the mod_websocket addon which can be complex to setup.
#
# Dependencies are Python, Twisted and Autobahn.
#
# This example sets up a WebSockets server listening on localhost:9000. Messages received from
# WebSocket clients are forwarded to the MQTT broker using the endpointforward plugin provided
@claws
claws / authentication.py
Last active July 24, 2021 19:37
An authentication module for pyzmq based on zauth from czmq. See the included test file for usage. To run the test function use: python test_authentication.py
'''
An authentication module for pyzmq modelled on zauth from czmq.
The functions to read and generate certificates should be interoperable
with czmq's zcert's - though are not as fully featured.
'''
import datetime
import glob
import json
@claws
claws / czmq-stream-client.c
Created October 30, 2013 12:07
ZMQ can communicate with non-ZMQ TCP peers using the ZMQ_STREAM socket type. This is a silly example that uses the CZMQ library to create a non-ZMTP TCP server and a non-ZMTP TCP client. It was created to investigate how one might make use of the ZMQ_STREAM socket type.
//
// A minimal TCP client using the ZMQ API. This demonstrates a
// useful mechanism for bridging a ZMQ based system out to other
// TCP based systems.
//
#include "czmq.h"
@claws
claws / zsockmon.c
Last active November 24, 2016 12:35
A candidate CZMQ implementation for a ZMQ socket event monitor.
// Monitor socket transport events (tcp and ipc only)
#include "../include/czmq.h"
// remove this when zsockmon.h is properly moved into ../include
// and added to ../include/czmq.h (via adding to model/project.xml)
#include "zsockmon.h"
@claws
claws / threads-example-fixed.c
Last active December 22, 2015 01:18
These simple code examples were used as a code reference for a question to the zeromq mailing list about "Context was terminated" printed to stdout. The -fixed versions appear to resolve the issue.
// This simple code example was used as a code reference for a
// question to the zeromq mailing list.
//
// gcc -o threads-example.o -c threads-example.c -Wall -Wimplicit -Wextra -I/usr/local/include
// gcc -o threads-example threads-example.o -L/usr/local/lib -lzmq -lczmq
//
#include "czmq.h"
@claws
claws / bomau_publisher.py
Last active April 22, 2016 18:13
Publish Australian weather observations to MQTT
#!/usr/bin/env python
'''
Chris Laws 2013
Publish Australian weather observations over MQTT.
This script periodically retrieves weather observations from the
Australia Bureau of Meteorology. The source data comes from a file
@claws
claws / resolved-writer-modified.c
Created March 2, 2013 01:50
Avro C resolving issue
// This example shows how to write data into an Avro container file, and how to
// read data from a file, both with and without schema resolution. The source
// of this example can be found [on GitHub][gh].
//
// [gh]: https://github.com/dcreager/avro-examples/tree/master/resolved-writer
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>