Jython socket typecasting problems

Donn Cave donn at drizzle.com
Sat Feb 11 17:02:19 EST 2006


Quoth "Diez B. Roggisch" <deets at nospam.web.de>:
| > This is the code section of my server class (I cut this from a Python
| > example):
| >     def establishConnection(self):
| >         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
| >         self.socket.connect((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.connect(self.host, self.port)
|
| Note the missing parentheses.

It would have been pretty easy to check that!

   >>> s.connect(host, port)
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
     File "<string>", line 1, in connect
   TypeError: connect() takes exactly one argument (2 given)
   >>> s.connect((host, port))
   >>> s.close()

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 at drizzle.com



More information about the Python-list mailing list