How to connect a socket to another computer's socket through Internet
<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="http://i.stack.imgur.com/9CseJ.png" rel="nofollow"><img src="http://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>

<p>Computer B can't directly connect to computer A since it has an IP address which is not reachable from the outside.  You need to set up a port forwarding rule in the 101.81.83.169 router that redirects incoming connection requests for port 50007 to IP address 192.168.0.4.</p>

<p>However, since you say that you are seeking a solution without changing router configurations, you need something different.</p>

<p>In this case, you could setup an intermediate server running on the public Internet that both computers can then connect to and serves as an intermediate tunneling platform between them. Solutions for this already exist, for example have a look at <a href="https://ngrok.com/" rel="nofollow">ngrok</a>, which has Python bindings available.</p>

python sockets networking