title: How to connect a socket to another computer's socket through Internet
body: <p>I recently have some difficulties to connect a socket to another computer's socket through Internet, an image is worth a thousand words:</p>
  
  <p><a href="https://i.stack.imgur.com/9CseJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9CseJ.png" alt="enter image description here"></a></p>
  
  <p>Computer <strong>A</strong> is running this "<strong>listener.py</strong>" script:</p>
  
  <pre><code>import socket
  PORT = 50007              
  BUFFER = 2048              
  HOST = ''  
  
  if __name__ == '__main__':
      with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
          s.bind((HOST, PORT))
          s.listen(1)
          conn, addr = s.accept()
          with conn:
              print('Connected by', addr)
              while True:
                  data = conn.recv(BUFFER)
                  if not data: break
                  conn.sendall(data)
  </code></pre>
  
  <p>Computer <strong>B</strong> is running this "<strong>sender.py</strong>" script:</p>
  
  <pre><code>import socket
  HOST = '101.81.83.169'    # The remote host
  PORT = 50007              # The same port as used by the server
  
  
  if __name__ == '__main__':
      with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
          s.connect((HOST, PORT))
          s.sendall(b'Hello, world')
  </code></pre>
  
  <p>So first of all, I run the "<strong>listener</strong>" script of the computer <strong>A</strong>. Then, I run the "<strong>sender</strong>" script of the computer B. However, when I execute the "<strong>sender</strong>" script, I received a <strong>error</strong> message which explains me that I am not authorized to connect to this remote address. </p>
  
  <p>So I would like to know how can I connect a socket to another socket through internet without changing the router configurations.</p>
  
  <p>Thank you very much for your help.</p>
  
  <p><strong>Edit</strong>: Here the error message (I didn't execute the same script for some reasons, but it's the same error message)</p>
  
  <pre><code>sock.connect(('101.81.83.169',50007)) Traceback (most recent call last):   File "&lt;stdin&gt;", line 1, in
   &lt;module&gt;   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in
  meth     return getattr(self._sock,name)(*args) socket.error: [Errno 61] Connection refused
  </code></pre>