Skip to content

Commit ea2653c

Browse files
authored
Merge pull request #5 from troublegum/dev
Release 1.2
2 parents 987146e + 55c6946 commit ea2653c

File tree

2 files changed

+131
-8
lines changed

2 files changed

+131
-8
lines changed

README.md

+31-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
## Install
88

99
Download a code and unpack it into your project folder.
10-
Use Thonny IDE or other IDE to load your code in ESP8266/ESP32 board.
10+
Use Thonny IDE or other IDE for upload your code in ESP8266/ESP32 board.
1111

1212
## Quick start
1313

@@ -57,8 +57,8 @@ import json
5757
def return_json(request):
5858
''' request handler '''
5959
json_str = json.dumps({"param_one": 1, "param_two": 2})
60-
server.send("HTTP/1.0 200 OK\r\n");
61-
server.send("Content-Type: application/json\r\n\r\n");
60+
server.send("HTTP/1.0 200 OK\r\n")
61+
server.send("Content-Type: application/json\r\n\r\n")
6262
server.send(json_str)
6363
6464
server = MicroPyServer()
@@ -78,7 +78,7 @@ def show_index(request):
7878
7979
def on_request_handler(request, address):
8080
if str(address[0]) != '127.0.0.1':
81-
server.send("HTTP/1.0 403\r\n\r\n");
81+
server.send("HTTP/1.0 403\r\n\r\n")
8282
server.send('ACCESS DENIED!')
8383
return False
8484
return True
@@ -95,7 +95,7 @@ server.start()
9595

9696
### Turn ON / OFF a LED example
9797

98-
You can remote control a led via internet.
98+
You can remote control a LED via internet.
9999

100100
![schema](https://habrastorage.org/webt/jb/xu/aj/jbxuaj0nr8fnqllbq27p_vfx3bw.png)
101101

@@ -119,7 +119,7 @@ wlan.active(True)
119119
120120
while not wlan.isconnected():
121121
wlan.connect(wlan_id, wlan_pass)
122-
print("Connected... IP: " + wlan.ifconfig()[0]);
122+
print("Connected... IP: " + wlan.ifconfig()[0])
123123
124124
def do_on(request):
125125
''' on request handler '''
@@ -145,6 +145,27 @@ server.add_route("/off", do_off)
145145
server.start()
146146
```
147147

148+
### Use utils for create response
149+
```
150+
from micropyserver import MicroPyServer
151+
import utils
152+
153+
def hello_world(request):
154+
''' request handler '''
155+
utils.send_response(server, "HELLO WORLD!")
156+
157+
def not_found(request):
158+
''' request handler '''
159+
utils.send_response(server, "404", 404)
160+
161+
server = MicroPyServer()
162+
''' add routes '''
163+
server.add_route("/", hello_world)
164+
server.add_route("/404", not_found)
165+
''' start server '''
166+
server.start()
167+
```
168+
148169

149170
## MicroPyServer methods
150171

@@ -158,10 +179,12 @@ Send response to client - srv.send(response)
158179

159180
Return current request - srv.get_request()
160181

161-
### Event handlers
162-
163182
Set handler on every request - server.on_request(handler)
164183

165184
Set handler on 404 - server.on_not_found(handler)
166185

167186
Set handler on server error - server.on_error(handler)
187+
188+
## Utils methods
189+
190+
Send response to client - utils.send_response(server, response, http_code=200, content_type="text/html", extend_headers=None)

utils.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
MicroPyServer is a simple HTTP server for MicroPython projects.
3+
4+
@see https://github.com/troublegum/micropyserver
5+
6+
The MIT License
7+
8+
Copyright (c) 2019 troublegum. https://github.com/troublegum/micropyserver
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in
18+
all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
THE SOFTWARE.
27+
"""
28+
29+
""" HTTP response codes """
30+
HTTP_CODES = {
31+
100: 'Continue',
32+
101: 'Switching protocols',
33+
102: 'Processing',
34+
200: 'Ok',
35+
201: 'Created',
36+
202: 'Accepted',
37+
203: 'Non authoritative information',
38+
204: 'No content',
39+
205: 'Reset content',
40+
206: 'Partial content',
41+
207: 'Multi status',
42+
208: 'Already reported',
43+
226: 'Im used',
44+
300: 'Multiple choices',
45+
301: 'Moved permanently',
46+
302: 'Found',
47+
303: 'See other',
48+
304: 'Not modified',
49+
305: 'Use proxy',
50+
307: 'Temporary redirect',
51+
308: 'Permanent redirect',
52+
400: 'Bad request',
53+
401: 'Unauthorized',
54+
402: 'Payment required',
55+
403: 'Forbidden',
56+
404: 'Not found',
57+
405: 'Method not allowed',
58+
406: 'Not acceptable',
59+
407: 'Proxy authentication required',
60+
408: 'Request timeout',
61+
409: 'Conflict',
62+
410: 'Gone',
63+
411: 'Length required',
64+
412: 'Precondition failed',
65+
413: 'Request entity too large',
66+
414: 'Request uri too long',
67+
415: 'Unsupported media type',
68+
416: 'Request range not satisfiable',
69+
417: 'Expectation failed',
70+
418: 'I am a teapot',
71+
422: 'Unprocessable entity',
72+
423: 'Locked',
73+
424: 'Failed dependency',
74+
426: 'Upgrade required',
75+
428: 'Precondition required',
76+
429: 'Too many requests',
77+
431: 'Request header fields too large',
78+
500: 'Internal server error',
79+
501: 'Not implemented',
80+
502: 'Bad gateway',
81+
503: 'Service unavailable',
82+
504: 'Gateway timeout',
83+
505: 'Http version not supported',
84+
506: 'Variant also negotiates',
85+
507: 'Insufficient storage',
86+
508: 'Loop detected',
87+
510: 'Not extended',
88+
511: 'Network authentication required',
89+
}
90+
91+
92+
def send_response(server, response, http_code=200, content_type="text/html", extend_headers=None):
93+
""" send response """
94+
server.send("HTTP/1.0 " + str(http_code) + " " + HTTP_CODES.get(http_code) + "\r\n")
95+
server.send("Content type:" + content_type + "\r\n")
96+
if extend_headers is not None:
97+
for header in extend_headers:
98+
server.send(header + "\r\n")
99+
server.send("\r\n")
100+
server.send(response)

0 commit comments

Comments
 (0)