Link to home
Start Free TrialLog in
Avatar of James Hancock
James HancockFlag for United States of America

asked on

Why does this site's Python example code not work?

Hi.
If I copy this site's Server & Client example code into IDLE, it explodes,

"connection refused" !

It looks straightforward. - but it doesn't connect, whether I have the server or client on my Mac or PC, respectively

What am I missing ? I got it working, aside,  in UDP, but now TCP is not simple like in Java.

That code's page is here
What is the ultimate Python networking site?

Thanks
Avatar of pepr
pepr

The code works for me as is -- see the snapshot:

User generated image
... both in Python 2 and in Python 3 (after slight modification).  Try to ping 127.0.0.1 to see whether the IP works (this should always work if the drivers are installed correctly and the service is not disabled somehow). Try netstat -a to learn more about your ports, and choose another if it is already used.
Avatar of James Hancock

ASKER

Thanks

Improvement.

My localhost server at least registers connections from Client runs.

The server registers the local connection attempt, but it doesn't send the text input back.


I don't ask for text input, but hardcode it instead
data = "moo"

and the Server.py cmd line error is on the q.send(data)
TypeError : str doesn't support the buffer interface.
So, it can't send a hardcoded string.
Suggestions?
Thx
The reason for the error is clear. You do use Python 3, and you try to send bytes of the string. However, Python 3 strings are abstract. There are no bytes to get implicitly. You have to explicitly encode the strings to bytes using some encoding. The data must be of the bytes type. Try to set
data = 'moo'.encode('utf-8')

Open in new window

.

The client gets (the stream of) bytes and if you need to get text, you have to decode the bytes at the client side:
print("Message from server : ", msg.decode('utf-8'))

Open in new window

Thanks

I put in the new code, with the Macbook OS X server's ip address as the host from the client
but it links now (tries) and on the Windows 7 client machine, it says "No connection could be made because the target machine actively refused it" [WinError 10061]

From a client machine's cmd console, pinging the OS X ip address with 32 bytes says
Reply from (a weird, wrong IP address) TTL expired In transit
4 times

The target machine is a macbook, client is PC. What might be wrong on the target machine?
Well, I do not know.
ASKER CERTIFIED SOLUTION
Avatar of James Hancock
James Hancock
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@beavoid: Do not consider this comment an objection. Next time, you can simply accept your own answer.
No one commented, so I put the solution in! once I found it