Python: sending data between two computers via sockets

I am working on a script that will transfer data between two separate computers with Internet access. I am using the standard python socket module. It works great when I run both the client and the server on the same computer, but I can not get everything to work when they run on different computers.

Here is part of my server code:

import socket, time,os, random class Server(): def __init__(self,Adress=('',5000),MaxClient=1): self.s = socket.socket() self.s.bind(Adress) self.s.listen(MaxClient) def WaitForConnection(self): self.Client, self.Adr=(self.s.accept()) print('Got a connection from: '+str(self.Client)+'.') s = Server() s.WaitForConnection() 

And here is part of my client code:

 import socket class Client(): def __init__(self,Adress=("Here is the IP of the computer on which the \ server scrip is running",5000)): self.s = socket.socket() self.s.connect(Adress) c = Client() 

When I run these scripts on two different computers with Internet access, the client cannot connect and causes an error, and the server waits for connections forever.

What am I doing wrong?

+8
python sockets tcp connection
source share
1 answer

This is probably not related to your code, which looks fine. I believe this is a problem with the IP addresses you are using.

If the computers are on different networks, you need to make sure that the IP address you are transmitting is the only one available on the network. This basically means that if the IP address you are using starts with 192.168.? then you are using the wrong IP address.

You can easily verify this by running the command:
(windows): ipconfig
(linux): ifconfig

If you use the correct IP address, I would check the settings of my router and / or the firewall settings, which can very strongly block the port number that you are trying to use.

+7
source share

All Articles