From 6e9caf8dd7315fed874d36da8d5514d4e3deeb00 Mon Sep 17 00:00:00 2001 From: "matt.gummel" Date: Thu, 3 Sep 2020 22:54:21 -0700 Subject: [PATCH 1/4] adding ignored files --- .gitignore | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d55f023 --- /dev/null +++ b/.gitignore @@ -0,0 +1,131 @@ +# Byte-compiled / optimized / DLL files +*passwords.txt +**/.idea/* +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ From f4603bfdf4454af6adc3870bf752120998ccbae3 Mon Sep 17 00:00:00 2001 From: "matt.gummel" Date: Thu, 3 Sep 2020 22:59:55 -0700 Subject: [PATCH 2/4] removing passwords --- .gitignore | 3 ++- passwords.txt | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) delete mode 100644 passwords.txt diff --git a/.gitignore b/.gitignore index d55f023..bfc8877 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Byte-compiled / optimized / DLL files -*passwords.txt +passwords.txt +**passwords.txt **/.idea/* __pycache__/ *.py[cod] diff --git a/passwords.txt b/passwords.txt deleted file mode 100644 index 9f2747f..0000000 --- a/passwords.txt +++ /dev/null @@ -1,2 +0,0 @@ -EarlGreyTea HotHotHot -MyHomeAccessPoint aueiUeh73NB From a066f2b4ee49b5db30d8e68de1183b9f30af1570 Mon Sep 17 00:00:00 2001 From: "matt.gummel" Date: Sun, 6 Sep 2020 22:14:50 -0700 Subject: [PATCH 3/4] full webserver functionality --- .DS_Store | Bin 0 -> 6148 bytes flexible_web_server/main.py | 89 ++++++++++++++++++++++-------------- 2 files changed, 54 insertions(+), 35 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d9b274b064eb4e15132e32babddc789cc7b6a265 GIT binary patch literal 6148 zcmeHKOHKnZ41I=HRAp1dhGnk64Wg=?fCGSr7N|NfsO7huqw^dasUt>$6|~5flIO&q zBu|=T3_zwY^K)PVV8o^RF0)K$~e9PWqc-iN4AV~Iy}_J4|7 zPA~9IlR7MLg*WyUaYJ0=Rr~EDA2CORXWW-;ciW~D#w_uGr=MjSf49_r&>Oo927-ZL zAQ;#|2K3q`6tiP%SgsCsr34@jI9-Ig))E?%9J6C06uFuGv)7uT_OYz4H^mf!-r$#c1}VI{mU^Yp7Xt SzJwFwN1zZAT`=$q4156N1~(@F literal 0 HcmV?d00001 diff --git a/flexible_web_server/main.py b/flexible_web_server/main.py index d1855c1..58ac4eb 100644 --- a/flexible_web_server/main.py +++ b/flexible_web_server/main.py @@ -3,54 +3,72 @@ except: import socket -response_404 = """HTTP/1.0 404 NOT FOUND - -

404 Not Found

-""" - -response_500 = """HTTP/1.0 500 INTERNAL SERVER ERROR - -

500 Internal Server Error

-""" - response_template = """HTTP/1.0 200 OK %s """ - import machine import ntptime, utime from machine import RTC -from time import sleep - +seconds = ntptime.time() rtc = RTC() -try: - seconds = ntptime.time() -except: - seconds = 0 rtc.datetime(utime.localtime(seconds)) +adc = machine.ADC(0) +pin = machine.Pin(16, machine.Pin.OUT) +switch_pin = machine.Pin(10, machine.Pin.IN) + + def time(): body = """ - -

Time

-

%s

- - -""" % str(rtc.datetime()) + +

Time

+

%s

+ + + """ % str(rtc.datetime()) return response_template % body + def dummy(): body = "This is a dummy endpoint" return response_template % body + +def light(): + body = "{value: " + adc.read() + "}" + return response_template % body + + +def switch(): + body = "{state: " + switch_pin.value() + "}" + return response_template % body + + +def light_on(): + pin.value(0) + body = "You turned a light on!" + return response_template % body + + +def light_off(): + pin.value(1) + body = "You turned a light off!" + return response_template % body + + handlers = { 'time': time, 'dummy': dummy, + 'light_on': light_on, + 'light_off': light_off, + 'light': light, + 'switch': switch, } + def main(): s = socket.socket() ai = socket.getaddrinfo("0.0.0.0", 8080) @@ -60,10 +78,9 @@ def main(): s.bind(addr) s.listen(5) - print("Listening, connect your browser to http://:8080") + print("Listening, connect your browser to http://:8080/") while True: - sleep(1) res = s.accept() client_s = res[0] client_addr = res[1] @@ -71,19 +88,21 @@ def main(): print("Request:") print(req) - try: - path = req.decode().split("\r\n")[0].split(" ")[1] - handler = handlers[path.strip('/').split('/')[0]] - response = handler() - except KeyError: - response = response_404 - except Exception as e: - response = response_500 - print(str(e)) + # The first line of a request looks like "GET /arbitrary/path/ HTTP/1.1". + # This grabs that first line and whittles it down to just "/arbitrary/path/" + path = req.decode().split("\r\n")[0].split(" ")[1] + + # Given the path, identify the correct handler to use + handler = handlers[path.strip('/').split('/')[0]] + + response = handler() + # A handler returns an entire response in the form of a multi-line string. + # This breaks up the response into single strings, byte-encodes them, and + # joins them back together with b"\r\n". Then it sends that to the client. client_s.send(b"\r\n".join([line.encode() for line in response.split("\n")])) client_s.close() print() -main() +main() \ No newline at end of file From d311d61a660da1dbedc5893ebd6dcf1f3c745312 Mon Sep 17 00:00:00 2001 From: "matt.gummel" Date: Sun, 6 Sep 2020 22:19:15 -0700 Subject: [PATCH 4/4] added templates back --- flexible_web_server/main.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/flexible_web_server/main.py b/flexible_web_server/main.py index 58ac4eb..ac4c878 100644 --- a/flexible_web_server/main.py +++ b/flexible_web_server/main.py @@ -3,6 +3,13 @@ except: import socket +response_404 = """HTTP/1.0 404 NOT FOUND +

404 Not Found

+""" + +response_500 = """HTTP/1.0 500 INTERNAL SERVER ERROR +

500 Internal Server Error

+""" response_template = """HTTP/1.0 200 OK %s @@ -10,6 +17,8 @@ import machine import ntptime, utime from machine import RTC +from time import sleep + seconds = ntptime.time() rtc = RTC() rtc.datetime(utime.localtime(seconds)) @@ -81,21 +90,26 @@ def main(): print("Listening, connect your browser to http://:8080/") while True: + sleep(1) res = s.accept() client_s = res[0] client_addr = res[1] req = client_s.recv(4096) print("Request:") print(req) - - # The first line of a request looks like "GET /arbitrary/path/ HTTP/1.1". - # This grabs that first line and whittles it down to just "/arbitrary/path/" - path = req.decode().split("\r\n")[0].split(" ")[1] - - # Given the path, identify the correct handler to use - handler = handlers[path.strip('/').split('/')[0]] - - response = handler() + try: + # The first line of a request looks like "GET /arbitrary/path/ HTTP/1.1". + # This grabs that first line and whittles it down to just "/arbitrary/path/" + path = req.decode().split("\r\n")[0].split(" ")[1] + + # Given the path, identify the correct handler to use + handler = handlers[path.strip('/').split('/')[0]] + response = handler() + except KeyError: + response = response_404 + except Exception as e: + response = response_500 + print(str(e)) # A handler returns an entire response in the form of a multi-line string. # This breaks up the response into single strings, byte-encodes them, and