Python客户端无法与ruby服务器通信。我得到[Errno 10061]无法建立连接,因为目标计算机主动拒绝了我

2024-10-02 02:32:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我打算在rubyonrails应用程序的数据库中运行Python编写的机器学习算法。经过一些研究,我发现了套接字,因此创建了一个Ruby服务器和Python客户机。我在两个不同的命令提示符终端上运行它们。在

下面是Ruby服务器代码:

require "socket"

server = TCPServer.open(2000)

loop {
  client = server.accept
  client.puts(Time.now.ctime)
  client.puts "Closing the connection. Bye!"
  client.close
  }

下面是Python客户端代码:

^{2}$

我不明白问题出在哪里。请协助。在


Tags: 代码服务器算法client机器数据库应用程序终端
1条回答
网友
1楼 · 发布于 2024-10-02 02:32:32

对于我上面的问题给出了深刻的回答,代码Ruby服务器和Python客户机应该如下所示。在

对于Ruby服务器:

require "socket" # Get sockets from stdlib

server = TCPServer.open("127.0.0.1" , 2000) # Socket to listen on port 2000

loop {                     # Server runs forever
  client = server.accept   # Wait for a client to connect
  client.puts(Time.now.ctime) # Send the time to the client
  client.puts "Closing the connection. Bye!" 
  client.close # Disconnect from the client

  }

对于Python客户机:

^{pr2}$

Ruby中TCPServer类的open()方法接受两个参数。第一个是主机名,第二个是端口,即

TCPServer.open(hostname , port)

相关问题 更多 >

    热门问题