How can i connect two computers with python socket?

Question:

im new here!
I have a problem with connection between two computers connected with different wi-fi’s. After about 20 seconds i get information that connection can’t be done.
There is my code:
SERVER:

from socket import *


lista = ['computer']

s = socket(AF_INET, SOCK_STREAM)

port = 21312
s.bind(('my ipv4', port))

s.listen(5)
while True:

for i in range (0, len(lista)):
    a = str(lista[i]).encode()
    c, addr = s.accept()
    print("CONNECTION WITH",addr)
    c.send(a)
    print(a)
    c.close()

CLIENT:

import socket
from socket import *

port = 21312

while True:
s = socket(AF_INET,SOCK_STREAM)
s.connect(('my ipv4', port))
odebrana = (s.recv(1024))
decoded = odebrana.decode()
print(decoded)
s.close()

Answers:

Likely you are experiencing an issue because your server sits behind a Network Address Translator (NAT). This way your client cannot use the server’s IP directly since it is not reachable. There are a few ways around it.

  1. The easiest and not very practical one is: get both machines in the same network, then it should work.
  2. Get a public IP address for the server. You can do that by hosting it on a cloud server that provides you with a public IP, e.g., aws, azure, google cloud etc.
  3. In the old days we used hamachi to get a VPN that would connect both machines. Then they can identify each other over that VPN. Simply turn on hamachi (or any other VPN solution), run your server, then from your client (connected to the VPN), use the VPN’s server IP (hamachi will provide you with one when you setup a network).

Disclaimer: I have not used hamachi in about 15 years, but just went through the process because of one of the comments below.

Seems like you can create an account, then once you turn it on you should see your v4 and v6 addresses as shown below:

Hamachi IP

Highlighted is my v4 address. I suspect you need to create a network, join both PCs in the same network and then use hamachi’s IP to emulate behaviour as if they were connected via LAN.

Answered By: Misho Janev

So I faced the similar problem while sending image files between 2 computers using python sockets. I solved the issue by following this way:

  • First I completed writing the connection code of both server.py and client.py

Note: server.py should be in one computer and client.py should be in another computer.

server.py

import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
print(host)
server.bind((host, 12000))
server.listen()

client_socket, client_address = server.accept()

file = open('server_image.jpg','wb')

image_chunk = client_socket.recv(2048)

while image_chunk:
    file.write(image_chunk)
    image_chunk = client_socket.recv(2048)

file.close()
client_socket.close()

client.py

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # AF_INET = IP, SOCK_STREAM = TCP

server_host = 'LAPTOP-1231'  # Replace this hostname with hostname printed in server.py

client.connect((server_host, 12000))  # 127.0.0.1

file = open('That_Sinking_Feeling_27.jpg', 'rb')
image_data = file.read(2048)

while image_data:
    client.send(image_data)
    image_data = file.read(2048)

file.close()
client.close()
  • Now you should add the image in the directory where client.py is located, so that you can send it to another computer (server). Rename it to img.jpg

  • Then, you need to run server.py in your another computer. It will print the hostname in terminal. Then copy that hostname and paste it in client.py (server_host = hostname_from_server)

  • Then run client.py

Finally the image will be transferred to new computer (server)

Answered By: Prakash Dahal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.