Jython socket typecasting problems

Donn Cave donn at u.washington.edu
Mon Feb 13 15:01:24 EST 2006


In article <1139857815.855155.58400 at g43g2000cwa.googlegroups.com>,
 "Mark Fink" <mark at mark-fink.de> wrote:
> 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.
> >>> import socket
> >>> s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

> >>> s.connect((localhost, 8080))
> Traceback (innermost last):
>   File "<console>", line 1, in ?
> NameError: localhost

Did you mean to put 'localhost' in quotes?

> >>> s.connect(('127.0.0.1', 8080))
> Traceback (innermost last):
>   File "<console>", line 1, in ?
>   File "D:\AUT_TEST\Jython21\Lib\socket.py", line 135, in connect
> java.net.ConnectException: Connection refused: connect

> Unfortunately this is not the solution (I tried 127.0.0.1 before my
> first post).

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.

> I added the two print lines as recommended:
>         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>         print type(self.host), repr(self.host)
>         print type(self.port), repr(self.port)
>         self.socket.connect((self.host, self.port))
>         self.socketOutput = self.socket.getOutputStream()
> 
> And more specific information:
> D:\AUT_TEST\workspace\JyFIT\fit>jython JyFitServer2.py 127.0.0.1 1234
> 12
> ['JyFitServer2.py', '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.py", line 148, in ?
>   File "JyFitServer2.py", line 31, in run
>   File "JyFitServer2.py", line 98, in establishConnection
>   File "D:\AUT_TEST\Jython21\Lib\socket.py", line 135, in connect
> TypeError: java.net.Socket(): 1st arg can't be coerced to
> java.net.InetAddress or String
> 
> No casting from string to string?? I learned that there is no such
> thing as explicit typecasting. What to do?

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 at u.washington.edu



More information about the Python-list mailing list