Skip to content

Instantly share code, notes, and snippets.

@righ
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save righ/f1800f4d223aed72fb56 to your computer and use it in GitHub Desktop.
Save righ/f1800f4d223aed72fb56 to your computer and use it in GitHub Desktop.
#!/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