473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Jython socket typecasting problems

I try to port a server application to Jython. At the moment I use
Jython21\Lib\socket.py
Currently I do face problems with casting the string "localhost" to the
desired value:
D:\AUT_TEST\workspace\JyFIT>jython 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 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

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

D:\AUT_TEST\workspace\JyFIT>jython 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 establishConnection
File "D:\AUT_TEST\Jython21\Lib\socket.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 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?

Feb 11 '06 #1
8 5950
> 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.

Diez
Feb 11 '06 #2
Quoth "Diez B. Roggisch" <de***@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, do**@drizzle.com
Feb 11 '06 #3
Mark Fink wrote:
The cast to Integer for the port does not work either:

D:\AUT_TEST\workspace\JyFIT>jython 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 establishConnection
File "D:\AUT_TEST\Jython21\Lib\socket.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 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?


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.
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('www.google.com', 80))
s.close()
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 establishConnection() 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.
s.connect(('www.google.com', '80'))

Traceback (innermost last):
File "<console>", line 1, in ?
File "C:\Downloads\Java\jython-2.1\Lib\socket.py", line 135, in connect
TypeError: java.net.Socket(): 1st arg can't be coerced to
java.net.InetAddress 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
Feb 12 '06 #4
Diez B. Roggisch wrote:
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.

"""connect(address)
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/

Feb 13 '06 #5
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 s.connect((localhost, 8080)) Traceback (innermost last):
File "<console>", line 1, in ?
NameError: localhost 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
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
at
java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava: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(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?

Feb 13 '06 #6
In article <11*********************@g43g2000cwa.googlegroups. com>,
"Mark Fink" <ma**@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, do**@u.washington.edu
Feb 13 '06 #7
Mark Fink wrote:
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?


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
Feb 13 '06 #8
Kent Johnson wrote:
Mark Fink wrote:
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?

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.


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/

Feb 13 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave Benjamin | last post by:
Hey good people, I've been doing a lot of simultaneous Jython and CPython programming lately, and just wanted to say, with no intended ill will toward any of the individuals who have been...
0
by: Hameed Khan | last post by:
hi all, i am getting some problems with my first socket script. can any one of you point me why this is happening. the server script suppose to accept one connection at a time and send countdown...
4
by: angel | last post by:
A java runtime environment includes jvm and java class (for example classes.zip in sun jre). Of course jython need jvm,but does it need java class. Thanx
7
by: Jan Gregor | last post by:
Hello I found that jython catches exact java exceptions, not their subclasses. Is there some way to get around this limitation (or error) ? My program has class representing database source...
1
by: scott | last post by:
I installed darwinports and did a "sudo port install jython" ------------------------- scott$ which jython /opt/local/bin/jython ------------------------- Jython works in interactive...
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
1
by: Mark Fink | last post by:
Hi there, I am about to port a server application from Java to Jython. For the socket part I found some examples written in Python. I have problems to figure out the socket part of the...
0
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= | last post by:
Hello, First of all I wish you a good day. My help request is about .NET asynchrounus socket communication. I have developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I...
5
by: sarup26 | last post by:
Hello .. I would like to know more about Python and Jython? What is the difference between both of them? What is the future for Jython and which are the areas where it is used? Swot
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.