?
Solved

Python networking refusing connections. Help!

Posted on 2014-09-13
20
Medium Priority
?
317 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
[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
  • 12
  • 7
20 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40321484
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 84

Expert Comment

by:Dave Baldwin
ID: 40321556
I installed Python 2.7.8 and it didn't make any difference.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40321772
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:beavoid
ID: 40321780
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 84

Expert Comment

by:Dave Baldwin
ID: 40321983
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 84

Expert Comment

by:Dave Baldwin
ID: 40322011
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
ID: 40322285
To be sure,

My About window on IDLE says

Python Version 3.3.3  

IDLE version 3.3.3
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40322358
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
ID: 40322391
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 84

Expert Comment

by:Dave Baldwin
ID: 40322408
0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 40322412
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
ID: 40324273
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 84

Expert Comment

by:Dave Baldwin
ID: 40324381
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
ID: 40324604
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 84

Expert Comment

by:Dave Baldwin
ID: 40324894
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
ID: 40331256
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 84

Expert Comment

by:Dave Baldwin
ID: 40331332
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 84

Expert Comment

by:Dave Baldwin
ID: 40331870
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
ID: 40333719
I am getting very promising results now using SocketServers here

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

Expert Comment

by:Dave Baldwin
ID: 40333752
You're welcome, glad to help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

770 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