Skip to content

Commit 5a57eab

Browse files
committed
few tweaks to make responses easier
1 parent ecbe100 commit 5a57eab

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

gurgleapps_webserver.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import network
2-
import socket
3-
import time
42
import re
5-
import config
3+
import time
64
import uos
75
import uasyncio as asyncio
8-
import _thread
96
import ujson as json
107
from response import Response
118
from request import Request
@@ -119,17 +116,10 @@ async def serve_request(self, reader, writer):
119116
if file:
120117
print("file found so serving it")
121118
print(file)
122-
writer.write('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
123-
writer.write(file)
124-
await writer.drain()
125-
await writer.wait_closed()
119+
await response.send(file)
126120
return
127121
print("file not found")
128-
response = self.html % "page not found "+url
129-
writer.write('HTTP/1.0 404 Not Found\r\nContent-type: text/html\r\n\r\n')
130-
writer.write(response)
131-
await writer.drain()
132-
await writer.wait_closed()
122+
await response.send(self.html % "page not found "+url, status_code=404)
133123
if (url == "/shutdown"):
134124
self.serving = False
135125
except OSError as e:

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ async def example_func(request, response, param1, param2):
1414
print("param1: " + param1)
1515
print("param2: " + param2)
1616
response_string = json.dumps({ "param1": param1, "param2": param2, "post_data": request.post_data})
17-
await response.send("200 OK", "application/json", response_string)
17+
await response.send_json(response_string, 200)
1818

1919

2020

2121
async def send_status(request, response):
2222
# send boolean status and number frequency
2323
response_string = json.dumps({"status": status, "delay": delay})
24-
await response.send("200 OK", "application/json", response_string)
24+
await response.send_json(response_string, 200)
2525

2626

2727
async def set_delay(request, response, new_delay):

response.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ class Response:
44
def __init__(self, writer):
55
self.writer = writer
66

7-
async def send(self, status, content_type, content):
8-
self.writer.write(f'HTTP/1.0 {status}\r\nContent-type: {content_type}\r\n\r\n')
9-
self.writer.write(content)
7+
async def send(self, body, status_code = 200, content_type = "text/html"):
8+
self.writer.write(f'HTTP/1.0 {status_code}\r\nContent-type: {content_type}\r\n\r\n')
9+
self.writer.write(body)
1010
await self.writer.drain()
11-
await self.writer.wait_closed()
11+
await self.writer.wait_closed()
12+
13+
async def send_html(self, body, status_code=200):
14+
await self.send(body, status_code, content_type='text/html')
15+
16+
async def send_json(self, body, status_code=200):
17+
await self.send(body, status_code, content_type='application/json')

0 commit comments

Comments
 (0)