Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[47] 230307 Ch. 6 Flask๋ฅผ ํ™œ์šฉํ•œ ์„ผ์„œ ์ œ์–ด: 1. Flask ์„ธํŒ…ํ•˜๊ธฐ, 2. Flask ์›น์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ, 3. Flask ๋ผ์šฐํŒ…, 4. Flask LED ์ œ์–ดํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 47์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Python, Flask, SQL

[47] 230307 Ch. 6 Flask๋ฅผ ํ™œ์šฉํ•œ ์„ผ์„œ ์ œ์–ด: 1. Flask ์„ธํŒ…ํ•˜๊ธฐ, 2. Flask ์›น์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ, 3. Flask ๋ผ์šฐํŒ…, 4. Flask LED ์ œ์–ดํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 47์ผ]

yjyuwisely 2023. 3. 7. 12:49

230307 Tue 47th class

Ch. 6 Flask๋ฅผ ํ™œ์šฉํ•œ ์„ผ์„œ ์ œ์–ด
์ง„๋„: p. 156 ~ (๊ต์žฌ: IoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„, ์ €์ž: ์ตœ์ฃผํ˜ธ , ๊น€์žฌ๋ฒ” , ์ •๋™์ง„)
๊ต์žฌ ์ฝ”๋“œ ๊นƒํ—ˆ๋ธŒ: https://github.com/codingspecialist/RaspberryPi4-Book-Example
๊ต์žฌ ์†Œ์Šค ๋ฐ PPT: http://jspstudy.co.kr/main/main.jsp
์ €์ž ์นดํŽ˜: https://cafe.naver.com/metacoding

 

IoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„ | ์ตœ์ฃผํ˜ธ - ๊ต๋ณด๋ฌธ๊ณ 

IoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„ | ใ€ŽIoT ์‚ฌ๋ฌผ์ธํ„ฐ๋„ท์„ ์œ„ํ•œ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด 4 ์ •์„ใ€์€ ์™•์ดˆ๋ณด๋„ ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด๋ฅผ ์ด์šฉํ•ด ๋‹ค์–‘ํ•œ IoT๋ฅผ ์ง์ ‘ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋„๋ก ์ „๊ธฐ์ „์ž ๊ธฐ์ดˆ ์›๋ฆฌ๋ถ€ํ„ฐ ์„ผ

product.kyobobook.co.kr

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


๋ชฐ๋ž๋˜ ๊ฒƒ

- ํด๋ผ์ด์–ธํŠธ: ์ปดํ“จํ„ฐ ๋„คํŠธ์›Œํฌ์—์„œ ํด๋ผ์ด์–ธํŠธ(client, ๋ฌธํ™”์–ด: ์˜๋ขฐ๊ธฐ)๋Š” ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•˜์—ฌ ์„œ๋ฒ„๋ผ๋Š” ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ ์‹œ์Šคํ…œ ์ƒ์˜ ์›๊ฒฉ ์„œ๋น„์Šค์— ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋Š” ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ์„œ๋น„์Šค๋ฅผ ๋งํ•œ๋‹ค.

- ๋ชจ๋“ˆ(module)์ด๋ž€ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ตฌ์„ฑ ์š”์†Œ๋กœ, ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ์™€ ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜๋กœ ๋ฌถ์€ ๋‹จ์œ„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ณดํ†ต ํ•˜๋‚˜์˜ ์†Œ์Šค ํŒŒ์ผ์— ๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ , ํ•จ์ˆ˜์˜ ๊ธฐ๋Šฅ๋ณ„๋กœ ๋”ฐ๋กœ ๋ชจ๋“ˆ์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋“ˆ์„ ํ•ฉ์ณ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ„์ฒ˜๋Ÿผ ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ๋ฅผ ๊ธฐ๋Šฅ๋ณ„๋กœ ๋‚˜๋ˆ ์„œ ๋…๋ฆฝ๋œ ํŒŒ์ผ์— ์ €์žฅํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์„ ๋ชจ๋“ˆํ™” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

- A view function, or view for short, is a Python function that takes a web request and returns a web response. This response can be the HTML contents of a web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response.

์ฐธ๊ณ : https://docs.djangoproject.com/en/4.1/topics/http/views/

Django views are Python functions that takes http requests and returns http response, like HTML documents.
(route ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์•„๋ž˜์— ์žˆ์Œ)

์ฐธ๊ณ : https://www.w3schools.com/django/django_views.php

- ๋ผ์šฐํŒ…์€ ๋„คํŠธ์›Œํฌ์—์„œ ๊ฒฝ๋กœ๋ฅผ ์„ ํƒํ•˜๋Š” ํ”„๋กœ์„ธ์Šค์ž…๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ ๋„คํŠธ์›Œํฌ๋Š” ๋…ธ๋“œ๋ผ๊ณ  ํ•˜๋Š” ์—ฌ๋Ÿฌ ์‹œ์Šคํ…œ๊ณผ ์ด๋Ÿฌํ•œ ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ๋กœ ๋˜๋Š” ๋งํฌ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ƒํ˜ธ ์—ฐ๊ฒฐ๋œ ๋„คํŠธ์›Œํฌ์—์„œ ๋‘ ๋…ธ๋“œ ๊ฐ„์˜ ํ†ต์‹ ์€ ์—ฌ๋Ÿฌ ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ์ด๋ฃจ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Routing is the process of selecting a path for traffic in a network or between or across multiple networks. Broadly, routing is performed in many types of networks, including circuit-switched networks, such as the public switched telephone network (PSTN), and computer networks, such as the Internet.


