Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4312

Erlang client cannot connect to Python server: exception error: econnrefused

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    Description

      Connecting the Erlang client with the Python server gives connection error:

      Erlang client:

      – the examples are taken from the Apache Thrift tutorials —

      -module(client).

      -include("calculator_thrift.hrl").

      -export([t/0]).

      p(X) ->
      io:format("~p~n", [X]),
      ok.

      t() ->
      Port = 9090,

      Unknown macro: {ok, Client0}

      = thrift_client_util:new("localhost",
      Port,
      calculator_thrift,
      []),

      {Client1, {ok, ok}} = thrift_client:call(Client0, ping, []),
      io:format("ping~n", []),

      {Client2, {ok, Sum}} = thrift_client:call(Client1, add, [1, 1]),
      io:format("1+1=~p~n", [Sum]),

      {Client3, {ok, Sum1}} = thrift_client:call(Client2, add, [1, 4]),
      io:format("1+4=~p~n", [Sum1]),

      Work = #'Work'

      Unknown macro: {op=?TUTORIAL_OPERATION_SUBTRACT, num1=15, num2=10}

      ,
      {Client4, {ok, Diff}} = thrift_client:call(Client3, calculate, [1, Work]),
      io:format("15-10=~p~n", [Diff]),

      {Client5, {ok, Log}} = thrift_client:call(Client4, getStruct, [1]),
      io:format("Log: ~p~n", [Log]),

      Client6 =
      try
      Work1 = #'Work'

      Unknown macro: {op=?TUTORIAL_OPERATION_DIVIDE, num1=1, num2=0}

      ,
      {ClientS1, {ok, _Quot}} = thrift_client:call(Client5, calculate, [2, Work1]),

      io:format("LAME: exception handling is broken~n", []),
      ClientS1
      catch
      throw:

      Unknown macro: {ClientS2, Z}

      ->
      io:format("Got exception where expecting - the " ++
      "following is NOT a problem!!!~n"),
      p(Z),
      ClientS2
      end,

      {Client7, {ok, ok}} = thrift_client:call(Client6, zip, []),
      io:format("zip~n", []),

      {_Client8, ok} = thrift_client:close(Client7),
      ok.

      Python server:

      #!/usr/bin/env python

      from tutorial import Calculator
      from tutorial.ttypes import InvalidOperation, Operation

      from shared.ttypes import SharedStruct

      from thrift.transport import TSocket
      from thrift.transport import TTransport
      from thrift.protocol import TBinaryProtocol
      from thrift.server import TServer

      class CalculatorHandler:
      def _init_(self):
      self.log = {}

      def ping(self):
      print('ping()')

      def add(self, n1, n2):
      print('add(%d,%d)' % (n1, n2))
      return n1 + n2

      def calculate(self, logid, work):
      print('calculate(%d, %r)' % (logid, work))

      if work.op == Operation.ADD:
      val = work.num1 + work.num2
      elif work.op == Operation.SUBTRACT:
      val = work.num1 - work.num2
      elif work.op == Operation.MULTIPLY:
      val = work.num1 * work.num2
      elif work.op == Operation.DIVIDE:
      if work.num2 == 0:
      x = InvalidOperation()
      x.whatOp = work.op
      x.why = 'Cannot divide by 0'
      raise x
      val = work.num1 / work.num2
      else:
      x = InvalidOperation()
      x.whatOp = work.op
      x.why = 'Invalid operation'
      raise x

      log = SharedStruct()
      log.key = logid
      log.value = '%d' % (val)
      self.log[logid] = log

      return val

      def getStruct(self, key):
      print('getStruct(%d)' % (key))
      return self.log[key]

      def zip(self):
      print('zip()')

      if _name_ == '_main_':
      handler = CalculatorHandler()
      processor = Calculator.Processor(handler)
      transport = TSocket.TServerSocket(port=9090)
      tfactory = TTransport.TBufferedTransportFactory()
      pfactory = TBinaryProtocol.TBinaryProtocolFactory()

      server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

      1. You could do one of these for a multithreaded server
      2. server = TServer.TThreadedServer(
      3. processor, transport, tfactory, pfactory)
      4. server = TServer.TThreadPoolServer(
      5. processor, transport, tfactory, pfactory)

      print('Starting the server...')
      server.serve()
      print('done.')

      Runtime protocol:

      EBIN Path : D:\SoftDevelopment\Projects\erlang-and-thrift_idea\erlang-and-thrift_build\default\lib\erlang_and_thrift\ebin D:\SoftDevelopment\Projects\erlang-and-thrift_idea\erlang-and-thrift_build\default\lib\jsx\ebin D:\SoftDevelopment\Projects\erlang-and-thrift_idea\erlang-and-thrift_build\default\lib\thrift\ebin
      ------------------------------------------
      Eshell V9.0 (abort with ^G)
      1> client:t().

        • exception error: no match of right hand side value
          Unknown macro: {error,econnrefused}

          in function client:t/0 (d:/SoftDevelopment/Projects/erlang-and-thrift_idea/erlang-and-thrift/_build/default/lib/erlang_and_thrift/src/client.erl, line 33)
          2> Terminate batch job (Y/N)? y

      D:\SoftDevelopment\Projects\erlang-and-thrift_idea\erlang-and-thrift>tcping64 localhost 9090

      Probing ::1:9090/tcp - Port is open - time=0.580ms
      Probing ::1:9090/tcp - Port is open - time=0.622ms
      Probing ::1:9090/tcp - Port is open - time=1.038ms
      Probing ::1:9090/tcp - Port is open - time=1.050ms

      Ping statistics for ::1:9090
      4 probes sent.
      4 successful, 0 failed.
      Approximate trip times in milli-seconds:
      Minimum = 0.580ms, Maximum = 1.050ms, Average = 0.822ms

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            jking3 James E. King III
            wwe Walter Weinmann
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment