title: python socket connection refused on Mac but not on Windows
body: <p>This is not a duplicate of <a href="https://stackoverflow.com/questions/12394950/connection-refused">connection refused</a>. This questions asks about the difference between <code>MacOS</code> and <code>Windows</code>. In addition the firewall on my <code>Mac</code> is off.</p>
  
  <p>I'm trying to communicate with a device through a socket. The code is written in <code>python</code>. For some reason if I run my code on <code>Windows</code> everything works, however if I do it on a <code>Mac</code> it doesn't. A colleague of mine wrote the same code in <code>C</code> and ran it on a <code>Mac</code> and had no issues. Does anybody know what could be the issue?</p>
  
  <p>I can provide more details if needed. Just let me know which ones.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is the code</p>
  
  <pre><code>class Device(object):
      def __init__(self, ip_addr, port):
          self.ip_addr = ip_addr
          self.port = port
  
      def say_hi(self):
          msg = [254, 49, 0]
          ret = self.send_msg(msg)
          msg = [254, 29]
          return self.send_msg(msg)
  
      def say_bye(self):
          msg = [254, 49, 0]
          ret = self.send_msg(msg)
          msg = [254, 30]
          return self.send_msg(msg)
  
      def get_status(self):
          msg = [254, 49, 0]
          ret = self.send_msg(msg)
          msg = [254, 16]
          return self.send_msg(msg)
  
      def send_msg(self, msg):
          msg = ''.join(chr(i) for i in msg)
          s = socket.create_connection((self.ip_addr , self.port))
          s.sendall(msg)
          ret = s.recv(1)
          s.close()
          return ord(ret)
  </code></pre>
  
  <p>Here is the error that I'm getting:</p>
  
  <p><code>[Errno 61] Connection refused</code></p>
  
  <p>The error occurs at line: </p>
  
  <p><code>s = socket.create_connection((self.ip_addr , self.port))</code></p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p><code>ip_addr='172.24.176.46', port='2101'</code></p>
  
  <p>I'm able to access the address. Here are results from <code>ping</code></p>
  
  <pre><code>$ ping 172.24.176.46    
  PING 172.24.176.46 (172.24.176.46): 56 data bytes
  64 bytes from 172.24.176.46: icmp_seq=0 ttl=64 time=0.495 ms
  64 bytes from 172.24.176.46: icmp_seq=1 ttl=64 time=0.398 ms
  64 bytes from 172.24.176.46: icmp_seq=2 ttl=64 time=0.452 ms
  ^C
  </code></pre>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>I found a code that works and it works on Windows as well as on a Mac. I will provide it below, but what essentially I do is that I create a connection during class initialization (i.e. in <code>__init__</code>) and <strong>don't</strong> close a connection in <code>send_msg</code>. Here is my question: is it safe and proper to do it this way? It doesn't seem like a good approach not to close a connection. I would appreciate any help with this. My new code is below.</p>
  
  <pre><code>class Device(object):
      def __init__(self, ip_addr, port):
          self.ip_addr = ip_addr
          self.port = port
          self.socket = socket.create_connection((self.ip_addr , self.port))
  
      def say_hi(self):
          msg = [254, 49, 0]
          ret = self.send_msg(msg)
          msg = [254, 29]
          return self.send_msg(msg)
  
      def say_bye(self):
          msg = [254, 49, 0]
          ret = self.send_msg(msg)
          msg = [254, 30]
          return self.send_msg(msg)
  
      def get_status(self):
          msg = [254, 49, 0]
          ret = self.send_msg(msg)
          msg = [254, 16]
          return self.send_msg(msg)
  
      def send_msg(self, msg):
          msg = ''.join(chr(i) for i in msg)
          self.socket.sendall(msg)
          ret = self.socket.recv(1)
          return ord(ret)
  </code></pre>