# Dockerfile | |||||
FROM python:3.9-slim | |||||
WORKDIR /app | |||||
COPY requirements.txt . | |||||
RUN pip install --no-cache-dir -r requirements.txt | |||||
COPY app.py . | |||||
CMD ["python", "app.py"] |
# Dockerfile | |||||
FROM python:3.9-slim | |||||
WORKDIR /app | |||||
COPY requirements.txt . | |||||
RUN pip install requests | |||||
COPY player.py . | |||||
CMD ["python", "player.py"] |
# app.py | |||||
from flask import Flask, request | |||||
from werkzeug.urls import url_quote_plus | |||||
app = Flask(__name__) | |||||
def fizzbuzz(num): | |||||
if num % 3 == 0 and num % 5 == 0: | |||||
return "fizzbuzz" | |||||
elif num % 3 == 0: | |||||
return "fizz" | |||||
elif num % 5 == 0: | |||||
return "buzz" | |||||
else: | |||||
return str(num) | |||||
@app.route('/fizzbuzz', methods=['POST']) | |||||
def compute_fizzbuzz(): | |||||
data = request.json | |||||
try: | |||||
number = int(data['number']) | |||||
return fizzbuzz(number) | |||||
except (KeyError, ValueError): | |||||
return "Invalid input: Please provide a valid number", 400 | |||||
if __name__ == '__main__': | |||||
app.run(host='0.0.0.0', port=8080) |
version: '3.8' | |||||
services: | |||||
fizzbuzz_server: | |||||
image: fizzbuzz_app | |||||
ports: | |||||
- "8080:8080" | |||||
networks: | |||||
- fizzbuzz_network | |||||
hostname: fizzbuzz_server | |||||
fizzbuzz_client: | |||||
image: player | |||||
depends_on: | |||||
- fizzbuzz_server | |||||
networks: | |||||
- fizzbuzz_network | |||||
networks: | |||||
fizzbuzz_network: | |||||
driver: bridge |
import requests | |||||
import json | |||||
# Define the server adress and port | |||||
#SERVER_ADDR = "http://172.18.0.2" | |||||
#SERVER_ADDR = "http://127.0.0.1" | |||||
#SERVER_ADDR = "host.docker.internal" | |||||
SERVER_ADDR = "http://fizzbuzz_server" | |||||
SERVER_PORT =8080 | |||||
# Define the URL for the FizzBuzz endpoint | |||||
URL = f"{SERVER_ADDR}:{SERVER_PORT}/fizzbuzz" | |||||
try: | |||||
for number in range(1,101): | |||||
# Define the payload data (JSON format) | |||||
payload = {'number': number} | |||||
# Send the HTTP POST request with JSON payload | |||||
response = requests.post(URL, json=payload) | |||||
# Print the response | |||||
print(f"Number: {number}, Response: {response.text}") | |||||
except requests.RequestException as e: | |||||
# Handle request exceptions | |||||
print("Error:", e) |
# requirements.txt | |||||
Flask==2.0.1 | |||||
Werkzeug==2.2.2 |