Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
1 change: 1 addition & 0 deletions socket_programming/file_transfer/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
33 changes: 33 additions & 0 deletions socket_programming/file_transfer/receiver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import socket


def start_ftp_server(host, port) -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file socket_programming/file_transfer/receiver.py, please provide doctest for the function start_ftp_server

Please provide type hint for the parameter: host

Please provide type hint for the parameter: port

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file socket_programming/file_transfer/receiver.py, please provide doctest for the function start_ftp_server

Please provide type hint for the parameter: host

Please provide type hint for the parameter: port

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file socket_programming/file_transfer/receiver.py, please provide doctest for the function start_ftp_server

Please provide type hint for the parameter: host

Please provide type hint for the parameter: port

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen(1)

print(f"Server listening on {host}:{port}...")

while True:
client_socket, client_address = server_socket.accept()
print(f"Connection from {client_address}")

file_name = client_socket.recv(1024).decode()
print(f"Receiving file: {file_name}")

with open(file_name, "wb") as file:
while True:
data = client_socket.recv(1024)
if not data:
break
file.write(data)

print(f"File {file_name} received successfully.")
client_socket.close()


if __name__ == "__main__":
host = "127.0.0.1"
port = 12345

start_ftp_server(host, port)
28 changes: 28 additions & 0 deletions socket_programming/file_transfer/sender.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import socket


def send_file_to_server(host, port, file_path) -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file socket_programming/file_transfer/sender.py, please provide doctest for the function send_file_to_server

Please provide type hint for the parameter: host

Please provide type hint for the parameter: port

Please provide type hint for the parameter: file_path

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file socket_programming/file_transfer/sender.py, please provide doctest for the function send_file_to_server

Please provide type hint for the parameter: host

Please provide type hint for the parameter: port

Please provide type hint for the parameter: file_path

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))

file_name = file_path.split("/")[-1]
client_socket.send(file_name.encode())

with open(file_path, "rb") as file:
while True:
data = file.read(1024)
if not data:
break
client_socket.send(data)

print(f"File {file_name} sent successfully.")
client_socket.close()


if __name__ == "__main__":
host = "127.0.0.1"
port = 12345

file_path = "file.txt"

send_file_to_server(host, port, file_path)
Loading