0

我有两台不同的电脑。我想让其中一个成为主机,另一个成为发送信息的代理。我想用 osbrain 做这个。但是,我面临一个问题。

Host Agent 正在打开服务器。2. 我的代理连接到服务器,但我无法在两者之间进行通信。你能帮助我吗?

Host_Agent.py

from osbrain import run_nameserver
from osbrain.proxy import locate_ns
from osbrain import run_agent
import osbrain
import time
import pickle

def log_message(agent, message):
    agent.log_info('Received: %s' % message)

if __name__ == '__main__':

    ns_sock = '127.0.0.1:1212'

    osbrain.config['TRANSPORT'] = 'tcp'

    ns_proxy = run_nameserver(ns_sock)
    ns_addr = locate_ns(ns_sock)
    # New Agent

    while True:
        time.sleep(5)
        agents_in_NS = osbrain.nameserver.NameServer.agents(ns_proxy)
        print('Current agents in Nameserver are: %s' %agents_in_NS)

新代理.py

from osbrain import NSProxy
from osbrain import run_agent
import osbrain
import Pyro4
import pickle
import time

if __name__ == '__main__':

    ns_addr = '127.0.0.1:1212'

    osbrain.config['TRANSPORT'] = 'tcp'

    ns_proxy = NSProxy(ns_addr)

    print('Registering Agent with server...')
    agent_proxy = run_agent('Agent3', ns_addr)
    address = agent_proxy.bind('PUSH', alias='main')
    time.sleep(5)
    print('I have joined the nameserver!')

    for i in range(1000):
        print("I try to say HEY!")
        agent_proxy.send('main',message='Hey')
        print("I tried")
        time.sleep(2)

    print("Done")
4

1 回答 1

2

127.0.0.1 is the loopback address so anywhere you us it the server will only listen for connections from only the machine it's running on

In Host_Agent.py, change the localhost address(ns_sock = '127.0.0.1:1212') to a whildcard (ns_sock = '0.0.0.0:1212'). This will let your server listen for all inbound connections from all hosts.

Now, in New_Agent.py, replace localhost with the IP address of the machine that Host_Agent.py is running on

ns_addr = '${IP_ADDRESS_OF_OTHER_MACHINE}:1212'
于 2020-02-07T15:07:02.453 回答