Skip to content

Instantly share code, notes, and snippets.

@tdpreece
tdpreece / run_stub_server_example.py
Created November 5, 2016 12:24
Stub http server that can find a free port to run on
'''
Demo of how to start up a simple http server on a free port in a given range.
I find that this can be useful in tests.
>$ python run_stub_server_example.py
*** Server 1 ***
Server 1: is_running=True, port=60000
*** Server 2 ***
Port 60000 in use.
Server 2: is_running=True, port=60001
def log_method_call(func):
def inner(*args, **kwargs):
try:
frame = inspect.currentframe()
stack_trace = inspect.getouterframes(frame)
stack_trace_relevant_parts = [
" ".join((f[1], str(f[2]), f[4][0].strip()))
for f in stack_trace
]
pretty_stack_trace = "\n".join(stack_trace_relevant_parts)
@tdpreece
tdpreece / stomp_example.py
Created September 28, 2015 19:30
stomp.py example
# Python 2.7.6
# stomp.py v4.1.5 (https://pypi.python.org/pypi/stomp.py)
import time
import stomp
class MyListener(stomp.ConnectionListener):
def on_message(self, headers, message):
print('MyListener:\nreceived a message "{}"\n'.format(message))
@tdpreece
tdpreece / simple_http_server.sh
Created September 25, 2015 16:38
Running a Python SimpleHTTPServer in the background and killing it when doneSimpleHTTPServer
#!/usr/bin/env bash
# Create a page in the current dir
echo "My Test Page" > test.html
# Start server
python -m SimpleHTTPServer 8000 &> /dev/null &
pid=$!
# Give server time to start up