Skip to content

Instantly share code, notes, and snippets.

@bedilbek
Last active May 16, 2024 09:00
Show Gist options
  • Save bedilbek/ed7f019a5377a1b57f8754ffd484de2c to your computer and use it in GitHub Desktop.
Save bedilbek/ed7f019a5377a1b57f8754ffd484de2c to your computer and use it in GitHub Desktop.
Python Server for Time Retrieval of server
FROM python:3.12-alpine
EXPOSE 8123
WORKDIR /app
RUN <<EOF cat >> /app/http_time_server.py
import http.server
import socketserver
import time
# Define the HTTP request handler
class TimeRequestHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
# Set the response status code
self.send_response(200)
# Set the response headers
self.send_header('Content-type', 'text/plain; charset=utf-8')
self.end_headers()
# Get the current time
current_time = time.time()
# Send the current time as the response body
self.wfile.write(str(current_time).encode())
def main():
# Specify the server host and port
server_host = '0.0.0.0'
server_port = 8123
# Create an HTTP server with the specified host and port
with socketserver.TCPServer((server_host, server_port), TimeRequestHandler) as server:
print(f"HTTP server is running on http://{server_host}:{server_port}")
server.serve_forever()
if __name__ == "__main__":
main()
EOF
CMD ["python", "http_time_server.py"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment