Skip to content

Commit f70d7b5

Browse files
committed
New version 1.1
1 parent e6022e1 commit f70d7b5

File tree

5 files changed

+215
-248
lines changed

5 files changed

+215
-248
lines changed

README.md

+167-149
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,167 @@
1-
# MicroPyServer
2-
3-
[MicroPyServer](https://github.com/troublegum/micropyserver) is a simple HTTP server for MicroPython projects.
4-
5-
## Install
6-
7-
Download a code and unpack it on your project folder.
8-
Use Thonny IDE or other IDE for upload your code in ESP8266/ESP32 board.
9-
10-
## Quick start
11-
12-
### Hello world example
13-
14-
```
15-
from micropyserver import MicroPyServer
16-
17-
def show_message(request):
18-
''' request handler '''
19-
server.send("HELLO WORLD!")
20-
21-
server = MicroPyServer()
22-
''' add route '''
23-
server.add_route("/", show_message)
24-
''' start server '''
25-
server.start()
26-
```
27-
28-
### Add some routes
29-
```
30-
from micropyserver import MicroPyServer
31-
32-
def show_index(request):
33-
''' main request handler '''
34-
server.send("THIS IS INDEX PAGE!")
35-
36-
def another_action(request):
37-
''' another action handler '''
38-
server.send("THIS IS ANOTHER ACTION!")
39-
40-
server = MicroPyServer()
41-
''' add routes '''
42-
server.add_route("/", show_index)
43-
server.add_route("/another_action", another_action)
44-
''' start server '''
45-
server.start()
46-
```
47-
48-
49-
### Send JSON response example
50-
51-
```
52-
from micropyserver import MicroPyServer
53-
import json
54-
55-
def return_json(request):
56-
''' request handler '''
57-
json_str = json.dumps({"param_one": 1, "param_two": 2})
58-
server.send(json_str, content_type="Content-Type: application/json")
59-
60-
server = MicroPyServer()
61-
''' add route '''
62-
server.add_route("/", return_json)
63-
''' start server '''
64-
server.start()
65-
```
66-
67-
### Access denied example
68-
```
69-
from micropyserver import MicroPyServer
70-
71-
def show_index(request):
72-
''' main request handler '''
73-
server.send("THIS IS INDEX PAGE!")
74-
75-
def on_request_handler(request, address):
76-
if str(address[0]) != '127.0.0.1':
77-
server.send('ACCESS DENIED!', 403)
78-
return False
79-
return True
80-
81-
82-
server = MicroPyServer()
83-
''' add route '''
84-
server.add_route("/", show_index)
85-
''' add request handler '''
86-
server.on_request(on_request_handler)
87-
''' start server '''
88-
server.start()
89-
```
90-
91-
### Turn ON / OFF a led example
92-
93-
You can remote control a led via internet.
94-
95-
![schema](https://habrastorage.org/webt/jb/xu/aj/jbxuaj0nr8fnqllbq27p_vfx3bw.png)
96-
97-
```
98-
import esp
99-
import network
100-
from micropyserver import MicroPyServer
101-
102-
wlan_id = "your wi-fi"
103-
wlan_pass = "your password"
104-
105-
wlan = network.WLAN(network.STA_IF)
106-
wlan.active(True)
107-
if not wlan.isconnected():
108-
wlan.connect(wlan_id, wlan_pass)
109-
110-
def do_on(request):
111-
''' on request handler '''
112-
pin.value(1)
113-
server.send("ON")
114-
115-
def do_off(request):
116-
''' off request handler '''
117-
pin.value(0)
118-
server.send("OFF")
119-
120-
pin = machine.Pin(13, machine.Pin.OUT)
121-
server = MicroPyServer()
122-
''' add routes '''
123-
server.add_route("/on", do_on)
124-
server.add_route("/off", do_off)
125-
''' start server '''
126-
server.start()
127-
```
128-
129-
130-
### More examples
131-
132-
More examples you can find in a folder "examples".
133-
134-
## MicroPyServer methods
135-
136-
Constructor - srv = MicroPyServer(host="0.0.0.0", port=80)
137-
138-
Start server - srv.start()
139-
140-
Add new route - srv.add_route(path, handler, method="GET")
141-
142-
Send response to client - srv.send(response, status=200, content_type="Content-Type: text/plain", extra_headers=[])
143-
144-
Send 404 to client - srv.not_found()
145-
146-
Send 500 to client - srv.internal_error(error)
147-
148-
149-
1+
# MicroPyServer
2+
3+
[MicroPyServer](https://github.com/troublegum/micropyserver) is a simple HTTP server for MicroPython projects.
4+
5+
**Important!** Version 1.1.x is not compatible with version 1.0.1 and older.
6+
7+
## Install
8+
9+
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.
11+
12+
## Quick start
13+
14+
15+
### Hello world example
16+
17+
```
18+
from micropyserver import MicroPyServer
19+
20+
def hello_world(request):
21+
''' request handler '''
22+
server.send("HELLO WORLD!")
23+
24+
server = MicroPyServer()
25+
''' add route '''
26+
server.add_route("/", hello_world)
27+
''' start server '''
28+
server.start()
29+
```
30+
31+
### Add some routes
32+
```
33+
from micropyserver import MicroPyServer
34+
35+
def show_index(request):
36+
''' main request handler '''
37+
server.send("THIS IS INDEX PAGE!")
38+
39+
def another_action(request):
40+
''' another action handler '''
41+
server.send("THIS IS ANOTHER ACTION!")
42+
43+
server = MicroPyServer()
44+
''' add routes '''
45+
server.add_route("/", show_index)
46+
server.add_route("/another_action", another_action)
47+
''' start server '''
48+
server.start()
49+
```
50+
51+
### Send JSON response example
52+
53+
```
54+
from micropyserver import MicroPyServer
55+
import json
56+
57+
def return_json(request):
58+
''' request handler '''
59+
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");
62+
server.send(json_str)
63+
64+
server = MicroPyServer()
65+
''' add route '''
66+
server.add_route("/", return_json)
67+
''' start server '''
68+
server.start()
69+
```
70+
71+
### Access denied example
72+
```
73+
from micropyserver import MicroPyServer
74+
75+
def show_index(request):
76+
''' main request handler '''
77+
server.send("THIS IS INDEX PAGE!")
78+
79+
def on_request_handler(request, address):
80+
if str(address[0]) != '127.0.0.1':
81+
server.send("HTTP/1.0 403\r\n\r\n");
82+
server.send('ACCESS DENIED!')
83+
return False
84+
return True
85+
86+
87+
server = MicroPyServer()
88+
''' add route '''
89+
server.add_route("/", show_index)
90+
''' add request handler '''
91+
server.on_request(on_request_handler)
92+
''' start server '''
93+
server.start()
94+
```
95+
96+
### Turn ON / OFF a LED example
97+
98+
You can remote control a led via internet.
99+
100+
![schema](https://habrastorage.org/webt/jb/xu/aj/jbxuaj0nr8fnqllbq27p_vfx3bw.png)
101+
102+
```
103+
import esp
104+
import network
105+
import machine
106+
import ubinascii
107+
from micropyserver import MicroPyServer
108+
109+
wlan_id = "your wi-fi"
110+
wlan_pass = "your password"
111+
112+
print("Start...")
113+
114+
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
115+
print("MAC: " + mac)
116+
117+
wlan = network.WLAN(network.STA_IF)
118+
wlan.active(True)
119+
120+
while not wlan.isconnected():
121+
wlan.connect(wlan_id, wlan_pass)
122+
print("Connected... IP: " + wlan.ifconfig()[0]);
123+
124+
def do_on(request):
125+
''' on request handler '''
126+
pin.value(1)
127+
server.send("ON")
128+
129+
def do_off(request):
130+
''' off request handler '''
131+
pin.value(0)
132+
server.send("OFF")
133+
134+
def do_index(request):
135+
''' index request handler '''
136+
server.send("SWITCH ON/OFF")
137+
138+
pin = machine.Pin(13, machine.Pin.OUT)
139+
server = MicroPyServer()
140+
''' add routes '''
141+
server.add_route("/", do_index)
142+
server.add_route("/on", do_on)
143+
server.add_route("/off", do_off)
144+
''' start server '''
145+
server.start()
146+
```
147+
148+
149+
## MicroPyServer methods
150+
151+
Constructor - srv = MicroPyServer(host="0.0.0.0", port=80)
152+
153+
Start server - srv.start()
154+
155+
Add new route - srv.add_route(path, handler, method="GET")
156+
157+
Send response to client - srv.send(response)
158+
159+
Return current request - srv.get_request()
160+
161+
### Event handlers
162+
163+
Set handler on every request - server.on_request(handler)
164+
165+
Set handler on 404 - server.on_not_found(handler)
166+
167+
Set handler on server error - server.on_error(handler)

examples/example1.py

-32
This file was deleted.

examples/example2.py

-31
This file was deleted.

examples/index.html

-6
This file was deleted.

0 commit comments

Comments
 (0)