github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/examples/dockercoins/rng/rng.py (about)

     1  from flask import Flask, Response
     2  import os
     3  import socket
     4  import time
     5  
     6  app = Flask(__name__)
     7  
     8  # Enable debugging if the DEBUG environment variable is set and starts with Y
     9  app.debug = os.environ.get("DEBUG", "").lower().startswith('y')
    10  
    11  hostname = socket.gethostname()
    12  
    13  urandom = os.open("/dev/urandom", os.O_RDONLY)
    14  
    15  
    16  @app.route("/")
    17  def index():
    18      return "RNG running on {}\n".format(hostname)
    19  
    20  
    21  @app.route("/<int:how_many_bytes>")
    22  def rng(how_many_bytes):
    23      # Simulate a little bit of delay
    24      time.sleep(0.1)
    25      return Response(
    26          os.read(urandom, how_many_bytes),
    27          content_type="application/octet-stream")
    28  
    29  
    30  if __name__ == "__main__":
    31      app.run(host="0.0.0.0", port=80)
    32