简体   繁体   中英

simple rpyc client and server for sending string data

I'm working on a program in python using rpyc. My goal is to create a simple server that accepts bytes of data (String) from a client. I'm new both to python and rpyc. Here is my server.py code:

from rpyc.utils.server import ThreadedServer # or ForkingServer

class MyService(rpyc.Service):
    # My service
    pass

if __name__ == "__main__":
    server = ThreadedServer(MyService, port = 18812)
    server.start()

Then there is my client.py code:

from rpyc.core.stream import SocketStream
from rpyc.core.channel import Channel

b = SocketStream.connect("localhost", 18812)
c = Channel(b, compress=True)
c.send("abc")

b.close()
c.close()

Yet when running my client.py there is an error in console. If I'm understanding it correctly, i must create a stream in server.py that is associated with the client. Is that the case? How can i achieve that?

You're using the low level primitives, but you didn't put a Protocol over those. Anyway, you really don't need to go there. Here's what you want to do:

myserver.py

import rpyc
from rpyc.utils.server import ThreadedServer

class MyService(rpyc.Service):
    # My service
    def exposed_echo(self, text):
        print(text)

if __name__ == "__main__":
    server = ThreadedServer(MyService, port = 18812)
    server.start()

then open a python shell and try

>>> import rpyc
>>> c = rpyc.connect("localhost", 18812)
>>> c.root.echo("hello")
'hello'

note that this is using the service-oriented mode. you can also use the classic mode. just run bin/rpyc_classic.py and then connect using rpyc.classic.connect("host")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM