Skip to content

Commit 4e4e5a7

Browse files
author
funnygeeker
committed
2.1.0 update
1 parent 179eaf4 commit 4e4e5a7

13 files changed

Lines changed: 1726 additions & 337 deletions

README.ZH-CN.md

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
### 特点
88
- 尽可能模仿了 `Flask` 框架的风格
9-
- 集成了常用的:GET 请求解析,表单解析,HTML渲染,文件发送等功能
9+
- 集成了常用的:GET 请求参数解析,表单解析,HTML渲染,文件发送,Cookies设置,Cookies获取,动态路由 等常用功能
1010

1111
### 使用说明
1212
- 本项目一共有三个版本的文件,请根据实际需要进行选择:
@@ -21,8 +21,18 @@
2121

2222

2323
### 示例代码
24+
- 这里我们使用 [micropython-easynetwork](https://github.com/funnygeeker/micropython-easynetwork) 作为示例,连接局域网(也可以工作在AP模式,使用其他设备连接开发板)
2425
```python
25-
from libs.easyweb import EasyWeb
26+
import time
27+
from libs.easynetwork import Client
28+
from libs.easyweb import EasyWeb, render_template, send_file, make_response
29+
30+
client = Client()
31+
client.connect("ssid", "password") # 或者 client.connect("ssid", "")
32+
33+
while not client.isconnected():
34+
pass
35+
print('IP Address: {}'.format(client.ifconfig()[0]))
2636

2737
ew = EasyWeb() # 初始化 EasyWeb
2838

@@ -37,13 +47,23 @@ def home(request):
3747
print('Json: ', request.json)
3848
print('Path: ', request.path)
3949
print('Header: ', request.headers)
50+
print('Cookies: ', request.cookies)
4051
print('Full_Path: ', request.full_path)
41-
return ew.render_template("/web/wifi.html")
52+
return render_template("/web/wifi.html")
4253

4354
# 发送文件
4455
@ew.route('/easyweb.png')
4556
def img(request):
46-
return ew.send_file("/web/EasyWeb_256px.png")
57+
# 访问网页的 /easyweb.png 试试?
58+
return send_file("/web/EasyWeb_256px.png")
59+
60+
# 下载文件
61+
@ew.route('/download')
62+
def download(request):
63+
# 访问网页的 /easyweb.png 试试?
64+
# as_attachment: 是否作为附件发送文件,作为附件时会被下载
65+
# attachment_filename: 下载文件时向用户显示的文件名。如果未提供,将使用原始文件名
66+
return send_file("/web/EasyWeb_256px.png", as_attachment=True, attachment_filename='easyweb.png')
4767

4868
# 停止 EasyWeb
4969
@ew.route('/stop')
@@ -52,14 +72,51 @@ def stop(request):
5272

5373
# 获取字符串
5474
@ew.route('/user/<string>')
55-
def welcome(request):
75+
def user(request):
76+
# 访问网页的 /user/123456 试试?
5677
return "<h1>Hello {}</h1>".format(request.match)
5778

5879
# 获取路径
59-
@ew.route('/download/<path>')
60-
def download(request):
61-
return "<h1>Download {}</h1>".format(request.match)
80+
@ew.route('/path/<path>')
81+
def path(request):
82+
# 访问网页的 /path/123/456 试试?
83+
return "<h1>Path {}</h1>".format(request.match)
84+
85+
# 渲染 HTML
86+
@ew.route('/time')
87+
def the_time(request):
88+
# 访问网页的 /time 然后刷新几遍页面,观察网页的变化
89+
return render_template("/web/time.html", time=time.time())
90+
91+
# 获取与设置 Cookies
92+
@ew.route('/cookie')
93+
def cookie(request):
94+
# 访问网页的 /cookie 然后刷新几遍页面,观察网页的变化
95+
response = make_response('<h2>Cookies: {}</h2>'.format(str(request.cookies)))
96+
response.set_cookie('cookie_name', 'cookie_value')
97+
return response
98+
99+
# 自定义状态码
100+
@ew.route('/404')
101+
def status_code(request):
102+
# 访问网页的 /404,打开开发人员工具观察状态码
103+
return '<h2>404 Not Found</h2>', 404
104+
105+
# 获取与设置 Cookies,同时自定义状态码
106+
@ew.route('/cookie2')
107+
def cookie2(request):
108+
# 访问网页的 /cookie 然后刷新几遍页面,观察网页的变化
109+
response = make_response() # 也可以在后面为 response.data 赋值,来代替初始化时赋值
110+
response.data = '<h2>Cookies: {}</h2></br><h2>404 Not Found</h2>'.format(str(request.cookies))
111+
response.set_cookie('cookie_name', 'cookie_value')
112+
return response, 404
113+
114+
# 获取 JSON 格式的内容
115+
@ew.route('/json')
116+
def cookie2(request):
117+
# 访问网页的 /json
118+
return {'type': 'json', 'num': 123}
62119

63120
ew.run()
64-
print('======END======')
121+
print('======END======') # 访问 /stop
65122
```

README.md

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
[简体中文 (Chinese)](./README.ZH-CN.md)
22
# micropython-easyweb
3+
# micropython-easyweb
4+
35
![EasyWeb](./web/EasyWeb_256px.png)
4-
- Web Server library for `Micropython`: Simple, easy-to-use, versatile, highly compatible
6+
- Web Server Library for Micropython: Easy to use, versatile, highly compatible
57

68
### Features
7-
- Aims to mimic the style of the `Flask` framework as much as possible
8-
- Integrates commonly used functionalities such as parsing GET requests, form parsing, HTML rendering, and file sending
9+
- Aims to mimic the style of the Flask framework as much as possible
10+
- Integrates common functionalities such as GET request parameter parsing, form parsing, HTML rendering, file sending, cookie setting, cookie retrieval, dynamic routing, and more.
911

10-
### Usage Instructions
11-
- There are three versions of files available in this project, please choose according to your specific needs:
12-
- `thread`: `/libs/easyweb_thread.py` implemented using multi-threading
13-
- `asyncio`: `/libs/easyweb.py` implemented using asynchronous programming, with good compatibility and reliability
14-
- `single`: `/libs/easyweb_single.py` implemented using single-threaded looping, with good compatibility
12+
### Instructions
13+
- There are three versions of the project files, please choose the one that suits your needs:
14+
- `thread`: `/libs/easyweb_thread.py` - implemented with multithreading
15+
- `asyncio`: `/libs/easyweb.py` - implemented with asynchronous support, provides better compatibility and reliability
16+
- `single`: `/libs/easyweb_single.py` - implemented with a single thread loop, provides good compatibility
1517

1618
### Compatibility
1719
#### Tested Devices
1820
- `ESP-01S`: `single`
1921
- `ESP32-C3`: `single`, `thread`, `asyncio`
2022

21-
### Example Code
23+
### Sample Code
24+
- Here we use [micropython-easynetwork](https://github.com/funnygeeker/micropython-easynetwork) as an example to connect to the local network (it can also work in AP mode, allowing other devices to connect to the development board).
2225
```python
23-
from libs.easyweb import EasyWeb
26+
import time
27+
from libs.easynetwork import Client
28+
from libs.easyweb import EasyWeb, render_template, send_file, make_response
29+
30+
client = Client()
31+
client.connect("ssid", "password") # or client.connect("ssid", "")
32+
33+
while not client.isconnected():
34+
pass
35+
print('IP Address: {}'.format(client.ifconfig()[0]))
2436

2537
ew = EasyWeb() # Initialize EasyWeb
2638

@@ -35,29 +47,76 @@ def home(request):
3547
print('Json: ', request.json)
3648
print('Path: ', request.path)
3749
print('Header: ', request.headers)
50+
print('Cookies: ', request.cookies)
3851
print('Full_Path: ', request.full_path)
39-
return ew.render_template("/web/wifi.html")
52+
return render_template("/web/wifi.html")
4053

41-
# Send a file
54+
# Send file
4255
@ew.route('/easyweb.png')
4356
def img(request):
44-
return ew.send_file("/web/EasyWeb_256px.png")
57+
# Try accessing /easyweb.png on the website
58+
return send_file("/web/EasyWeb_256px.png")
59+
60+
# Download file
61+
@ew.route('/download')
62+
def download(request):
63+
# Try accessing /easyweb.png on the website
64+
# as_attachment: Whether to send the file as an attachment, it will be downloaded when sent as an attachment
65+
# attachment_filename: The filename displayed to the user when downloading. If not provided, the original filename will be used
66+
return send_file("/web/EasyWeb_256px.png", as_attachment=True, attachment_filename='easyweb.png')
4567

4668
# Stop EasyWeb
4769
@ew.route('/stop')
4870
def stop(request):
4971
ew.stop()
5072

51-
# Get a string
73+
# Get string
5274
@ew.route('/user/<string>')
53-
def welcome(request):
75+
def user(request):
76+
# Try accessing /user/123456 on the website
5477
return "<h1>Hello {}</h1>".format(request.match)
5578

56-
# Get a path
57-
@ew.route('/download/<path>')
58-
def download(request):
59-
return "<h1>Download {}</h1>".format(request.match)
79+
# Get path
80+
@ew.route('/path/<path>')
81+
def path(request):
82+
# Try accessing /path/123/456 on the website
83+
return "<h1>Path {}</h1>".format(request.match)
84+
85+
# Render HTML
86+
@ew.route('/time')
87+
def the_time(request):
88+
# Access /time on the website and refresh the page a few times to observe the changes on the webpage
89+
return render_template("/web/time.html", time=time.time())
90+
91+
# Get and set Cookies
92+
@ew.route('/cookie')
93+
def cookie(request):
94+
# Access /cookie on the website and refresh the page a few times to observe the changes on the webpage
95+
response = make_response('<h2>Cookies: {}</h2>'.format(str(request.cookies)))
96+
response.set_cookie('cookie_name', 'cookie_value')
97+
return response
98+
99+
# Custom status code
100+
@ew.route('/404')
101+
def status_code(request):
102+
# Access /404 on the website, open the developer tools to observe the status code
103+
return '<h2>404 Not Found</h2>', 404
104+
105+
# Get and set Cookies, while customizing the status code
106+
@ew.route('/cookie2')
107+
def cookie2(request):
108+
# Access /cookie on the website and refresh the page a few times to observe the changes on the webpage
109+
response = make_response() # You can also assign a value to response.data later instead of initializing it during initialization
110+
response.data = '<h2>Cookies: {}</h2></br><h2>404 Not Found</h2>'.format(str(request.cookies))
111+
response.set_cookie('cookie_name', 'cookie_value')
112+
return response, 404
113+
114+
# Get content in JSON format
115+
@ew.route('/json')
116+
def cookie2(request):
117+
# Access /json on the website
118+
return {'type': 'json', 'num': 123}
60119

61120
ew.run()
62-
print('======END======')
121+
print('======END======') # Access /stop
63122
```

0 commit comments

Comments
 (0)