Swapnil Das
Swapnil Das

Reputation: 1

How to transfer image files wirelessly between two PCs at different networks using sockets in python?

I have been trying to send files between two PCs using sockets.

Computer A(Sending end):

import socket
from PIL import Image

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.connect(('ip_address', 9999))

with Image.open('Image.jpg') as img:
    image_data = img.tobytes()

s.sendall(image_data)

print('Image sent successfully')

s.close()

Computer B(Receiving end):

import socket
server =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(('ip_address',9999))
server.listen()

client,addr=server.accept()

file_name=client.recv(1024).decode()
print(file_name)

file=open(file_name,"wb")
file_bytes=b""

done=False

while not done:
    data=client.recv(1024)
    if file_bytes[-5:]==b"<END>":
        done=True
    else:
        file_bytes+=data

file.write(file_bytes)

file.close()

server.close()
client.close()

Using this code,it is possible to transfer files if the PCs are connected on the same LAN.Can anyone suggest what modifications are needed to be made so that I can transfer files over different networks?

Upvotes: 0

Views: 11

Answers (0)

Related Questions