cn-py-serv/server/file_read.py
Иван Солнцев eabd85f68b
Support keep alive
Improvements:
  - Response return Content-Length header;
  - HTTP handler run recursive after call;
  - File iterator when call before yield file size.
2024-10-08 13:54:15 +03:00

43 lines
1.3 KiB
Python

#
# CatNet Python Server
#
# Copyright (C) 2023-2024 John Solntsev
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import logging
import os
log = logging.getLogger(__name__)
def fileiter(filename):
log.debug("Calc file %s size" % filename)
yield os.path.getsize(filename)
log.debug("Open file %s to byte-read" % filename)
with open(filename, "rb") as f:
while True:
file_data = f.read(1024)
yield file_data
if len(file_data) >= 1024:
log.debug("File size greeter 1024 bytes, yield data. Continue to read")
else:
log.debug("File size lower 1024 bytes, yield data. Break to read")
break