An Object-oriented Binary RPC Framework
Project description
xrpc
An Object-oriented Binary RPC Framework
Library Usage
1. define service interface (.xidl)
// Greeter.xidl
class HelloReq {
Str name
}
class HelloReply {
Str msg
}
class Greeter {
HelloReply sayHello(HelloReq req)
sendFile(self, Stream)
}
2. generate code from xidl
xrpc-gen python Greeter.xidl
will got file Greeter.py
# Greeter.py
class Greeter 接口定义
class PrGreeter 代理
class SrGreeter 服务
3. 实现服务
- 继承并实现
SrGreeter
class GreeterService(SrGreeter):
def sayHello(req: HelloReq) -> HelloReply:
print('hello %s' % req.name)
return HelloReply('Hello, ', req.name)
def sendFile(file: Stream) -> None:
save_dir = '__temp/'
if not os.path.exists(save_dir):
os.mkdir(save_dir)
s = FileWriterStream(file, save_dir + file.name)
self.channel.registerStream(s) # will receive the stream
- 继承并实现默认服务
SrPeer的查找服务接口
class MyPeerService(SrPeer):
def getService(name: str) -> IInterface:
if name == Greeter.__name__:
return GreeterService(self.channel)
raise ValueError("service not found!")
4. 启动服务
config = Config()
peer = MyPeerService(config)
conn = WSConnection() # use websocket
async with conn.start(peer, 'greeter') as (ip, port):
# server started, show qrcode
qr = qrcode.QRCode()
qr.border = 1
qr.add_data("%s://%s:%d" % (conn.protocol, ip, port))
qr.print_ascii(invert=True)
await asyncio.Future() # wait forever
示例发布了一个名为
greeter的websocket服务, 该服务可在局域网内自动发现
5. 调用服务接口
config = Config()
conn = WSConnection() # use websocket
async with conn.connect('greeter', config) as peer:
# find service
obj = await peer.getService(Greeter.__name__)
greeter = PrGreeter(obj)
# normal rpc call
ret = await greeter.sayHello(HelloMsg('Daniel'))
print(ret.msg) # Hello, Daniel
# Send file
file = FileReaderStream('file.txt')
await greeter.sendFile(file) # call sendFile
await peer.channel.sendStream(file) # send file stream
查找并连接名为
greeter的websocket 服务
6. 加密握手协议
config中传入handshake即可配置握手协议. 协议实现HandShakeProtocal
config = Config(handshake=HandShakeV1())
握手和加密方式可自定义
7. 使用其他的连接协议
目前支持使用websocket(WSConnection)和tcp连接协议(TCPConnection), 如果想要自定义协议, 请实现Connection类
conn = MyCustomConnection() # use custom connection protocal
8. 局域网发现
默认打开局域网发现功能, 如果想要关闭, 则在启动服务时传入参数discoverable=False
async with conn.start(peer, name, discoverable=False) as (ip, port):
...
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file oorpc-0.1.1.tar.gz.
File metadata
- Download URL: oorpc-0.1.1.tar.gz
- Upload date:
- Size: 23.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.10 Darwin/23.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc6bc2bd4f15b8064e03514ab16becebef36db5d2e025a6adfb4d12a212d0b8d
|
|
| MD5 |
4d88657d8d97946fe279f7ab97a7ac18
|
|
| BLAKE2b-256 |
a0c4bd57e9e31ffbfb0c1fd2c67b47df85d47fc6ad21f1d0abb4cb033bd6bacd
|
File details
Details for the file oorpc-0.1.1-py3-none-any.whl.
File metadata
- Download URL: oorpc-0.1.1-py3-none-any.whl
- Upload date:
- Size: 30.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.10 Darwin/23.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79329ece5743835c2fba2de672111d8c564471480f4bb16ad1a360660e4bbd4b
|
|
| MD5 |
b832c963aea1f3aee876f0007a708a87
|
|
| BLAKE2b-256 |
30679d23fb9e1d5be734e655af9643e99985f4c31e4194fa4e7015f3ac0719f5
|