Python Socket to connect over Global Public IP Address - python

I have been working on a project to connect computers located in different locations together through Python. Initially, while testing, I used my private IP address (I did not know it was private at the time) to connect computers on the same network as mine. But as soon as I tried doing this with computers located on different networks in different locations, it simply did not work.
And I assume this is because the program is using the local IP address of my computer that can connect only to computers on the same network. Here are my simplified programs:
Here is my server-side script:
server = socket.gethostbyname(socket.gethostname()) # 10.128.X.XXX which is the Internal IP
print(server)
port = 5555
clients = 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((server, port))
s.listen(2)
print("Waiting for connection...")
while True:
conn, addr = s.accept()
print("Connected to: ", addr)
conn.send(str.encode(f"{clients}"))
clients += 1
and here is my client side-script:
class Network:
def __init__(self):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = "10.128.0.2"
self.port = 5555
self.addr = (self.server, self.port)
self.id = int(self.connect())
def connect(self):
self.client.connect(self.addr)
return self.client.recv(2048).decode()
network = Network()
print(f"Connected as client {network.id}")
Now when I tried replacing the private IP address with the global IP address (as specified here: How do I get the external IP of a socket in Python?) I got the following error:
# Getting the Global IP Address
from requests import get
server = get("https://api.ipify.org").text
s.bind((server, port))
OSError: [WinError 10049] The requested address is not valid in its context
I have tried searching a lot on how to communicate (transfer small amounts of data as strings) between multiple computers located in different locations using different networks, but I haven't really gotten a solution. Is there a way that I can do this?

In server you always use local IP (it is IP of one of network cards in computer or 0.0.0.0 to use all network cards)
s.bind( (local_IP, port) )
# or
s.bind( ('0.0.0.0', port) )
In client you use external IP
s.connect( (external_IP, port) )
External client uses external IP to connect with your Internet Provider route and this router knows that this external IP is assigned to your computer and it redirects it your server.
At the same time local client can use local IP to connect with the same server.
external_client --> router(externa_IP) --> server(local_IP) <-- local_client

Related

WinError 10060 python connect python socket through router

I tried setting up a TCP socket with Python, and it works totally fine as long as I'm in the same network. But my problem is that I can't get it to work if one of the devices isn't in the same network.
I already tried setting the bind IP to '0.0.0.0', and if I try connecting I get socket error 10060. I think the problem probably has something to do with port-forwarding, but I don't know how to do it in Python.
Here is the code I used to test it, and I don't know how I get the public IP from my device in Python (not the gethostbyname() function, that only returns the private IP).
I hope you can help me.
Server.py
HOST = '0.0.0.0'
PORT = 5000
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
while True:
text = input("Enter text: ").encode("utf-8")
send_msg(conn,text)
echo = recv_msg(conn).decode("utf-8")
print(echo)
client.py
#HOST = '192.168.0.220'
HOST = 'x.x.x.x' #ip address of server
PORT = 5000`
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
while True:
data = recv_msg(s)
send_msg(s,data)
def send_msg(sock, msg):
msg = struct.pack('>Q', len(msg)) + msg
sock.sendall(msg)
def recv_msg(sock):
raw_msglen = recvall(sock, 8)
if not raw_msglen:
return None
msglen = struct.unpack('>Q', raw_msglen)[0]
return recvall(sock, msglen)
def recvall(sock: socket.socket, n):
data = bytearray()
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data.extend(packet)
return data
The code shown is fine. bind()'ing the server to 0.0.0.0 is typically the correct thing to do, that allows it to listen on all local IPs installed on its machine. Or, you can bind() it to the specific IP of the adapter that is connected to your network. Either way is fine.
On the client side, if the client is on the same network, it can connect() to the server's LAN IP, and all is well.
The problem comes when the client is not on the same network. That means the client simply cannot connect to your server directly. There is no physical route to facilitate that connection. So, to remedy that, you must setup a Port Forwarding rule in your LAN router. You must open a public IP/Port on the router's WAN, and map it to the server's LAN IP/Port. Then the client will be able to connect to the router's WAN IP/Port and let the router forward the packets to the server's LAN IP/Port, and vice versa.
If your router supports uPNP, your server code can setup this Port Forwarding programmably. There are uPNP libraries available, or your OS may even have APIs for that. Otherwise, your router admin must setup the Port Forwarding rule by hand.
To discover your router's WAN IP for the client to connect to, your server code can query an external site like https://whatismyip.com, or even query the router directly (if the router has an API for this purpose). Otherwise, you should subscribe to a 3rd party Dynamic DNS service that assigns you a static hostname that the client can always connect to, and then your server code, or even the router itself, can update the IP for that hostname whenever the router's WAN IP changes.

What all things do i have to change in my socket programs to make it work for computers on different networks

If i have simple socket program with server and client programs to send a message how do I modify it to make it work for different networks. (say my friend and i want to send hi from our pcs)
The error message is:
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
#Server
import socket
host = 'local host'
port = 5000
s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
s.bind(('', port))
s.listen(1)
c, addr = s.accept()
print("CONNECTION FROM:", str(addr))
c.send(b"Hi")
msg = "Bye.............."
c.send(msg.encode())
c.close()
#Client
import socket
host = 'local host'
port = 5000
s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
s.connect(('127.0.0.1', port))
msg = s.recv(1024)
while msg:
print('Recived:' + msg.decode())
msg = s.recv(1024)
s.close()
Set up port forwarding on your router. Suppose your local system is the server serving on port 5000 and your friend is the client.
Open an Command Prompt on the server and run ipconfig to get your local IP address. It is usually of the form 192.168.x.x but can vary. It should also be available in your router's status page.
On your router, configure port forwarding for internal IP = local IP, interal port = 5000, external port = 5000.
Your router should also have your external Internet address on its status page. You can also just google whatsmyip. Give that IP and the external port number (5000) to your friend. They use that to connect their client to your server.

Python problem with connecting socket to server

I have written a simple program to connect a client to a server in python.
When I run this on my local network with my local IP address, everything works fine.
I've gotten a google cloud server and a linux VM on it. I've uploaded server.py onto the server,
but when I run it, I cannot connect from my computer with the client code.
I've tried pinging the server from my computer and that works. I also looked at which ports were being
listened to on the server side. When I left the code as below, port 5555 was not listed.
When I replaced the server in server.py by server = "", then tested for which ports were being listened to,
port 5555 was being used, but not by the public IP address, instead by the local one.
Here is the code (I've removed everything irrelevant to establishing the network connection):
server.py:
import socket
from _thread import *
import pickle
server = "34.89.182.513"
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server, port))
except socket.error as e:
str(e)
s.listen(4)
print("Waiting for a connection, Server Started")
def threaded_client(conn, player_id):
pass # I've removed this code, since it is not relevant to question
while True:
conn, addr = s.accept()
print("Connected to:", addr)
start_new_thread(threaded_client, (conn, 0))
client.py:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect("34.89.182.513", 5555)
The google cloud VM lists two different IP addresses, one private and one public. I've been using the public
one. Does that have anything to do with the problem?

Python sockets: [Errno 99] when binding to ip on local network

I am trying to set up a UDP unicast between two linux-machines on my local network, using the python sockets library. I manage to send and receive the package using the following code:
Send
import socket
HOST = '192.168.1.194' # IP of remote machine
PORT = 47808
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto('Hello UDP', (HOST, PORT))
s.close()
Receive
import socket
HOST = ''
PORT = 47808
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((HOST, PORT))
while True:
try:
data, addr = s.recvfrom(1024)
print 'Received: {0} # {1}'.format(data, addr)
except KeyboardInterrupt:
break
s.close()
However, binding to '' makes the receiving code accept packets from any local interface. If I try to bind to the IP address of the sending machine specifically (changing HOST = ''to HOST = '192.168.1.130' in the receiving code), I get a socket.error: [Errno 99] Cannot assign requested address. No other services are using the port, and I have tried different ports with no change in behaviour. How can I configure my socket to only receive packets from a specific address?
First, let's deal with the error you are seeing. .bind() names the local end of the socket, not the remote. So the host part must refer to the local machine (e.g., 'localhost', '127.0.0.1, '192.168.1.194', or '' (wildcard for all local interfaces).) So, when you specify an address that isn't local to the machine running .bind(), you get an error.
Second, there is no way to "configure my socket to only receive packets from a specific address." As an alternative, you can use the returned address from .recvfrom() to ignore data you don't care about.
data, addr = s.recvfrom(1024)
if addr != '192.168.1.130':
continue

python TCP client can't connect to my TCP server

I can run two instances of my program and connect them to each other, but when someone else at a different ip trys to connect to my server their socket fails to connect. My code doesn't have input the ip of the users computer, but has my ip(as I am going to be the only person running the server) hard coded in.
Here is the server class connect function:
def connect(self,host,port):
self.host = host
self.port = port
self.unconnected_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.unconnected_socket.bind((self.host,self.port))
self.unconnected_socket.listen(5)
This is the when the program creates a game
self.server = server.Server()
self.server.connect(ip,port) #ip is my computer's ip address
self.serverThread = Thread(target=self.server.serve_forever)
self.serverThread.daemon = True
self.serverThread.start()
self.client.connect(ip,port)
Now here is the class client connect function:
def connect(self,host,port):
self.host = host
self.port = port
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.host,self.port))
When someone on a different computer runs the program but chooses to join the game:
self.client.connect(ip,port)
I am using a slightly modified Mastermind Networking Lib - 1.5.2 from pygames.
Blind guess: You are probably binding your server to the address 127.0.0.1 (or localhost). This address cannot be reached from outbound connections.
Instead, you should be setting the host of the bound address to socket.INADDR_ANY

Categories