首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pyro4 [Errno -2]

Pyro4 [Errno -2]
EN

Stack Overflow用户
提问于 2020-07-17 10:21:25
回答 2查看 322关注 0票数 0

我正在试用我的个人电脑和树莓Pi 4之间的pyro4连接。

我的电脑上的代码是:

代码语言:javascript
复制
# saved as server.py
import Pyro4, Pyro4.naming
import socket, threading

# Define an object that will be accessible over the network.
# This is where all your code should go...
@Pyro4.expose
class MessageServer(object):
    def show_message(self, msg):
        print("Message received: {}".format(msg))


# Start a Pyro nameserver and daemon (server process) that are accessible
# over the network. This has security risks; see
# https://pyro4.readthedocs.io/en/stable/security.html
hostname = socket.gethostname()
ns_thread = threading.Thread(
    target=Pyro4.naming.startNSloop, kwargs={'host': hostname}
)
ns_thread.daemon = True   # automatically exit when main program finishes
ns_thread.start()
main_daemon = Pyro4.Daemon(host=hostname)

# find the name server
ns = Pyro4.locateNS()
# register the message server as a Pyro object
main_daemon_uri = main_daemon.register(MessageServer)
# register a name for the object in the name server
ns.register("example.message", main_daemon_uri)

# start the event loop of the main_daemon to wait for calls
print("Message server ready.")
main_daemon.requestLoop()

我的覆盆子的密码是:

代码语言:javascript
复制
import Pyro4
import sys

print("Message:")
msg=sys.stdin.readline().strip()

message_server = Pyro4.Proxy("PYRONAME:192.168.1.5")
message_server.show_message(msg)

我的PC上的代码没有显示任何错误,但是当我试图发送来自raspberry的消息时,我得到如下信息:

代码语言:javascript
复制
What s your message?
test
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/socketutil.py", line 102, in getIpAddress
    return getaddr(config.PREFER_IP_VERSION) if ipVersion is None else getaddr(ipVersion)
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/socketutil.py", line 94, in getaddr
    ip = socket.getaddrinfo(hostname or socket.gethostname(), 80, family, socket.SOCK_STREAM, socket.SOL_TCP)[0][4][0]
  File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/core.py", line 515, in connect_and_handshake
    sslContext=sslContext)
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/socketutil.py", line 266, in createSocket
    if getIpVersion(connect[0]) == 4:
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/socketutil.py", line 68, in getIpVersion
    address = getIpAddress(hostnameOrAddress)
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/socketutil.py", line 106, in getIpAddress
    return getaddr(0)
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/socketutil.py", line 94, in getaddr
    ip = socket.getaddrinfo(hostname or socket.gethostname(), 80, family, socket.SOCK_STREAM, socket.SOL_TCP)[0][4][0]
  File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/pi/Desktop/client.py", line 10, in <module>
    message_server.show_message(msg)
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/core.py", line 275, in __getattr__
    self._pyroGetMetadata()
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/core.py", line 615, in _pyroGetMetadata
    self.__pyroCreateConnection()
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/core.py", line 588, in __pyroCreateConnection
    uri = _resolve(self._pyroUri, self._pyroHmacKey)
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/core.py", line 1915, in _resolve
    return nameserver.lookup(uri.object)
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/core.py", line 275, in __getattr__
    self._pyroGetMetadata()
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/core.py", line 615, in _pyroGetMetadata
    self.__pyroCreateConnection()
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/core.py", line 596, in __pyroCreateConnection
    connect_and_handshake(conn)
  File "/home/pi/.local/lib/python3.7/site-packages/Pyro4/core.py", line 549, in connect_and_handshake
    raise ce
Pyro4.errors.CommunicationError: cannot connect to ('JAKOB-PC', 9090): [Errno -2] Name or service not known

我的个人电脑的防火墙已经禁用,所以不应该有任何问题。我的本地ip是192.168.1.5。我正在使用无头树莓,并在上面用puTTY和VNC编写代码。

我搜索了这个错误,但是找不到任何答案。任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-17 08:15:21

总之,我无法解决Pyro的问题,(几乎)没有人帮助我,所以我决定用'websockets‘代替。

您可以阅读文档这里,但我将在这里解释它。

首先,您需要两个具有网络连接的设备。您还必须对它们运行python 3.6.1。在此之后,您还需要安装websockets,如果您还没有在pip install websockets中安装websockets,或者我必须使用pip3 install websockets来安装websockets。

下面的代码运行在服务器上,并处理从客户端发送给它的消息。函数'hello‘是处理请求和返回响应的一个简单示例。“请求”是数据服务器接收到的数据,数据必须是字节,字符串是可迭代的。响应是通过将请求转换为整数、对其进行平方并将其转换回字符串来进行的。然后将此响应发送回客户端。“start_server”定义了服务器、定义其行为的函数(Hello)、服务器正在运行的ip地址(192.168.1.117)和接收请求的端口(8765)。

代码语言:javascript
复制
!/usr/bin/env python

import asyncio
import websockets

print("Running...")

async def hello(websocket, path):
    request = await websocket.recv()
    print("Request: " + request)

    response = str(int(request)*int(request))

    await websocket.send(response)
    print("Response:" + response)

start_server = websockets.serve(hello, "192.168.1.117", 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

下一位是客户端的代码。‘'uri’是服务器的ip和端口。函数'tellServ‘要求您输入一些数据(’tell‘变量)并将其发送到服务器。在此之后,它等待回复,一旦得到它,就会打印出来。在这种情况下,如果我输入号码"6“服务器将回答"36”。函数循环在while循环中,因此我可以发送多个数字,而不必重新启动脚本。

代码语言:javascript
复制
#!/usr/bin/env python

import asyncio
import websockets

uri = "ws://192.168.1.117:8765"

async def tellServ():
    async with websockets.connect(uri) as websocket:
        tell = input("Podatek ki ga posles: ")
        await websocket.send(tell)

        reply = await websocket.recv()
        print("Odgovor:")
        print(reply)
while 1:
    asyncio.get_event_loop().run_until_complete(tellServ())
票数 0
EN

Stack Overflow用户

发布于 2020-07-17 11:09:14

是我干的

代码语言:javascript
复制
#
# Server.py
#
from __future__ import print_function
import Pyro4


@Pyro4.expose
@Pyro4.behavior(instance_mode="single")
class Messenger(object):
    # This is a constructor
    def __init__(self):
        pass

    # This method will be called on the server
    def send_message(self, name, message):
        print("[{0}] {1}".format(name, message))


def main():
    Pyro4.Daemon.serveSimple(
        {
            Messenger: "example.messenger"
        },
        ns=True)


if __name__ == "__main__":
    main()
代码语言:javascript
复制
#
# Client.py
#

# This is the code that visits the warehouse.
import sys

import Pyro4
import Pyro4.util


sys.excepthook = Pyro4.util.excepthook

messenger = Pyro4.Proxy("PYRONAME:example.messenger@192.168.1.5")
messenger.send_message("Tim", "Hello!")

然后跑

  • python -m Pyro4.naming -n 192.168.1.5
  • python Server.py
  • python Client.py
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62951995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档