Skip to content

Commit

Permalink
Support unix domain socket
Browse files Browse the repository at this point in the history
  • Loading branch information
gongzhang committed Aug 20, 2019
1 parent f93d3f8 commit f34e988
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# procbridge-python

ProcBridge is a super-lightweight IPC (Inter-Process Communication) protocol over TCP socket. It enables you to **send and recieve JSON** between processes easily. ProcBridge is much like a simplified version of HTTP protocol, but only transfer JSON values.
ProcBridge is a super-lightweight IPC (Inter-Process Communication) protocol over TCP socket or Unix domain socket. It enables you to **send and recieve JSON** between processes easily. ProcBridge is much like a simplified version of HTTP protocol, but only transfer JSON values.

Please note that this repo is the **Python implementation** of ProcBridge protocol. You can find detailed introduction of ProcBridge protocol in the main repository: [gongzhang/procbridge](https://github.com/gongzhang/procbridge).

# Installation

```
pip install procbridge==1.1.3
pip install procbridge==1.2.0
```

# Example
Expand All @@ -17,14 +17,14 @@ Server Side:
```python
import procbridge as pb

def delegate(method, payload):
def delegate(method, args):

# define remote methods:
if method == 'echo':
return payload
return args

elif method == 'sum':
return sum(x for x in payload)
return sum(x for x in args)

elif method == 'err':
raise RuntimeError("an server error")
Expand All @@ -44,7 +44,7 @@ import procbridge as pb
client = pb.Client('127.0.0.1', 8000)

# call remote methods:
print(client.request("echo", 123))
print(client.request("echo", ['a', 'b', 'c']))
print(client.request("sum", [1, 2, 3, 4]))
client.request("echo", 123) # 123
client.request("echo", ['a', 'b', 'c']) # ['a', 'b', 'c']
client.request("sum", [1, 2, 3, 4]) # 10
```
45 changes: 40 additions & 5 deletions procbridge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,29 @@
class Client:

def __init__(self, host: str, port: int):
self.family = socket.AF_INET
self.host = host
self.port = port
self.path = None

@classmethod
def from_unix(cls, path: str):
ins = cls('', 0)
ins.family = socket.AF_UNIX
ins.path = path
return ins

@classmethod
def from_inet(cls, host: str, port: int):
return cls(host, port)

def request(self, method: str, payload: Any = None) -> Any:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.host, self.port))
if self.family == socket.AF_UNIX:
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(self.path)
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.host, self.port))
try:
p.write_request(s, method, payload)
code, result = p.read_response(s)
Expand All @@ -31,22 +48,40 @@ def request(self, method: str, payload: Any = None) -> Any:
class Server:

def __init__(self, host: str, port: int, delegate: Callable[[str, Any], Any]):
self.family = socket.AF_INET
self.host = host
self.port = port
self.path = None
self.started = False
self.lock = threading.Lock()
self.socket = None
self.delegate = delegate

@classmethod
def from_unix(cls, path: str, delegate: Callable[[str, Any], Any]):
ins = cls('', 0, delegate)
ins.family = socket.AF_UNIX
ins.path = path
return ins

@classmethod
def from_inet(cls, host: str, port: int, delegate: Callable[[str, Any], Any]):
return cls(host, port, delegate)

def start(self, daemon=True):
self.lock.acquire()
try:
if self.started:
return

self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((self.host, self.port))
if self.family == socket.AF_UNIX:
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.path)
else:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((self.host, self.port))
self.socket.listen(0)
t = threading.Thread(target=_start_server_listener, args=(self,), daemon=daemon)
t.start()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="procbridge",
version="1.1.3",
version="1.2.0",
author="Gong Zhang",
author_email="[email protected]",
description="A super-lightweight IPC (Inter-Process Communication) protocol over TCP socket.",
Expand Down

0 comments on commit f34e988

Please sign in to comment.