How can I send my file to another network?

Question:

I can’t find a way to send a file over to another network. I have a file that I want to send to a different computer which is not on the same network. Right now I’m using a server and a client side. Here is the server side: (I use tqdm to generate a little bar that shows the progress)

import socket
import tqdm
import os
  
SERVER_HOST = ""  #using public IP will generate an error, so I left it blank
SERVER_PORT = 5001
BUFFER_SIZE = 4096
SEPARATOR = "<SEPARATOR>"

s = socket.socket()
s.bind((SERVER_HOST, SERVER_PORT))
s.listen(10)
print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}")
print("Waiting for the client to connect... ")

client_socket, address = s.accept()
print(f"[+] {address} is connected.")

received = client_socket.recv(BUFFER_SIZE).decode()
filename, filesize = received.split(SEPARATOR)
filename = os.path.basename(filename)
filesize = int(filesize)
progress = tqdm.tqdm(range(filesize), f"Receiving {filename}", unit="B", unit_scale=True, unit_divisor=1024)

with open(filename, "wb") as f:
    while True:
        bytes_read = client_socket.recv(BUFFER_SIZE)
        if not bytes_read:
            break
        f.write(bytes_read)
        progress.update(len(bytes_read))

client_socket.close()
s.close()`

I tried to set my public ip as the SERVER_HOST, but it gave me an error: "The requested address is not valid in its context". However I removed public address and used my IPv4 on client side, which allowed me to transfer the file inside my local network (But I want it to work from everywhere). This is the client side:

import tqdm
import os

SEPARATOR = "<SEPARATOR>"
BUFFER_SIZE = 4096

s = socket.socket()
host = "192.168.0.17" 
port = 5001

print(f"[+] Connecting to {host}:{port}")
s.connect((host, port))
print("[+] Connected to ", host)
filename = "data.txt"

filesize = os.path.getsize(filename)
print(f"{filesize}")
s.send(f"{filename}{SEPARATOR}{filesize}".encode())

progress = tqdm.tqdm(range(filesize), f"Sending {filename}", unit="B", unit_scale=True, unit_divisor=1024)
with open(filename, "rb") as f:
    while True:
        bytes_read = f.read(BUFFER_SIZE)
        if not bytes_read:
            break
        s.sendall(bytes_read)
        progress.update(len(bytes_read))
s.close()

Any suggestion will be appreciated. Also is there a better way to transfer files that I could try?

Asked By: Volkov

||

Answers:

Answer: All I had to do was port forward and it worked. There is a lot of tutorials on this topic if you are interested. Thank you for your help!

Answered By: Volkov
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.