Skip to content

Instantly share code, notes, and snippets.

@righ righ/only.py
Last active Aug 29, 2015

Embed
What would you like to do?
#!/usr/bin/env python
# coding: utf-8
import socket
import functools
def nop():
"""no operation"""
class only(object):
def __init__(self, socket_name='', on_error=nop):
self.socket_name = socket_name
self.error = on_error
def __call__(self, f):
@functools.wraps(f)
def _wrap(*args, **kwargs):
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
s.bind(self.socket_name)
result = f(*args, **kwargs)
except socket.error:
result = self.error()
finally:
s.close()
return result
return _wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.