[์›๋ฆฌ]
return์„ ํ†ตํ•ด ์›น๋ธŒ๋ผ์šฐ์ €์— ๊ฐ’์„ ์ค€๋‹ค.

๊ฐ’์„ ๋ฐ›๋Š” ๊ฒƒ์€ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ํ•œ๋‹ค. 
์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐ’์„ ์ฃผ๊ณ ๋ฐ›๋Š”๋‹ค.


GPIO.cleanup()

GPIO.cleanup()์€ ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ ์•ˆ์—์„œ ์ดˆ๊ธฐํ™”ํ–ˆ๋˜ ํ•€ ์„ค์ •๋“ค์„ ๋ชจ๋‘ '์ฒญ์†Œ'(clean up)ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ, ์ฆ‰ GPIO ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ/๋ชจ๋“ˆ์ด ์ ์œ ํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ํ•ด์ œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค. 

GPIO์˜ ์‚ฌ์šฉ์ด ๋ชจ๋‘ ๋๋‚  ๋•Œ(=ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋  ๋•Œ) GPIO.cleanup() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ณ , ๋ผ์ฆˆ๋ฒ ๋ฆฌ ํŒŒ์ด ๊ฐœ๋ฐœ์ž ์ธก์—์„œ๋„ ๊ฐ•๋ ฅํžˆ ๊ถŒ์žฅํ•˜๋Š” ์‚ฌํ•ญ์ด๋‹ค.

์ฐธ๊ณ : https://junolefou.tistory.com/5


์ฐธ๊ณ : https://programming119.tistory.com/194

์ฐธ๊ณ : https://hoyeonkim795.github.io/posts/rest_api/

๋ฌผ์Œํ‘œ ๋’ค์— ์˜ค๋Š” ๊ฒƒ์€ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์ด๋‹ค. 


Vscode์—์„œ Python Flask๊ฐœ๋ฐœํ™˜๊ฒฝ ๊ตฌ์ถ•ํ•˜๊ธฐ
์ฐธ๊ณ : https://krksap.tistory.com/1766


์š”์•ฝ

1. ๋ผ์ฆˆ๋ฒ ๋ฆฌํŒŒ์ด ์ง€๋‹ˆ์—๋””ํ„ฐ -> vscode ์—๋””ํ„ฐ
2. vscode ์—๋””ํ„ฐ + flask framework๋ฅผ ์„ค์ •
3. flask + server ๊ตฌ์ถ•

Ch. 6 Flask๋ฅผ ํ™œ์šฉํ•œ ์„ผ์„œ ์ œ์–ด

์ฑ…์˜ ๋ชฉ์ฐจ
01 _ Flask ์„ธํŒ…ํ•˜๊ธฐ

01-1 ๋ผ์ฆˆ๋ฒ ๋ฆฌ 4์—์„œ ์„ค์น˜ํ•˜๊ธฐ
01-2 Flask ์„ค์น˜ํ•˜๊ธฐ

02 _ Flask ์›น์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ

03 _ Flask ๋ผ์šฐํŒ…

04 _ Flask LED ์ œ์–ดํ•˜๊ธฐ
04-1 Flask LED ์ œ์–ด ์ •์  ๋ผ์šฐํŒ…
04-2 Flask LED ์ œ์–ด ๋™์  ๋ผ์šฐํŒ…ใ€ˆ์‚ฐํ˜•๊ด„ํ˜ธใ€‰
04-3 Flask LED ์ œ์–ด ๋™์  ๋ผ์šฐํŒ…ใ€ˆ์ฟผ๋ฆฌ์ŠคํŠธ๋งใ€‰

05 _ Flask ์›นํŽ˜์ด์ง€
05-1 Flask, HTML, CSS, Javascript๋ฅผ ์ด์šฉํ•œ ์›นํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ
05-2 fetch() ํ•จ์ˆ˜ ์ดํ•ดํ•˜๊ธฐ
05-3 ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์ดํ•ดํ•˜๊ธฐ

06 _ IP, Port, Process ๊ฐœ๋… ์žก๊ธฐ
06-1 IP ๊ฐœ๋… ์žก๊ธฐ
06-2 Port ๊ฐœ๋… ์žก๊ธฐ
06-3 Process ๊ฐœ๋… ์žก๊ธฐ

07 _ cron๊ณผ daemon ๊ฐœ๋… ์žก๊ธฐ
07-1 cron(ํฌ๋ก ) ๊ฐœ๋… ์žก๊ธฐ
07-2 daemon(๋ฐ๋ชฌ) ๊ฐœ๋… ์žก๊ธฐ

08 _ ๊ณต๊ณต๋ฐ์ดํ„ฐ ํ™œ์šฉํ•˜๊ธฐ
08-1 ๊ณต๊ณต๋ฐ์ดํ„ฐ ์‹ ์ฒญํ•˜๊ธฐ
08-2 ๊ณต๊ณต๋ฐ์ดํ„ฐ ๋ถ„์„ํ•˜๊ธฐ
08-3 ํŒŒ์ด์ฌ์„ ํ™œ์šฉํ•˜์—ฌ ์˜จ์Šต๋„ ํ™•์ธํ•˜๊ธฐ
ํ•™์Šต๋ชฉํ‘œ
- Flask๋ฅผ ์„ค์น˜ํ•˜๊ณ  ์›น ์„œ๋ฒ„๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋‹ค.
- Flask๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์„ผ์„œ๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค.
- IP, Port, Process, Cron, Daemon์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ•™์Šต๋‚ด์šฉ
- Flask ์„ธํŒ…ํ•˜๊ธฐ
- Flask ์›น ์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ
- Flask ๋ผ์šฐํŒ…
- Flask LED ์ œ์–ดํ•˜๊ธฐ
- Flask ์›น ํŽ˜์ด์ง€
- IP, Port, Process ๊ฐœ๋… ์žก๊ธฐ
- Cron, Daemon ๊ฐœ๋… ์žก๊ธฐ
- ๊ณต๊ณต๋ฐ์ดํ„ฐ ํ™œ์šฉํ•˜๊ธฐ

1 Flask ์„ธํŒ…ํ•˜๊ธฐ

- ๋Œ€ํ‘œ์ ์ธ ์›น ํ”„๋ ˆ์ž„์›Œํฌ๋กœ ์žฅ๊ณ (Django)์™€ ํ”Œ๋ผ์Šคํฌ(Flask)๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‘˜ ์ค‘์— ํ”Œ๋ผ์Šคํฌ๋ฅผ ์ด์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
- ์žฅ๊ณ ๋Š” ํ’€์Šคํƒ ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ๋•Œ ํ•„์š”๋กœ ํ•˜๋Š” ๋ชจ๋“  ๊ฒƒ๋“ค์ด ์ข…ํ•ฉ์ ์œผ๋กœ ๊ฐ–์ถ”์–ด์ง„ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ, ์ธ์ฆ๊ณผ ๊ถŒํ•œ, ORM, ํ…œํ”Œ๋ฆฟ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ๋กœ๊ทธ์ฒ˜๋ฆฌ, ๋ณด์•ˆ, ๊ด€๋ฆฌ์ž ๊ธฐ๋Šฅ ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ์š”์†Œ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.

- ํ”Œ๋ผ์Šคํฌ๋Š” ๊ฒฝ๋Ÿ‰ ์›น ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค.
WSGI์ฝ”์–ด์™€ URL ๋ผ์šฐํŒ…์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด werkzeug(๋ฒก์ž์ด๊ทธ)์™€ ํ…œํ”Œ๋ฆฟ ์ถœ๋ ฅ์„ ์œ„ํ•ด jinja2 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ•จ๊ป˜ ๋ฐฐํฌํ•œ๋‹ค.
๊ผญ ํ•„์š”ํ•œ ํ•ต์‹ฌ์š”์†Œ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ํ•„์š”ํ•  ๋•Œ ๋งˆ๋‹ค ์ง์ ‘ ํ™˜๊ฒฝ์— ๋งž์ถฐ ๊ฐœ๋ฐœ์„ ํ•˜๊ณ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐพ์•„์„œ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
 
์šฉ์–ด ์„ค๋ช…

- WSGI: The Web Server Gateway Interface is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language.

- Werkzeug is a collection of libraries that can be used to create a WSGI (Web Server Gateway Interface) compatible web application in Python. A WSGI (Web Server Gateway Interface) server is necessary for Python web applications since a web server cannot communicate directly with Python.

- Routing is the process of selecting a path for traffic in a network or between or across multiple networks. Broadly, routing is performed in many types of networks, including circuit-switched networks, such as the public switched telephone network (PSTN), and computer networks, such as the Internet.

- ํ…œํ”Œ๋ฆฟ ์—”์ง„(Template Engine)์ด๋ž€
ํ…œํ”Œ๋ฆฟ ์–‘์‹๊ณผ ํŠน์ • ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์— ๋”ฐ๋ฅธ ์ž…๋ ฅ ์ž๋ฃŒ๋ฅผ ํ•ฉ์„ฑํ•˜์—ฌ ๊ฒฐ๊ณผ ๋ฌธ์„œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด(๋˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด ์ปดํฌ๋„ŒํŠธ)๋ฅผ ๋งํ•œ๋‹ค.

๊ทธ ์ค‘ ์›น ํ…œํ”Œ๋ฆฟ ์—”์ง„(web template engine)์ด๋ž€ ์›น ๋ฌธ์„œ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ๋งํ•œ๋‹ค.
์ฐธ๊ณ : https://gmlwjd9405.github.io/2018/12/21/template-engine.html

- Jinja is a web template engine for the Python programming language.


1.1 ๋ผ์ฆˆ๋ฒ ๋ฆฌ 4์—์„œ ์„ค์น˜ํ•˜๊ธฐ

VSCode(Visual Studio Code) ์„ค์น˜ํ•˜๊ธฐ
1. VSCode๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
$ sudo apt install code

2. ๋ฉ”๋‰ด์ฐฝ์—์„œ ๊ฐœ๋ฐœ - Code - OSS๋ฅผ ํด๋ฆญํ•˜๋ฉด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

3. ์‹คํ–‰์ด ์™„๋ฃŒ๋œ๋‹ค.

4. ๋ฉ”๋‰ด์—์„œ File - Open Folder๋ฅผ ํ†ตํ•ด์„œ webapps ํด๋”๋ฅผ ๊ธฐ๋ณธ ํด๋”๋กœ ์„ค์ •ํ•œ๋‹ค. 


1.2 Flask ์„ค์น˜ํ•˜๊ธฐ

1. VSCode์—์„œ ํ„ฐ๋ฏธ๋„ ์—ด๊ธฐ

 

2. ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ํŒŒ์ด์ฌ ๋ฒ„์ „์„ ํ™•์ธํ•œ๋‹ค.
$ python -V


3. Flask ์„ค์น˜ 

https://flask.palletsprojects.com/en/2.2.x/

~ ํ‘œ์‹œ๋Š” ํด๋” ์œ„์น˜๋ฅผ ๋งํ•œ๋‹ค. 

4. ํ„ฐ๋ฏธ๋„์—์„œ python ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

$ python

5. flask๊ฐ€ ์ž˜ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด flask๋ฅผ import ํ•œ๋‹ค.

>>> from flask import Flask

์•„๋ฌด๋Ÿฐ ๋ฉ”์‹œ์ง€๊ฐ€ ์—†๋‹ค๋ฉด ์ •์ƒ์ ์œผ๋กœ ์„ค์น˜๊ฐ€ ๋œ ๊ฒƒ์ด๋‹ค.

6. Python extension์„ ์„ค์น˜ํ•œ๋‹ค.
์„ค์น˜ ์•Œ๋ฆผ์ฐฝ์ด ๋œจ์ง€ ์•Š์œผ๋ฉด ์™ผ์ชฝ์— ์žˆ๋Š” ๋„๊ตฌ ์ฐฝ ๊ฐ€์žฅ ์•„๋ž˜์— ์žˆ๋Š” extension์„ ํด๋ฆญํ•œ ๋’ค ๊ฒ€์ƒ‰์ฐฝ์—์„œ python์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์„ค์น˜ํ•œ๋‹ค.


2 Flask ์›น์„œ๋ฒ„ ๊ตฌ์ถ•ํ•˜๊ธฐ

ํด๋” ๊ตฌ์„ฑ๊ณผ ํŒŒ์ผ ๊ตฌ์„ฑ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•œ๋‹ค.

webapps
		- ch06
        		- helloworld
                			- app_start.py

1. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์ €์žฅํ•œ๋‹ค.
[1] app_start.py

from flask import Flask
app = Flask(__name__) #ํŒŒ์ผ๋ช… ์ €์žฅ
@app.route("/") # ์ด๋ก ์ ์œผ๋กœ ๋“ค์–ด๊ฐ, ์ง์ ‘ ํƒ€์ดํ•‘ํ•œ ์ฃผ์†Œ์— / ์ƒ๋žต ๊ฐ€๋Šฅ
#@app.route("/abc") 
#@app.route("/main") # ex) http://news.naver.com/main

def helloworld(): #์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ํ˜ธ์ถœํ•œ๋‹ค. 
	return "Hello World"

if __name__ = "__main__": #main ๋ฌธ์ž์—ด ์ €์žฅ
	app.run(host="0.0.0.0")

์œ„์˜ "__main__"์€ ์•„๋ž˜ ์ž๋ฐ”์˜ main()๊ณผ ๊ฐ™๋‹ค.

class A{}
class B{}
class C{
			public static void main(){
					B b = new B();
					A a = new A();
       }
}

2. ์‹คํ–‰๋ฐฉ๋ฒ•์€ ํ„ฐ๋ฏธ๋„์„ ์—ด๊ณ  ์•„๋ž˜๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

$ cd /home/pi/webapps/ch06/helloworld
$ python app_start.py

3. ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ด๊ณ  ๋‹ค์Œ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

http://localhost:5000


4. ์•„๋ž˜๋ถ€ํ„ฐ ์ฝ”๋“œ ๋ฆฌ๋ทฐ์ด๋‹ค.

1) from flask์—์„œ flask๋Š” ํด๋”๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

from flask import Flask

from flask: flask ํด๋” ๋˜๋Š” flask ๋ชจ๋“ˆ์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ flask ๋ชจ๋“ˆ์ด๋ž€ flask.py ํŒŒ์ผ์„ ์˜๋ฏธํ•œ๋‹ค.
์ž‘์„ฑํ•œ from flask๋Š” ํด๋”๋ช…์ด๋‹ค.

2) import Flask์—์„œ Flask๋Š” ํด๋ž˜์Šค๋ช…์ด๋‹ค.

from flask import Flask


