[英]simple rpyc client and server for sending string data
我正在使用rpyc在python中編寫程序。 我的目標是創建一個簡單的服務器,該服務器從客戶端接收字節數據(字符串)。 我對python和rpyc都是新手。 這是我的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()
然后是我的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()
但是,當運行我的client.py時,控制台出現錯誤。 如果我正確理解它,則必須在server.py中創建與客戶端關聯的流。 是這樣嗎 我該如何實現?
您正在使用低級原語,但沒有在這些原語上放置協議。 無論如何,您真的不需要去那里。 這是您想做的:
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()
然后打開一個python shell並嘗試
>>> import rpyc
>>> c = rpyc.connect("localhost", 18812)
>>> c.root.echo("hello")
'hello'
請注意,這是使用面向服務的模式。 您也可以使用經典模式。 只需運行bin/rpyc_classic.py
,然后使用rpyc.classic.connect("host")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.