2016-05-12 2 views
0

Ruby on RailsアプリケーションのデータベースのデータにPythonで記述された機械学習アルゴリズムを実行する予定です。いくつかの研究の後、私はソケットを発見し、RubyサーバーとPythonクライアントを作成しました。私は2つの異なるコマンドプロンプト端末でそれらを実行しています。ここでPythonクライアントはrubyサーバと通信できません。私は[Errno 10061]を取得するターゲットマシンが積極的にそれを拒否したために接続できません

は、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クライアントコードです:

import socket 

s = socket.socket() 
host = "localhost" 
port = 2000 
s.connect((host , port)) 

問題がどこにあるか私は理解していません。親切にお手伝いします。

+0

すなわちポートのホスト名であること第一及び第二のbothsプログラムは、同じホスト上で実行していますか? –

+0

あなたのコードはここで問題なく動作します。 –

+0

しかし、なぜこの非常に複雑なアプローチですか?なぜルビーにこだわるだけではないのですか?あなたはrubyを使用したくないのですがなぜPythonで直接データベースに接続するだけではないのですか? – e4c5

答えて

1

RubyサーバーとPythonクライアントのコードの上に私の質問に対する洞察力のある答えを以下に示す必要があります。 Rubyのサーバーの場合

:Pythonクライアントのために

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 

    } 

import socket # Import socket module 

s = socket.socket() # Create a socket object 
host = "127.0.0.1" 
port = 2000 # Reserve a port for your service. 
s.connect((host , port)) 
print s.recv(1024) 
s.close() # Close the socket when done 

RubyでTCPServerのクラスのopen()メソッドは、2つのパラメータを取ります。

TCPServer.open(hostname , port) 
関連する問題