简体   繁体   English

简单的rpyc客户端和服务器,用于发送字符串数据

[英]simple rpyc client and server for sending string data

I'm working on a program in python using rpyc. 我正在使用rpyc在python中编写程序。 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. 我对python和rpyc都是新手。 Here is my server.py code: 这是我的server.py代码:

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: 然后是我的client.py代码:

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. 但是,当运行我的client.py时,控制台出现错误。 If I'm understanding it correctly, i must create a stream in server.py that is associated with the client. 如果我正确理解它,则必须在server.py中创建与客户端关联的流。 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 然后打开一个python shell并尝试

>>> 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") 只需运行bin/rpyc_classic.py ,然后使用rpyc.classic.connect("host")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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