import Flask์—์„œ Flask๋Š” ํด๋ž˜์Šค๋ช…, ํ•จ์ˆ˜๋ช…, ๋ณ€์ˆ˜๋ช…์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋ฆ„์ด ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์€ ํด๋ž˜์Šค์ด๋‹ค.
๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ path๊ฐ€ ์ ์šฉ๋œ ๊ธฐ๋ณธ ํด๋”์—์„œ flask๋ผ๋Š” ํด๋”๋ฅผ ์ฐพ๊ณ , ๊ทธ ์•ˆ์— ์žˆ๋Š” Flaskํด๋ž˜์Šค๋ฅผ importํ•˜๋ผ๋Š” ๋œป์ด๋‹ค.
import๋ฅผ ํ†ตํ•ด์„œ ํ•จ์ˆ˜๋งŒ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

from ๋ชจ๋“ˆ๋ช… import ํ•จ์ˆ˜๋ช…

3) app์€ Flask ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง„๋‹ค.

app = Flask(__name__)

flask ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. flask ๊ฐ์ฒด๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €๋กœ ์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ๊ณผ ํ…œํ”Œ๋ฆฟ๊ณผ์˜ ์—ฐ๊ณ„์™€ ๊ฐ™์€ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „๋ฐ˜์— ๋Œ€ํ•ด์„œ ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ๋ฉ”์ธ ๊ฐ์ฒด์ด๋‹ค. 
Flask ํด๋ž˜์Šค๋Š” ์ƒ์„ฑ์ž ์ธ์ž๋ฅผ ํ•˜๋‚˜ ๋ฐ›๊ฒŒ ๋˜๋Š”๋ฐ ๋ฐ”๋กœ __name__์ด๋‹ค. 
__name__ : ๋‚ด ํŒŒ์ผ๋ช…, ์ฆ‰ ๋ชจ๋“ˆ๋ช…์„ ์˜๋ฏธํ•œ๋‹ค. 
์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ํŒŒ์ผ๋ช…์ด app_start.py์ด๋ฏ€๋กœ __name__์€ app_start.py์ด๋‹ค.
๊ฒฐ๊ตญ Flask ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ app_start.py ํŒŒ์ผ๋ช…์„ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. 

__name__ ๋‚ด์žฅ ๋ณ€์ˆ˜์ด๋ฉด์„œ ์ „์—ญ ๋ณ€์ˆ˜์ด๋‹ค.
์ด ๋ณ€์ˆ˜์—๋Š” ํ•ด๋‹น ํŒŒ์ด์ฌ ํŒŒ์ผ์˜ ์ด๋ฆ„(์—ฌ๊ธฐ์„œ๋Š”app_start)์ด ์ €์žฅ๋œ๋‹ค.
์ด๋•Œ ํŒŒ์ด์ฌ์ด ํ•ด์„์„ ํ•  ๋•Œ app_start.py๋ฅผ ์‹œ์ž‘์ ์œผ๋กœ ํ•˜๊ธฐ ์œ„ํ•ด
app_start.pyํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ __name__๋ณ€์ˆ˜์— __main__(๋ฌธ์ž์—ด)์ด๋ผ๋Š” ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค.

๊ฒฐ๋ก : if __name__ == "__main__"์€ ์‹œ์ž‘์ ์„ ์˜๋ฏธํ•œ๋‹ค. 

4) app.route("/")๋Š” ๋ผ์šฐํŒ…์„ ์œ„ํ•œ ๋ทฐ ํ•จ์ˆ˜(๋ฐ‘์˜ def ํ•จ์ˆ˜)๋ฅผ ๋“ฑ๋กํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. 
์ฐธ๊ณ : https://hleecaster.com/flask-introduction

@app.route("/")

"/"์ด๋ฏ€๋กœ http://localhost:5000/์œผ๋กœ ์š”์ฒญํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น route๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
ํด๋ผ์ด์–ธํŠธ๊ฐ€ URI๋กœ ("/") ์š”์ฒญ์„ ํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น route ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์•„๋ž˜์— ์žˆ๋Š” ๋ทฐ ํ•จ์ˆ˜๊ฐ€ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋œ๋‹ค.
ex) ("/") ์ด๋ ‡๊ฒŒ ๋˜์–ด์žˆ์œผ๋‹ˆ http://localhost:5000/์œผ๋กœ ์š”์ฒญํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น route๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. 

@app.route("/helloworld")

์œ„์™€ ๊ฐ™๋‹ค๋ฉด ํ˜ธ์ถœ ๋ฐฉ๋ฒ•์€ http://localhost:5000/helloworld๊ฐ€ ๋œ๋‹ค.

5) "Hello World"๋Š” ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ ์š”์ฒญํ•œ์ชฝ์œผ๋กœ ์‘๋‹ต๋˜๋Š” String ๊ฐ’์ด๋‹ค.

def helloworld():
	return "Hello World"

route ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ๋ทฐ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ
* ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ฃผ์†Œ์š”์ฒญ์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋ทฐ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๋ฐ ์ด๋•Œ ๋ทฐ ํ•จ์ˆ˜์—๋Š” ํ•„์ˆ˜์ ์œผ๋กœ return๋ฌธ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

