Solved

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

Posted on 2014-09-09
8
253 Views
Last Modified: 2014-09-29
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
0
Comment
Question by:beavoid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 40313745
The code works for me as is -- see the snapshot:

Client and server communication.
... 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.
0
 

Author Comment

by:beavoid
ID: 40314512
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
0
 
LVL 29

Expert Comment

by:pepr
ID: 40314552
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:beavoid
ID: 40318475
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?
0
 
LVL 29

Expert Comment

by:pepr
ID: 40318648
Well, I do not know.
0
 

Accepted Solution

by:
beavoid earned 0 total points
ID: 40342788
My networking code all works now!

Instead of localhost address, I had to use the literal IP address of the machine.
The major problem was also not being able to include data in a message.

making it a byte array was the solution
data = bytearray(5)
Data[0]=1

..send(data)
0
 
LVL 29

Expert Comment

by:pepr
ID: 40342840
@beavoid: Do not consider this comment an objection. Next time, you can simply accept your own answer.
0
 

Author Closing Comment

by:beavoid
ID: 40349533
No one commented, so I put the solution in! once I found it
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASA NAT rule change 3 86
How to join another Exchange server to a network, across a WAN 9 43
HomeGroup Issues 3 41
Wifi Router Confliction with Network 12 50
When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
Most of the applications these days are on Cloud. Cloud is ubiquitous with many service providers in the market. Since it has many benefits such as cost reduction, software updates, remote access, disaster recovery and much more.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question