I am using Python 3.8 on a Linux 5.4 machine. I have a Python Flask server with an endpoint that returns the output of the 'docker-compose logs' command. In a browser, the connection stays open and the page is updated automatically when new log output is generated by docker-compose. I want to do the same thing with requests from the terminal. I've tried the streaming example in this post: Python Requests Stream Data from API. However, nothing is ever printed and the function call appears to hang.
How can I view the 'live output' from the docker-compose logs command using Python 3 Requests as a client for the Flask endpoint? Thanks.
Requests Stream:
>>> def stream():
s = requests.Session()
r = s.get(url, headers=headers, stream=True)
print(r.status_code)
for line in r.iter_lines():
if line:
print(line)
Endpoint
# Define the function that will yield from the docker logs command
def getLogs():
try:
cmd = 'cd /persistent-storage/docker-compose && docker-compose logs --tail=10 -t -f --no-color'
app.config['LOGGER'].info(f"Running command: {cmd} ...")
proc = subprocess.Popen(
cmd,
shell=True,
universal_newlines=True,
stdout=subprocess.PIPE
)
# Read the standard output and yield it to the logger
for line in iter(proc.stdout.readline,''):
time.sleep(0.005)
yield line.rstrip() + '<br/>\n'
except Exception as err:
app.config['LOGGER'].error(f"Could not get docker-compose logs: {err} ...")
# Return a 'live feed' to the browser of the docker-compose logs
return Response(getLogs(), mimetype='text/html')
from Python 3 requests and live output from Flask Response()
No comments:
Post a Comment