Skip to content

Instantly share code, notes, and snippets.

@GoldraK
Created October 6, 2023 10:43
Show Gist options
  • Save GoldraK/8f5144ead3e988462dd480fb2b0aa64f to your computer and use it in GitHub Desktop.
Save GoldraK/8f5144ead3e988462dd480fb2b0aa64f to your computer and use it in GitHub Desktop.
import socket
def send_file(filename, host, port):
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
with open(filename, "rb") as file:
while True:
chunk = file.read(2048) # 2KB chunk size
if not chunk:
break
client_socket.send(chunk)
print("File sent successfully.")
client_socket.close()
if __name__ == "__main__":
host = "127.0.0.1" # Server IP address
port = 12345 # Port to connect to
filename = "file_to_send.txt" # Replace with the actual file path
send_file(filename, host, port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment