Jython socket typecasting problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Fink

    #1

    Jython socket typecasting problems

    I try to port a server application to Jython. At the moment I use
    Jython21\Lib\so cket.py
    Currently I do face problems with casting the string "localhost" to the
    desired value:
    D:\AUT_TEST\wor kspace\JyFIT>jy thon fit/JyFitServer2.py localhost 1234
    23
    ['fit/JyFitServer2.py ', 'localhost', '1234', '23']
    localhost
    Traceback (innermost last):
    File "fit/JyFitServer2.py ", line 146, in ?
    File "fit/JyFitServer2.py ", line 31, in run
    File "fit/JyFitServer2.py ", line 96, in establishConnec tion
    File "D:\AUT_TEST\Jy thon21\Lib\sock et.py", line 135, in connect
    TypeError: java.net.Socket (): 1st arg can't be coerced to
    java.net.InetAd dress or String

    The cast to Integer for the port does not work either:

    D:\AUT_TEST\wor kspace\JyFIT>jy thon fit/JyFitServer2.py "" 1234 23
    ['fit/JyFitServer2.py ', '', '1234', '23']

    Traceback (innermost last):
    File "fit/JyFitServer2.py ", line 146, in ?
    File "fit/JyFitServer2.py ", line 31, in run
    File "fit/JyFitServer2.py ", line 96, in establishConnec tion
    File "D:\AUT_TEST\Jy thon21\Lib\sock et.py", line 135, in connect
    TypeError: java.net.Socket (): 2nd arg can't be coerced to int

    This is the code section of my server class (I cut this from a Python
    example):
    def establishConnec tion(self):
    self.socket = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    self.socket.con nect((self.host , self.port))
    Do I have to use explicit typecasting? How can this be achieved?

  • Diez B. Roggisch

    #2
    Re: Jython socket typecasting problems

    > This is the code section of my server class (I cut this from a Python[color=blue]
    > example):
    > def establishConnec tion(self):
    > self.socket = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    > self.socket.con nect((self.host , self.port))
    > Do I have to use explicit typecasting? How can this be achieved?[/color]

    There is no such thing as explicit casting in python. You can coerce
    values to values of another type - e.g. float("1.0"), and there are some
    "magic" methods to support that (for that example __float__)

    However, that doesn't seem to be your problem. You just pass the wrong
    arguments, which makes the jython wrapping punke on you as only certain
    types can be dealt with.

    To me this looks as if you are supposed to do it like this:

    self.socket.con nect(self.host, self.port)

    Note the missing parentheses.

    Diez

    Comment

    • Donn Cave

      #3
      Re: Jython socket typecasting problems

      Quoth "Diez B. Roggisch" <deets@nospam.w eb.de>:
      | > This is the code section of my server class (I cut this from a Python
      | > example):
      | > def establishConnec tion(self):
      | > self.socket = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      | > self.socket.con nect((self.host , self.port))
      | > Do I have to use explicit typecasting? How can this be achieved?
      |
      | There is no such thing as explicit casting in python. You can coerce
      | values to values of another type - e.g. float("1.0"), and there are some
      | "magic" methods to support that (for that example __float__)
      |
      | However, that doesn't seem to be your problem. You just pass the wrong
      | arguments, which makes the jython wrapping punke on you as only certain
      | types can be dealt with.
      |
      | To me this looks as if you are supposed to do it like this:
      |
      | self.socket.con nect(self.host, self.port)
      |
      | Note the missing parentheses.

      It would have been pretty easy to check that!
      [color=blue][color=green][color=darkred]
      >>> s.connect(host, port)[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "<string>", line 1, in connect
      TypeError: connect() takes exactly one argument (2 given)[color=blue][color=green][color=darkred]
      >>> s.connect((host , port))
      >>> s.close()[/color][/color][/color]

      connect() used to support that, kind of by accident of how arguments
      were parsed. 1.5.4 may have been the last version with that feature.
      The documented API has always been connect(address ), though.

      I don't know anything about the Java problem, though. Only thing
      I could suggest is to try to do as one probably does in Java, and
      use a DNS function to resolve the name to an address, and then use
      that address. The way Python uses (hostname, port) to represent
      a internet address is convenient but hides this step of the process.
      Maybe 'localhost' actually doesn't resolve on the original poster's
      computer, and the implementation somehow turns this into a type issue.
      If it does resolve, then maybe its IP address will work better here.

      Donn Cave, donn@drizzle.co m

      Comment

      • Kent Johnson

        #4
        Re: Jython socket typecasting problems

        Mark Fink wrote:[color=blue]
        > The cast to Integer for the port does not work either:
        >
        > D:\AUT_TEST\wor kspace\JyFIT>jy thon fit/JyFitServer2.py "" 1234 23
        > ['fit/JyFitServer2.py ', '', '1234', '23']
        >
        > Traceback (innermost last):
        > File "fit/JyFitServer2.py ", line 146, in ?
        > File "fit/JyFitServer2.py ", line 31, in run
        > File "fit/JyFitServer2.py ", line 96, in establishConnec tion
        > File "D:\AUT_TEST\Jy thon21\Lib\sock et.py", line 135, in connect
        > TypeError: java.net.Socket (): 2nd arg can't be coerced to int
        >
        > This is the code section of my server class (I cut this from a Python
        > example):
        > def establishConnec tion(self):
        > self.socket = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
        > self.socket.con nect((self.host , self.port))
        > Do I have to use explicit typecasting? How can this be achieved?
        >[/color]

        This should work if you give correct arguments. For example:
        Jython 2.1 on java1.4.2_06 (JIT: null)
        Type "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
        >>> import socket
        >>> s=socket.socket (socket.AF_INET , socket.SOCK_STR EAM)
        >>> s.connect(('www .google.com', 80))
        >>> s.close()[/color][/color][/color]

        It would be helpful to know what self.host and self.port are. Put
        print type(self.host) , repr(self.host)
        print type(self.port) , repr(self.port)

        in establishConnec tion() to print out the values you are using.

        If host.port is the command line argument it is a string and you need to
        convert it to int with the int() function.
        [color=blue][color=green][color=darkred]
        >>> s.connect(('www .google.com', '80'))[/color][/color][/color]
        Traceback (innermost last):
        File "<console>" , line 1, in ?
        File "C:\Downloads\J ava\jython-2.1\Lib\socket. py", line 135, in connect
        TypeError: java.net.Socket (): 1st arg can't be coerced to
        java.net.InetAd dress or String

        Note it complains about the first arg; this may well be your error.

        In general Python does not coerce argumentt types for you. Jython will
        translate between Java and Python types but it won't go as far as
        converting a String to an int.

        Kent

        Comment

        • Steve Holden

          #5
          Re: Jython socket typecasting problems

          Diez B. Roggisch wrote:[color=blue][color=green]
          >>This is the code section of my server class (I cut this from a Python
          >>example):
          >> def establishConnec tion(self):
          >> self.socket = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
          >> self.socket.con nect((self.host , self.port))
          >>Do I have to use explicit typecasting? How can this be achieved?[/color]
          >
          >
          > There is no such thing as explicit casting in python. You can coerce
          > values to values of another type - e.g. float("1.0"), and there are some
          > "magic" methods to support that (for that example __float__)
          >
          > However, that doesn't seem to be your problem. You just pass the wrong
          > arguments, which makes the jython wrapping punke on you as only certain
          > types can be dealt with.
          >
          > To me this looks as if you are supposed to do it like this:
          >
          > self.socket.con nect(self.host, self.port)
          >
          > Note the missing parentheses.
          >[/color]
          """connect(addr ess)
          Connect to a remote socket at address. (The format of address
          depends on the address family -- see above.) Note: This method has
          historically accepted a pair of parameters for AF_INET addresses instead
          of only a tuple. This was never intentional and is no longer available
          in Python 2.0 and later. """

          So if Jython 2.1 is conformant it should indeed require a single
          argument which is an (IPAddress, port) tuple.

          It might be that the OP can't, for some reason associate with the local
          environment, resolve "localhost" to an IP address. What happens if
          "127.0.0.1" is substituted?

          Irrespective of that, it does seem that the error message is at best
          misleading and at most just plain wrong. There's aproject to bring
          Jython up to a more recent specification, but it will take time ...

          regards
          Steve
          --
          Steve Holden +44 150 684 7255 +1 800 494 3119
          Holden Web LLC www.holdenweb.com
          PyCon TX 2006 www.python.org/pycon/

          Comment

          • Mark Fink

            #6
            Re: Jython socket typecasting problems

            thanks to the help of this group I moved a tiny step forward. Obviously
            it is not possible to resolve name localhost:
            Type "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
            >>> import socket
            >>> s=socket.socket (socket.AF_INET , socket.SOCK_STR EAM)
            >>> s.connect(local host, 8080)[/color][/color][/color]
            Traceback (innermost last):
            File "<console>" , line 1, in ?
            NameError: localhost[color=blue][color=green][color=darkred]
            >>> s.connect((loca lhost, 8080))[/color][/color][/color]
            Traceback (innermost last):
            File "<console>" , line 1, in ?
            NameError: localhost[color=blue][color=green][color=darkred]
            >>> s.connect(('127 .0.0.1', 8080))[/color][/color][/color]
            Traceback (innermost last):
            File "<console>" , line 1, in ?
            File "D:\AUT_TEST\Jy thon21\Lib\sock et.py", line 135, in connect
            java.net.Connec tException: Connection refused: connect
            at java.net.PlainS ocketImpl.socke tConnect(Native Method)
            at java.net.PlainS ocketImpl.doCon nect(PlainSocke tImpl.java:333)
            at
            java.net.PlainS ocketImpl.conne ctToAddress(Pla inSocketImpl.ja va:195)
            at java.net.PlainS ocketImpl.conne ct(PlainSocketI mpl.java:182)
            at java.net.SocksS ocketImpl.conne ct(SocksSocketI mpl.java:366)
            at java.net.Socket .connect(Socket .java:507)

            Unfortunately this is not the solution (I tried 127.0.0.1 before my
            first post).
            I added the two print lines as recommended:
            self.socket = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
            print type(self.host) , repr(self.host)
            print type(self.port) , repr(self.port)
            self.socket.con nect((self.host , self.port))
            self.socketOutp ut = self.socket.get OutputStream()

            And more specific information:
            D:\AUT_TEST\wor kspace\JyFIT\fi t>jython JyFitServer2.py 127.0.0.1 1234
            12
            ['JyFitServer2.p y', '127.0.0.1', '1234', '12']
            127.0.0.1
            org.python.core .PyString '127.0.0.1'
            org.python.core .PyString '1234'
            Traceback (innermost last):
            File "JyFitServer2.p y", line 148, in ?
            File "JyFitServer2.p y", line 31, in run
            File "JyFitServer2.p y", line 98, in establishConnec tion
            File "D:\AUT_TEST\Jy thon21\Lib\sock et.py", line 135, in connect
            TypeError: java.net.Socket (): 1st arg can't be coerced to
            java.net.InetAd dress or String

            No casting from string to string?? I learned that there is no such
            thing as explicit typecasting. What to do?

            Comment

            • Donn Cave

              #7
              Re: Jython socket typecasting problems

              In article <1139857815.855 155.58400@g43g2 000cwa.googlegr oups.com>,
              "Mark Fink" <mark@mark-fink.de> wrote:[color=blue]
              > thanks to the help of this group I moved a tiny step forward. Obviously
              > it is not possible to resolve name localhost:
              > Type "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
              > >>> import socket
              > >>> s=socket.socket (socket.AF_INET , socket.SOCK_STR EAM)[/color][/color][/color]
              [color=blue][color=green][color=darkred]
              > >>> s.connect((loca lhost, 8080))[/color][/color]
              > Traceback (innermost last):
              > File "<console>" , line 1, in ?
              > NameError: localhost[/color]

              Did you mean to put 'localhost' in quotes?
              [color=blue][color=green][color=darkred]
              > >>> s.connect(('127 .0.0.1', 8080))[/color][/color]
              > Traceback (innermost last):
              > File "<console>" , line 1, in ?
              > File "D:\AUT_TEST\Jy thon21\Lib\sock et.py", line 135, in connect
              > java.net.Connec tException: Connection refused: connect[/color]
              [color=blue]
              > Unfortunately this is not the solution (I tried 127.0.0.1 before my
              > first post).[/color]

              It does seem to be the solution to the problem you asked
              about, since you don't get a type error here. The problem
              you report now is quite different. There is no obvious
              problem with the Python code, this time - it just depends
              on what you're trying to do.
              [color=blue]
              > I added the two print lines as recommended:
              > self.socket = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
              > print type(self.host) , repr(self.host)
              > print type(self.port) , repr(self.port)
              > self.socket.con nect((self.host , self.port))
              > self.socketOutp ut = self.socket.get OutputStream()
              >
              > And more specific information:
              > D:\AUT_TEST\wor kspace\JyFIT\fi t>jython JyFitServer2.py 127.0.0.1 1234
              > 12
              > ['JyFitServer2.p y', '127.0.0.1', '1234', '12']
              > 127.0.0.1
              > org.python.core .PyString '127.0.0.1'
              > org.python.core .PyString '1234'
              > Traceback (innermost last):
              > File "JyFitServer2.p y", line 148, in ?
              > File "JyFitServer2.p y", line 31, in run
              > File "JyFitServer2.p y", line 98, in establishConnec tion
              > File "D:\AUT_TEST\Jy thon21\Lib\sock et.py", line 135, in connect
              > TypeError: java.net.Socket (): 1st arg can't be coerced to
              > java.net.InetAd dress or String
              >
              > No casting from string to string?? I learned that there is no such
              > thing as explicit typecasting. What to do?[/color]

              If I understand what you're doing there, it seems to
              confirm that when this socket implementation encounters
              an error in a network address, during connect(), it
              raises a type error. The thing to do, therefore, is
              expect TypeError, where in C Python you would expect
              a socket.gaierror or socket.error (in older versions)
              exception.

              Donn Cave, donn@u.washingt on.edu

              Comment

              • Kent Johnson

                #8
                Re: Jython socket typecasting problems

                Mark Fink wrote:[color=blue]
                > I added the two print lines as recommended:
                > self.socket = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
                > print type(self.host) , repr(self.host)
                > print type(self.port) , repr(self.port)
                > self.socket.con nect((self.host , self.port))
                > self.socketOutp ut = self.socket.get OutputStream()
                >
                > And more specific information:
                > D:\AUT_TEST\wor kspace\JyFIT\fi t>jython JyFitServer2.py 127.0.0.1 1234
                > 12
                > ['JyFitServer2.p y', '127.0.0.1', '1234', '12']
                > 127.0.0.1
                > org.python.core .PyString '127.0.0.1'
                > org.python.core .PyString '1234'
                > Traceback (innermost last):
                > File "JyFitServer2.p y", line 148, in ?
                > File "JyFitServer2.p y", line 31, in run
                > File "JyFitServer2.p y", line 98, in establishConnec tion
                > File "D:\AUT_TEST\Jy thon21\Lib\sock et.py", line 135, in connect
                > TypeError: java.net.Socket (): 1st arg can't be coerced to
                > java.net.InetAd dress or String
                >
                > No casting from string to string?? I learned that there is no such
                > thing as explicit typecasting. What to do?[/color]

                The problem is that the second arg is not an int. The second problem is
                that the error message errorneously points to the first arg. See my
                earlier post.

                Kent

                Comment

                • Steve Holden

                  #9
                  Re: Jython socket typecasting problems

                  Kent Johnson wrote:[color=blue]
                  > Mark Fink wrote:
                  >[color=green]
                  >>I added the two print lines as recommended:
                  >> self.socket = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
                  >> print type(self.host) , repr(self.host)
                  >> print type(self.port) , repr(self.port)
                  >> self.socket.con nect((self.host , self.port))
                  >> self.socketOutp ut = self.socket.get OutputStream()
                  >>
                  >>And more specific information:
                  >>D:\AUT_TEST\w orkspace\JyFIT\ fit>jython JyFitServer2.py 127.0.0.1 1234
                  >>12
                  >>['JyFitServer2.p y', '127.0.0.1', '1234', '12']
                  >>127.0.0.1
                  >>org.python.co re.PyString '127.0.0.1'
                  >>org.python.co re.PyString '1234'
                  >>Traceback (innermost last):
                  >> File "JyFitServer2.p y", line 148, in ?
                  >> File "JyFitServer2.p y", line 31, in run
                  >> File "JyFitServer2.p y", line 98, in establishConnec tion
                  >> File "D:\AUT_TEST\Jy thon21\Lib\sock et.py", line 135, in connect
                  >>TypeError: java.net.Socket (): 1st arg can't be coerced to
                  >>java.net.Inet Address or String
                  >>
                  >>No casting from string to string?? I learned that there is no such
                  >>thing as explicit typecasting. What to do?[/color]
                  >
                  >
                  > The problem is that the second arg is not an int. The second problem is
                  > that the error message errorneously points to the first arg. See my
                  > earlier post.
                  >[/color]

                  OK, so presumably you've worked out that where you assign

                  self.port = something

                  you need to use

                  self.port = int(something)

                  That should do it - Python doesn't have implicit type conversion like
                  Perl does, but there are plenty of conversion functions available. That
                  error message really wasn't helpful, was it?

                  regards
                  Steve
                  --
                  Steve Holden +44 150 684 7255 +1 800 494 3119
                  Holden Web LLC www.holdenweb.com
                  PyCon TX 2006 www.python.org/pycon/

                  Comment

                  Working...