This commit is contained in:
darkicewolf50 2024-11-23 12:00:42 -07:00
parent 3cd3091608
commit d41b86cf46
4 changed files with 71 additions and 0 deletions

17
Docker Test/Dockerfile Normal file
View File

@ -0,0 +1,17 @@
# Use an official Python runtime as a parent image
FROM python:3.10-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY ./mock /app
# Install any necessary dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Expose port 8080 for the container to listen on
EXPOSE 8080
# Command to run the Python server when the container starts
CMD ["python", "server.py"]

View File

@ -0,0 +1,10 @@
services:
test-http-container:
container_name: test-http-container
ports:
- 8080:8080
volumes:
- ./mock:/app
build: .
networks:
- testnet

View File

View File

@ -0,0 +1,44 @@
import json
import signal
import sys
from http.server import BaseHTTPRequestHandler, HTTPServer
# Function to generate the response
def send_funct():
to_send = {
"body": "Hello this is a test"
}
return json.dumps(to_send)
# Define request handler
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
# Send response headers
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
# Send the response body (the JSON data)
self.wfile.write(send_funct().encode('utf-8'))
# Graceful shutdown handler
def signal_handler(sig, frame):
print("\nShutting down server gracefully...")
sys.exit(0)
# Set up and start the server
if __name__ == "__main__":
# Register signal handler for graceful shutdown (e.g., Ctrl+C)
signal.signal(signal.SIGINT, signal_handler)
# Set the server address (localhost) and port (8080)
server_address = ('', 8080)
httpd = HTTPServer(server_address, RequestHandler)
print("Server started on port 8080")
try:
# Start the server and listen for requests
httpd.serve_forever()
except KeyboardInterrupt:
# Server shutdown is handled in signal_handler
pass