Skip to main content

oorpc

An Object-oriented Binary RPC Framework

Library Usage

0. Example

Please refer example

  1. start server
poetry install
poetry run python example/server.py
  1. run client to test
poetry run python example/client.py

接下来是详细步骤.

1. Define service interface (.xidl)

// greeter.xidl
class NameCard {
    Str name
    Num age
    Str intro
}

class Greeter {
    NameCard exchangeNameCard(NameCard card)
    void sendFile(self, Stream file)
}

2. Generate code from xidl

xrpc-gen python greeter.xidl

will got file greeter.py

# greeter.py
    class Greeter 接口定义
    class PrGreeter 代理
    class SrGreeter 服务

3. 实现服务

  1. 继承并实现SrGreeter
class GreeterService(SrGreeter):
    async def exchangeNameCard(self, card: NameCard) -> NameCard:
        print('Got name card:')
        print('\tname:', card.name)
        print('\tage:', card.age)
        print('\tintro:', card.intro)
        return NameCard('GG', 100, 'I am the Greeter Service')
    
    async def sendFile(self, file: xfmt.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)
  1. 继承并实现默认服务PeerService的查找服务接口
class MyPeerService(PeerService):
    async def getService(self, name: str) -> IInterface:
        if name == Greeter.__name__:
            return GreeterService(self.channel)
        raise ValueError("service not found!")

4. 启动服务

async def start_server():
    config = Config()
    peer = MyPeerService(config)
    conn = WSConnection() # use websocket
    async with conn.start(peer, 'greeter') as (ip, port):
        print("Server: %s://%s:%d" % (conn.protocol, ip, port))
        await asyncio.Future() # wait forever
asyncio.run(start_server())

示例发布了一个名为greeter的websocket服务, 该服务可在局域网内自动发现

5. 调用服务接口

async def test_client():     
    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
        card = await greeter.exchangeNameCard(NameCard('Client', 50, 'I am the test client!'))
        print('Got reply:')
        print('\tname:', card.name)
        print('\tage:', card.age)
        print('\tintro:', card.intro)
        
        # Send file
        file = 'example/client.py'
        print("Ready to send file", file)
        file = FileReaderStream(file)
        await greeter.sendFile(file) # call sendFile
        await peer.channel.sendStream(file) # send file stream

asyncio.run(test_client())

查找并连接名为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):
    ...

Metadata

Release files for oorpc 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for oorpc 0.1.4
File Size Uploaded Provenance Actions
oorpc-0.1.4.tar.gz 24.1 kB Without provenance Details for oorpc-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for oorpc 0.1.4
File Interpreter ABI Platform Actions
oorpc-0.1.4-py3-none-any.whl Python 3 none any Details for oorpc-0.1.4-py3-none-any.whl

Total release size: 54.3 kB

Release files / oorpc-0.1.4.tar.gz

Download URL oorpc-0.1.4.tar.gz
Size 24.1 kB
Tags Source
SHA-256 checksum
How to use checksums
757a2f2961a7afe0cdecf9cc9c76c9dec9a816abd3b30f395da2ac422d5b63c7
BLAKE2b-256 checksum
How to use checksums
2ab6d3411e670b6c74b7b3f01afcaeb026222251f60cc8861b4ac4e5914f5fbd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.10 Darwin/23.1.0

Release files / oorpc-0.1.4-py3-none-any.whl

Download URL oorpc-0.1.4-py3-none-any.whl
Size 30.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3b7a5df28851f93539a9addf0560062dc5cc99695d7d03325f65f9cbd878d1b
BLAKE2b-256 checksum
How to use checksums
424e56fe6965bd391f61c8bf35b086d15809b53d3b77f453a728cfb3f5d48ae4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.10.10 Darwin/23.1.0

Security for oorpc 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page