コード例 #1
0
    def test_empty_request(self):
        self._mock()

        app = Microdot()

        mock_socket.clear_requests()
        fd = mock_socket.FakeStream(b'\n')
        mock_socket._requests.append(fd)
        self._add_shutdown(app)
        app.run()
        self.assertTrue(fd.response.startswith(b'HTTP/1.0 400 N/A\r\n'))
        self.assertIn(b'Content-Length: 11\r\n', fd.response)
        self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
        self.assertTrue(fd.response.endswith(b'\r\n\r\nBad request'))

        self._unmock()
コード例 #2
0
    def test_400_handler(self):
        self._mock()

        app = Microdot()

        @app.errorhandler(400)
        def handle_400(req):
            return '400'

        mock_socket.clear_requests()
        fd = mock_socket.FakeStream(b'\n')
        mock_socket._requests.append(fd)
        self._add_shutdown(app)
        app.run()
        self.assertTrue(fd.response.startswith(b'HTTP/1.0 200 OK\r\n'))
        self.assertIn(b'Content-Length: 3\r\n', fd.response)
        self.assertIn(b'Content-Type: text/plain\r\n', fd.response)
        self.assertTrue(fd.response.endswith(b'\r\n\r\n400'))

        self._unmock()
コード例 #3
0
ファイル: mem.py プロジェクト: miguelgrinberg/microdot
from microdot import Microdot

app = Microdot()


@app.get('/')
def index(req):
    return {'hello': 'world'}


app.run()
コード例 #4
0
    def update_config(self):
        _thread.start_new_thread(self.update_reset_monitor, ())
        from microdot import Microdot, redirect, send_file, Response
        app = Microdot()

        ap_ssid = 'ChickenCoup-ConfigMode'
        ap_password = '******'

        ap = network.WLAN(network.AP_IF)
        ap.active(True)
        ap.config(essid=ap_ssid, password=ap_password)

        @app.route("/", methods=['GET', 'POST'])
        def index(request):
            #form_cookie= None
            #message_cookie = None
            if request.method == "GET":
                #print(dir(request))
                #print(request.form)
                #print(request.headers)
                return Response(body=self.build_html_form(),
                                headers={"Content-Type": "text/html"})
            elif request.method == "POST":
                if request.form.get('save', None):
                    ## they clicked save config. write the dict to flash as a json file...
                    print(request.form)
                    new_config = {
                        "wifi": {
                            "ssid": request.form['ssid'],
                            "passphrase": request.form['passphrase']
                        },
                        "location": {
                            "lat": request.form['lat'],
                            "lng": request.form['lng']
                        },
                        "time": {
                            "sunrise_offset": request.form['sunrise_offset'],
                            "sunset_offset": request.form['sunset_offset']
                        },
                        "pushover": {
                            "app_token": request.form['app_token'],
                            "group_key": request.form['group_key']
                        },
                        "motor_tuning": {
                            "motor_min": request.form['motor_min'],
                            "motor_max": request.form['motor_max'],
                            "ramp_steps": request.form['ramp_steps'],
                            "ramp_time": request.form['ramp_time'],
                        }
                    }

                    print(new_config)
                    with open("config.json", 'w', encoding='utf-8') as f:
                        print("Saving configuration to config.json...")
                        f.write(json.dumps(new_config))
                        print("Done!")

                    self.json_config = new_config
                    return Response(body=self.build_html_form(
                        message="Updated Configuration!"),
                                    headers={"Content-Type": "text/html"})

                elif request.form.get('reset', None):
                    ## They clicked on reset! Reset the device!
                    self.update_reset_scheduled = True
                    return send_file('reset.html')

        app.run(debug=True)
コード例 #5
0
ファイル: server.py プロジェクト: Forestierr/Light_trainer
        print("received m-2 : ", request.form.get('on-off'))
        print("min s : ", request.form.get('min-s'))
        print("max s : ", request.form.get('max-s'))
        print("colors : ", request.form.get("colors"))
        print("light : ", request.form.get("light"))

    response = send_file("./templates/mode-2.html")
    return response


@app.route('/mode-3', methods=['GET', 'POST'])
def mode3(request):

    if request.method == 'POST':
        print("received m-3 : ", request.form.get('on-off'))
        print("time : ", request.form.get('time'))
        print("colors : ", request.form.get("colors"))

    response = send_file("./templates/mode-3.html")
    return response


@app.before_request
def before_request(request):
    pass


if __name__ == "__main__":
    print("start server.")
    app.run(host="0.0.0.0", port=80, debug=True)
コード例 #6
0
ファイル: static.py プロジェクト: miguelgrinberg/microdot
from microdot import Microdot, send_file

app = Microdot()


@app.route('/')
def index(request):
    return send_file('static/index.html')


@app.route('/static/<path:path>')
def static(request, path):
    if '..' in path:
        # directory traversal is not allowed
        return 'Not found', 404
    return send_file('static/' + path)


app.run(debug=True)
コード例 #7
0
@app.route("/static/wesp32-logo.jpg")
def logo(request):
    return send_file('static/wesp32-logo.jpg')


# Update light
@app.route("/light")
def light(request):
    if 'level' in request.args:
        ledpwm.duty(int(request.args['level']))
    return str(ledpwm.duty())


# Memory info
@app.route("/memory")
def light(request):
    if 'gc' in request.args or 'collect' in request.args:
        print("Garbage collect")
        gc.collect()
    return str(gc.mem_free())


# Wait for network
while lan.ifconfig()[0] == '0.0.0.0':
    print("Waiting for network connection...")
    time.sleep(2)
print("Network connection established, starting Microdot server.")

# Run Microdot server
app.run(host=lan.ifconfig()[0], port=80)