Last active
August 29, 2015 14:16
-
-
Save righ/f1800f4d223aed72fb56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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