6) app.run()์ด ํ˜ธ์ถœ๋˜๋ฉด ์›น์„œ๋ฒ„๊ฐ€ ๋™์ž‘ํ•œ๋‹ค.

if __name__ = "__main__":
	app.run(host="0.0.0.0")

__name__: ๋‘ ๊ฐ€์ง€ ์ด๋ฆ„์„ ๊ฐ€์ง„๋‹ค.

1. ๋‚ด ๋ชจ๋“ˆ๋ช…: app_start.py (ํŒŒ์ผ ์ด๋ฆ„)

2. app_start.py ํŒŒ์ผ์„ ์ง์ ‘ ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ํ•˜๋ฉด __main__์ด๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„๋‹ค.
๋งŒ์•ฝ ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ app_start.py ํŒŒ์ผ์„ importํ•ด์„œ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด __name__์ด __main__์ด๋ผ๋Š” ์ด๋ฆ„์ด ๋  ์ˆ˜ ์—†๋‹ค.

__name__์ด __main__์ด ๋˜๋ ค๋ฉด import๊ฐ€ ์•„๋‹Œ ํ•ด๋‹น ํŒŒ์ผ์„ ์ง์ ‘ ์‹คํ–‰์‹œํ‚ฌ ๋•Œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. 
์œ„ ๋ฌธ์žฅ์ด ํ•„์š”ํ•œ ์ด์œ ๋Š” app_start.py ํŒŒ์ผ์„ ์ง์ ‘ ์‹คํ–‰์‹œ์ผฐ์„ ๋•Œ๋งŒ "์„œ๋ฒ„๋ฅผ ์ž‘๋™์‹œ์ผœ"๋ผ๋Š” ๋œป์ด๋‹ค.

app.run()์€ ์„œ๋ฒ„๋ฅผ ์‹œ์ž‘ํ•˜๋ผ๋Š” ๋ช…๋ น์–ด๊ณ , ์ธ์ž 0,0,0,0(ip ์ฃผ์†Œ)์€ ์–ด๋–ค ip์—์„œ ์š”์ฒญ์ด ๋“ค์–ด์™€๋„ ์‘๋‹ต์„ ํ•ด์ฃผ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

ํ˜„์žฌ ํŒŒ์ผ์ด app_start.py import flask
__name__ ==> "_main_"

ํ˜„์žฌ ํŒŒ์ผ์ด app_end.py import app_start
__name__ ==> "ํŒŒ์ผ๋ช…"(app_end)

์ฐธ๊ณ : https://www.freecodecamp.org/korean/news/untitled-2/


3 Flask ๋ผ์šฐํŒ… p. 180

URI ๋””์ŠคํŒจ์น˜: Flask์—์„œ URL์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ

URI ๋””์ŠคํŒจ์น˜์˜ ์—ญํ•  

1) ์‚ฌ์šฉ์ž๊ฐ€
์ž…๋ ฅํ•œ URI๋ฅผ ์ง€์ผœ๋ณด๋‹ค๊ฐ€ URI๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์˜ฌ๋ฐ”๋ฅธ ๊ธธ๋กœ ์•ˆ๋‚ดํ•ด์ค€๋‹ค.

2) ๋ทฐ ๋ฐ์ฝ”๋ ˆ์ด์…˜์„ ํ†ตํ•ด์„œ ๋ทฐ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์˜ฌ๋ฐ”๋ฅธ ๊ธธ๋กœ ์•ˆ๋‚ด
ํ•ด์ค€๋‹ค.

 

URI๋Š” ํŠน์ • ๋ฆฌ์†Œ์Šค๋ฅผ ์‹๋ณ„ํ•˜๋Š” ํ†ตํ•ฉ ์ž์› ์‹๋ณ„์ž(Uniform Resource Identifier)๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์›น ๊ธฐ์ˆ ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋…ผ๋ฆฌ์  ๋˜๋Š” ๋ฌผ๋ฆฌ์  ๋ฆฌ์†Œ์Šค๋ฅผ ์‹๋ณ„ํ•˜๋Š” ๊ณ ์œ ํ•œ ๋ฌธ์ž์—ด ์‹œํ€€์Šค๋‹ค.

์ฐธ๊ณ : https://www.charlezz.com/?p=44767


[2] app_start.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def helloworld():
	return "Hello World"

@app.route("/led/on")
def led_on():
	return "LED ON"
    
@app.route("/led/off")
def led_off():
	return "LEF OFF"
    
if __name__ == "__main__":
	app.run(host="0.0.0.0")

return์„ ํ†ตํ•ด ์›น๋ธŒ๋ผ์šฐ์ €์— ๊ฐ’์„ ์ค€๋‹ค.
๊ฐ’์„ ๋ฐ›๋Š” ๊ฒƒ์€ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ํ•œ๋‹ค. 
์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐ’์„ ์ฃผ๊ณ ๋ฐ›๋Š”๋‹ค. 

2. ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ด์–ด์„œ ์‹คํ–‰์‹œํ‚จ๋‹ค.
์›น๋ธŒ๋ผ์šฐ์ €์— localhost:5000/led/on ์ž…๋ ฅ ์‹œ ์›น๋ธŒ๋ผ์šฐ์ €์— LED ON ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ
์›น๋ธŒ๋ผ์šฐ์ €์— localhost:5000/led/off ์ž…๋ ฅ ์‹œ ์›น๋ธŒ๋ผ์šฐ์ €์— LED OFF๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ


4 Flask LED ์ œ์–ดํ•˜๊ธฐ
4.1 Flask LED ์ œ์–ด ์ •์  ๋ผ์šฐํŒ… p. 181 

[3] app_start.py 

from flask import Flask
import RPi.GPIO as GPIO

app = Flask(__name__)

LED = 8 #GPIO 8๋ฒˆ๊ณผ ์—ฐ๊ฒฐ 
GPIO.setmode(GPIO.BOARD) #BOARD๋Š” ์ปค๋„ฅํ„ฐ pin๋ฒˆํ˜ธ ์‚ฌ์šฉ
GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)

@app.route("/")
def helloworld():
	return "Hello World"

@app.route("/led/on")
def led_on():
	GPIO.output(LED, GPIO.HIGH)
    return "LED ON"
    
@app.route("/led/off")
def led_off():
	GPIO.output(LED, GPIO.LOW)
    return "LED OFF"
    
@app.route("/gpio/cleanup")
def gpio_cleanup():
	GPIO.cleanup()
    return "GPIO CLEANUP"
    
if __name__ == "__main__":
	app.run(host="0.0.0.0")

์ฐธ๊ณ 
์ €ํ•ญ์€ 200์˜ด ์ด์ƒ๋งŒ ๋œ๋‹ค๋ฉด OK!!

 

06 ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ด์–ด์„œ localhost:5000/gpio/cleanup์œผ๋กœ ์ ‘์†ํ•œ๋‹ค. (์ฃผ์†Œ์ฐฝ์— ์ ๋Š”๋‹ค.)
๊ฒฐ๊ณผ: GPIO CLEANUP

์ฐธ๊ณ : https://kibua20.tistory.com/95

๊ฒฐ๊ณผ: 

Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

Protocol ํ”„๋กœํ† ์ฝœ: http://0.0.0.0
Port ํฌํŠธ๋ฒˆํ˜ธ: 5000

์ฐธ๊ณ : https://code.visualstudio.com/docs/python/tutorial-flask


http://localhost:5000/led/off             <๋ณ€์ˆ˜๋ช…>: ์‚ฐํ˜ธ ๊ด„ํ˜ธ

                                        //๋ณ€์ˆ˜๋ช…(state) = ๊ฐ’(off) (๋ฌธ์ž์—ด์ด์–ด๋„ off๋กœ ํ‘œ์‹œํ•œ๋‹ค. " " ์•ˆ ์“ด๋‹ค.) 
http://localhost:5000/led/?state=off               : ์ฟผ๋ฆฌ์ŠคํŠธ๋ง


4.2 Flask LED ์ œ์–ด ๋™์  ๋ผ์šฐํŒ… ใ€ˆ์‚ฐํ˜• ๊ด„ํ˜ธใ€‰

- ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ๋˜๋Š” URL์˜ ๋ทฐ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‚ฐํ˜• ๊ด„ํ˜ธ <>์„ ์ด์šฉํ•ด์„œ ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค.
ํ•ด๋‹น ๋ณ€์ˆ˜๊ฐ€ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ถ€ํ„ฐ URL๋กœ ์ „๋‹ฌ๋˜๋ฉด ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„์ค˜์•ผ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

app_start.py
from flask import Flask
import RPi.GPIO as GPIO

app = Flask(__name__)

LED = 8
GPIO.setmode(GPIO.BOARD) #BOARD๋Š” ์ปค๋„ฅํ„ฐ pin๋ฒˆํ˜ธ ์‚ฌ์šฉ
GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)

@app.route("/")
def helloworld():
	return "Hello World"

@app.route("/led/<state>")
def led(state):
	if state == "on":
    	GPIO.output(LED, GPIO.HIGH)
    else:
    	GPIO.output(LED, GPIO.LOW)
   	return "LED" +state
    
@app.route("/gpio/cleanup")
def gpio_cleanup():
	GPIO.cleanup()
    return "GPIO CLEANUP"

if __name__ = "__main__"
	app.run(host="0.0.0.0")


4.3 Flask LED ์ œ์–ด ๋™์  ๋ผ์šฐํŒ… ใ€ˆ์ฟผ๋ฆฌ ์ŠคํŠธ๋งใ€‰?state=on

                                        //๋ณ€์ˆ˜๋ช…(state) = ๊ฐ’(off) (๋ฌธ์ž์—ด์ด์–ด๋„ off๋กœ ํ‘œ์‹œํ•œ๋‹ค. " " ์•ˆ ์“ด๋‹ค.) 
http://localhost:5000/led/?state=off               : ์ฟผ๋ฆฌ์ŠคํŠธ๋ง

๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ๋˜๋Š” URLํ•จ์ˆ˜๋Š” ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง ๋ฐฉ์‹์œผ๋กœ๋„ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์ด๋ž€
http://localhost:5000/led?state=on
์ฒ˜๋Ÿผ ์ฃผ์†Œ ํ˜ธ์ถœ์ด ์˜ฌ ๋•Œ ? ๋’ค์— ์˜ค๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.
? ๋’ค์— ์˜ค๋Š” ๊ฒƒ๋“ค์€ HTTP์š”์ฒญ์—์„œ GET๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ ๊ฐ€๋Šฅํ•˜๋‹ค.
์ „๋‹ฌ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ request ํด๋ž˜์Šค์˜ args ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. 

- ๋‹ค์Œ๊ณผ ๊ฐ™์ด flask ๋ชจ๋“ˆ์—์„œ request๋ฅผ importํ•ด์•ผ request๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 
from flask import Flask, request

app_start.py

from f1
from flask import Flask
import RPi.GPIO as GPIO

app = Flask(__name__)

LED = 8
GPIO.setmode(GPIO.BOARD) #BOARD๋Š” ์ปค๋„ฅํ„ฐ pin๋ฒˆํ˜ธ ์‚ฌ์šฉ
GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)

@app.route("/")
def helloworld():
	return "Hello World"

@app.route("/led")
def led():
	state = request.args.get("state") #on์—์„œ ๋ณ€์ˆ˜์ด๋ฆ„์ด "state"์ธ ๊ฒƒ์„ ๊ฐ€์ ธ์˜จ๋‹ค. 
	if state == "on":
    	GPIO.output(LED, GPIO.HIGH)
    else:
    	GPIO.output(LED, GPIO.LOW)
   	return "LED"+state
    
@app.route("/gpio/cleanup")
def gpio_cleanup():
	GPIO.cleanup()
    return "GPIO CLEANUP"

if __name__ = "__main__"
	app.run(host="0.0.0.0")

2. ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ด์–ด์„œ localhost:5000/led?state=on์œผ๋กœ ์ ‘์†ํ•œ๋‹ค. (=์ฃผ์†Œ์ฐฝ์— ์ ๋Š”๋‹ค.)
๊ฒฐ๊ณผ(์›นํŽ˜์ด์ง€์— ๋œฌ๋‹ค.): LED on

3. ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ด์–ด์„œ localhost:5000/led?state=off๋กœ ์ ‘์†ํ•œ๋‹ค.
๊ฒฐ๊ณผ: LED off

4. ๊ฒฐ๊ณผ:์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ด์–ด์„œ localhost:5000/led๋กœ ์ ‘์†ํ•œ๋‹ค. (?๊ฐ€ ์—†์œผ๋ฏ€๋กœ) 
๊ฒฐ๊ณผ: Internal Server Error ์›นํŽ˜์ด์ง€๊ฐ€ ๋œฌ๋‹ค. 


์„œ๋ฒ„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 
์ด์œ ๋Š” ๋ทฐ ํ•จ์ˆ˜์—์„œ๋Š” ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๊ฐ€ ์ค€๋น„๋˜์–ด ์žˆ์ง€๋งŒ ์ฃผ์†Œ ์š”์ฒญ ์‹œ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ์ „๋‹ฌํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
์ด๋Ÿด ๊ฒฝ์šฐ, ๋ฌผ๋ก  ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ์ •์ƒ์ ์œผ๋กœ ๋ณด๋‚ด๊ฒŒ ๋˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€ ์•Š๊ฒ ์ง€๋งŒ ๋ฏธ๋ž˜ ๋Œ€๋น„ํ•˜๋ฉด ์ข‹๋‹ค. 

5. app_start.py ํŒŒ์ผ์„ ์ˆ˜์ •ํ•œ๋‹ค.

@app.route("/led")
def led():
	state = request.args.get("state") #on์—์„œ ๋ณ€์ˆ˜์ด๋ฆ„์ด "state"์ธ ๊ฒƒ์„ ๊ฐ€์ ธ์˜จ๋‹ค. 
	if state == "on":
    	GPIO.output(LED, GPIO.HIGH)
    else:
    	GPIO.output(LED, GPIO.LOW)
    elif state == "error":
    	return "์ฟผ๋ฆฌ์ŠคํŠธ๋ง state๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."
    else:
    	return "์ž˜๋ชป๋œ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์ด ์ „๋‹ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
   	return "LED"+state

6. ๋‹ค์–‘ํ•˜๊ฒŒ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•œ๋‹ค.

localhost5000/led #state == "error"
#๊ฒฐ๊ณผ: ์ฟผ๋ฆฌ์ŠคํŠธ๋ง state๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

localhost5000/led?see=on #state == "error" 
#๊ฒฐ๊ณผ: ์ฟผ๋ฆฌ์ŠคํŠธ๋ง state๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

localhost5000/led?state= #๋’ค์— ๋น„์—ˆ์Œ
#๊ฒฐ๊ณผ: ์ž˜๋ชป๋œ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์ด ์ „๋‹ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

localhost5000/led?state=hello #on, off๋งŒ ์„ค์ •ํ–ˆ์Œ
#๊ฒฐ๊ณผ: ์ž˜๋ชป๋œ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์ด ์ „๋‹ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
Comments