Skip to content

Commit 23f2a6f

Browse files
committed
can run with no wifi network and select default index pages
1 parent a4ab1a9 commit 23f2a6f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/gurgleapps_webserver.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class GurgleAppsWebserver:
2121

2222
def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www", log_level=0):
2323
print("GurgleApps.com Webserver")
24+
self.default_index_pages = [] # ["index.html", "index.htm"]
2425
self.ip_address = '1.1.1.1'
2526
self.port = port
2627
self.timeout = timeout
@@ -41,10 +42,11 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
4142
</body>
4243
</html>
4344
"""
44-
if self.connect_wifi(self.wifi_ssid, self.wifi_password):
45-
print('point your browser to http://', self.ip_address)
46-
else:
47-
raise RuntimeError('network connection failed')
45+
if self.wifi_ssid!=None:
46+
if self.connect_wifi(self.wifi_ssid, self.wifi_password):
47+
print('point your browser to http://', self.ip_address)
48+
else:
49+
raise RuntimeError('network connection failed')
4850
self.server_running = False
4951

5052

@@ -88,7 +90,7 @@ def start_access_point(self, ssid, password=None):
8890

8991
async def maintain_connection(self):
9092
while True:
91-
if self.wlan_sta.isconnected() == False:
93+
if self.wlan_sta.isconnected() == False and self.wifi_ssid != None:
9294
print("Lost connection to Wi-Fi. Attempting to reconnect...")
9395
self.connect_wifi(self.wifi_ssid, self.wifi_password)
9496
await asyncio.sleep(10)
@@ -116,6 +118,8 @@ async def main():
116118
)
117119
await main()
118120

121+
def set_default_index_pages(self, default_index_pages):
122+
self.default_index_pages = default_index_pages
119123

120124
# async def start_server(self):
121125
# print("start_server")
@@ -213,6 +217,13 @@ async def serve_request(self, reader, writer):
213217
return
214218
# perhaps it is a folder
215219
if self.dir_exists(file_path): #serve a folder
220+
for index_page in self.default_index_pages:
221+
index_file_path = file_path.rstrip("/") + "/" + index_page
222+
if self.log_level > 1:
223+
print("index_file_path: "+str(index_file_path))
224+
if self.file_exists(index_file_path):
225+
await response.send_file(index_file_path, content_type=self.get_content_type(index_file_path))
226+
return
216227
files_and_folders = self.list_files_and_folders(file_path)
217228
await response.send_iterator(self.generate_root_page_html(files_and_folders))
218229
return

0 commit comments

Comments
 (0)