Socket with Python

Viewed 68

I read some of the Sockets documentation and watched some videos on the subject, I tried to make the sockets connection. First, I tried it with my internal IP, ran it on different endpoints and everything worked. I tried it with my external IP and it failed, I tried to run the same application on different devices, I ran server.py on my PC, and I ran client.py on my cell phone. No error message appeared, but the connection did not start.

Server code:

import socket

s = socket.socket() 
print("Socket successfully created")

port = 12345
s.bind(('Meu IP', port))
print("socket binded to %s" %(port))

s.listen(5)
print ("socket is listening")

while True:

     c, addr = s.accept()
     print('Got connection from', addr )
     c.send('Thank you for connecting'.encode())

     c.close()

Client code:

# Import socket module
import socket

s = socket.socket()

port = 12345

s.connect(('IP', port))

print (s.recv(1024).decode())
s.close()

I think the problem is the IP, I think I should put the valid external IP instead of the internal IP, but I don't know how to do that, because when I try this, an error appears. I just need to know how to create a socket between two machines on different networks. I have been looking for a solution to this problem for a long time. If any of you can help me, I appreciate it.

0 Answers
Related