Skip to content

Commit 2dc0a75

Browse files
committed
parent folder
1 parent b6b39a3 commit 2dc0a75

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

gurgleapps_webserver.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
7474
async def start_server(self):
7575
print("start_server")
7676
server_task = asyncio.create_task(asyncio.start_server(
77-
self.serve_request, "0.0.0.0", 80))
77+
self.serve_request, "0.0.0.0", self.port))
7878
await server_task
7979

8080
# async def start_server(self):
@@ -147,20 +147,22 @@ async def serve_request(self, reader, writer):
147147
if self.log_level > 0:
148148
print("file_path: "+str(file_path))
149149
# if uos.stat(file_path)[6] > 0:
150-
if self.file_exists(file_path):
150+
if self.file_exists(file_path): #serve a file
151151
content_type = self.get_content_type(url)
152152
if self.log_level > 1:
153153
print("content_type: "+str(content_type))
154154
await response.send_file(file_path, content_type=content_type)
155155
return
156-
if url == "/":
157-
print("root")
158-
files_and_folders = self.list_files_and_folders(self.doc_root)
156+
# perhaps it is a folder
157+
if self.dir_exists(file_path): #serve a folder
158+
files_and_folders = self.list_files_and_folders(file_path)
159159
await response.send_iterator(self.generate_root_page_html(files_and_folders))
160160
return
161-
html = self.generate_root_page_html(files_and_folders)
162-
await response.send(html)
163-
return
161+
# if url == "/":
162+
# print("root")
163+
# files_and_folders = self.list_files_and_folders(self.doc_root)
164+
# await response.send_iterator(self.generate_root_page_html(files_and_folders))
165+
# return
164166
print("file not found "+url)
165167
await response.send(self.html % "page not found "+url, status_code=404)
166168
if (url == "/shutdown"):
@@ -307,6 +309,8 @@ def blink_element(element, pin, duration=0.27):
307309
def list_files_and_folders(self, path):
308310
entries = uos.ilistdir(path)
309311
files_and_folders = []
312+
if path != self.doc_root:
313+
files_and_folders.append({"name": "..", "type": "directory"})
310314
for entry in entries:
311315
name = entry[0]
312316
mode = entry[1]

response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ async def send_file(self, file_path, status_code=200, content_type="text/html"):
3030
await self.send_headers(status_code, content_type, content_length=file_size)
3131

3232
#chunk_size = 1024 # Adjust the chunk size as needed
33-
chunk_size = 512 # Adjust the chunk size as needed
33+
#chunk_size = 512 # Adjust the chunk size as needed
34+
chunk_size = 256
3435
with open(file_path, "rb") as f:
3536
while True:
3637
chunk = f.read(chunk_size)

0 commit comments

Comments
 (0)