Solved

Python networking refusing connections. Help!

Posted on 2014-09-13
20
279 Views
Last Modified: 2014-09-19
Hi
Still at it..

My Python TCP networking code is plagued with Connection refusals. Both between machines and on localhost.
Someone said on another question that this worked for them. - and showed me windows cmd window doing it correctly.
I am trying to run the server code in IDLE alone and it is giving me connection refusals with the client...

Traceback (most recent call last):
  File "/Users/jameshancock/Desktop/deep red wars/DRWpython/Client.py", line 11, in <module>
    s.connect((host,port))          # Connect to server address
ConnectionRefusedError: [Errno 61] Connection refused
>>> Listening for connections..
SyntaxError: invalid syntax

Open in new window


It is still the code on this page here

Sorry to ask a new question, but I don't understand these connection refusals.
Is it a firewall thing?
I think IDLE may have a problem with trying to run two similar .py's at a time? Can I safely run two at a time in IDLE?
how do I start these from a console on a Mac? - so it is clearly two different Threads that don't crash one another with similar PORT usage on one Machine?
0
Comment
Question by:beavoid
  • 12
  • 7
20 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I'm trying to run both the code you have referred to and the demo code from the Python site here https://docs.python.org/2/library/socket.html#example and so far, they don't work and the errors don't make any sense.  I'm running Python 2.6 and the demos are for 2.7 but I don't know if that makes any difference.  Also, there are typos in the code you were using.

For what it's worth, the first two demos about hostname on this page work.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I installed Python 2.7.8 and it didn't make any difference.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I don't have access to a Mac, but your code works fine on Windows:

Screenshot (Python 2.7)
It would seem that there are environmental differences between Windows and Mac. Perhaps it's the firewall, but someone with a Mac would have to confirm that.
0
 

Author Comment

by:beavoid
Comment Utility
Okay, thanks

What a let down ! :)
This is like saying java.awt.Frame works in JDK 1.x and not JDK 4.x and on Windows, not Mac

Dave, which example page works fine? Just on Windows?

"For what it's worth, the first two demos about hostname on this page work." Will that be what I need, to do an RTS server listening socket?

Thanks
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Actually, after seeing @kaufmed's code, I realized I was doing something wrong.  So I ran the original client/server that you were using in IDLE and it worked.  In a while, I'll copy it over to my Mac (OSX 10.6) and see if it works.  I may have to install Python on that machine.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Ok, I put your original code on my MacBook Pro and it does run using Python 2.6.1.  I had to change the 'host' to the IP address of the machine (in both programs) and click on 'Ok' to allow the Server code to accept connections.  But it did run.
0
 

Author Comment

by:beavoid
Comment Utility
To be sure,

My About window on IDLE says

Python Version 3.3.3  

IDLE version 3.3.3
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I did upgrade to Python 2.7.8 but my MacBook Pro is from about 2006 and is running OS X 10.6.8.  On the Python site is 'demo code' for Python 3 also.  Do you have any other Python programs working?
0
 

Author Comment

by:beavoid
Comment Utility
Yes
I can do all the newbie printf type stuff with data structures. I couldn't get TKinter going, but I was able to code up a decent start to a Python RTS server in UDP. I just need reliable TCP initial connections and then I'll be on my bike.
Thx
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
And this for Python 3.3: https://docs.python.org/3.3/library/socket.html#socket-objects  And this: http://pymotw.com/2/socket/tcp.html  I didn't get either of them working but maybe there is info you can use there.
0
 

Author Comment

by:beavoid
Comment Utility
Nice pages, Thanks

I don't seem to understand what ActiveTcl is. I read the description. It is not clear. Why does IDLE not come with the best tkinter automatically? Is it the only way to do windows functionality? Why is there not a simple JFrame equivalent in Python? Was Python intended to be mainly a console type language?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
ActiveTcl is just a different version that the Python people seem to prefer to the Apple version.  As to the rest of your 'Why's... who knows?  These things usually come about to solve a small problem and then get used for larger ones without any particular intention or plan.
0
 

Author Comment

by:beavoid
Comment Utility
So, if my IDLE says I have Tk version 8.5.9, I'm ready to go on tkinter?

For that 3.3 networking functions and methods page, thanks, but it seems strange that it says some functionality apparently is for Windows, and not Mac.
Isn't it platform independent?
Thanks
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
As I understand that page, if IDLE is working, already have tkinter.  I see a number of comments about Windows on that page.  I don't know of any language that is completely "platform independent".  PHP has a number or Windows-only or Linux-only extensions.  PHP sockets probably have similar limitations.
0
 

Author Comment

by:beavoid
Comment Utility
Thanks

I found this page about Windows inadequacy...

(That you can only do local connections, which is also what I also discovered, btw)

It suggests using SocketServer. Does that sound valid?

here

How does this SocketServer page look below?
Dave, after, what, 3 months of plaguing you with Java TCP questions, could this be the end of it?

I found this SocketServer page here

? ?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I told you before that I never use 'localhost' because I have nothing set up to run on localhost.  I use the IP address of the machine.  

I copied your original Python (not Java) code to another machine.  I ran the server part on this machine and the client part on the other machine and they communicated just fine.  It also worked from my Mac to this machine.

The server code needs the IP address of the machine that it is running on and the firewall needs to be opened so it can receive requests from the client.  The client code needs the IP address of the machine that it is trying to contact which will be the IP address of the machine that the server code is running on.  Since it is an out-going request, it will not need the standard Windows firewall to open for it.

SocketServer may work, I don't know.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I just had another odd thing happen.  If you close the IDLE program window before the program finishes (the server program in particular), the 'socket' set up by the program persists unless you go into Task Manager and kill 'pythonw.exe'.  You get a 'socket already in use' error message.
0
 

Author Comment

by:beavoid
Comment Utility
I am getting very promising results now using SocketServers here

..Thanks to all the above advice.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome, glad to help.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Python algorithm 20 135
Python Install On Mac Yosemite 8 90
Macports Import beautifulsoup4 Problem 6 58
Python 2.7 - Passing arguments 8 46
The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now