feat: containerized everything

- UDP server will now bound to all interfaces
- Added instructions for docker setup
This commit is contained in:
minhtrannhat 2023-11-27 04:24:47 -05:00
parent a7eecc36de
commit fa544ac1fb
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
6 changed files with 101 additions and 3 deletions

17
Dockerfile.client Normal file
View File

@ -0,0 +1,17 @@
# Use the official Python image
FROM python:3.11
# Expose the specified port
EXPOSE 12000
# Create and set the working directory
WORKDIR /app
# Copy the local app files to the container
COPY ./src/myftp /app
RUN dd if=/dev/urandom of=/app/file_local.txt bs=1024 count=10
RUN dd if=/dev/urandom of=/app/image_local.png bs=1024 count=50
# Start your Python application
#CMD python client.py --debug 1

19
Dockerfile.server Normal file
View File

@ -0,0 +1,19 @@
# Use the official Python image
FROM python:3.11
# Expose the specified port
EXPOSE 12000
# Create and set the working directory
WORKDIR /app
# Copy the local app files to the container
COPY ./src/myftp /app
# Create files with random content in the /server directory
RUN mkdir /server
RUN dd if=/dev/urandom of=/server/file1.txt bs=1024 count=10
RUN dd if=/dev/urandom of=/server/image1.png bs=1024 count=50
# Start your Python application
#CMD python server.py --port_number 12000 --debug 1

View File

@ -12,6 +12,24 @@ You can run `python3 src/myftp/client.py` to start the client or `python3 src/my
### Server
By default, the server IP address or hostname or server name will be `127.0.0.1` (also known as `localhost`). The `--port_number` flag, if not specified will be by default `12000`.
By default, the server IP address or hostname or server name will be `0.0.0.0` (meaning it will bind to all interfaces). The `--port_number` flag, if not specified will be by default `12000`.
You can run `python3 src/myftp/server.py` to start the server or `python3 src/myftp/server.py --port_number <insert port number here> --debug 1` for debugging purposes and to specify the port number.
You can run `python3 src/myftp/server.py` to start the server or `python3 src/myftp/server.py --ip_addr <insert ip addr of the server> --port_number <insert port number here> --debug 1` for debugging purposes and to specify the port number.
## Testing with Docker
### Dependencies
`docker` and `docker-compose`
### Setup
- Make you are at the root of this repo.
- Build the system with `docker-compose up --build --remove-orphans`.
- Wait 10 seconds.
- 2 containers will be created on the same network `mynetwork`. Their respective IP addresses will be printed to stdout.
- Open two terminal windows: one for each of server and client.
- Run the server with `docker exec -it project-ftp_server_1 python server.py <insert any flags here>`.
- Run the client with `docker exec -it project-ftp_client_1 python client.py <insert any flags here>`.
- For the client, when asked to put in the ip address and port number of the server, you can put in `ftp_server 12000` or adjust to your chosen port number. The IP address is resolved by Docker so ftp_server can not be changed.
- Tear down everything with `docker-compose down`.

35
docker-compose.yaml Normal file
View File

@ -0,0 +1,35 @@
version: "3"
services:
ftp_server:
build:
context: .
dockerfile: Dockerfile.server
networks:
- mynetwork
command:
- sh
- -c
- >
sleep 10 &&
echo "My IP: $(hostname -i)" &&
tail -f /dev/null
image: ftp_server:latest
ftp_client:
build:
context: .
dockerfile: Dockerfile.client
networks:
- mynetwork
command:
- sh
- -c
- >
sleep 10 &&
echo "My IP: $(hostname -i)" &&
tail -f /dev/null
image: ftp_client:latest
networks:
mynetwork:

View File

@ -38,6 +38,7 @@ class UDPClient:
f"myftp> - {self.mode} - : Invalid command. Supported commands are put, get, summary, change and help"
)
# handling the "bye" command
if command == "bye":
client_socket.close()
print(f"myftp> - {self.mode} - Session is terminated")

View File

@ -59,6 +59,14 @@ def init():
help="Port number for the server. Default = 12000",
)
parser.add_argument(
"--ip_addr",
default="0.0.0.0",
required=False,
type=str,
help="Port number for the server. Default = 12000",
)
parser.add_argument(
"--debug",
type=int,
@ -76,7 +84,7 @@ def init():
# UDP client selected here
if protocol_selection == "2":
udp_server = UDPServer("127.0.0.1", args.port_number, args.debug)
udp_server = UDPServer(args.ip_addr, args.port_number, args.debug)
udp_server.run()