Search code examples
javascriptpythonregexroutesbottle

python bottle route with regex not matching - 404 error


I'm trying to setup a python camera app that streams to a browser and am using bottle. My javascript in the index.html is this:

<div class="panel-body"> <img src="stream/d" onload="imageRefresh(this, 30);" style="max-width: 100%;">

The imageRefresh function uses setTimeout and it takes the image url and appends a time value to it so that it is unique and the browser will fire off a request to the server. This process worked fine with a server I found on the internet called microwebsrv which is on github at: https://github.com/jczic/MicroWebSrv; however, I'm trying to move over to bottle on my raspberry pi. My routes for bottle look like this:

@route('/stream/d')
@route('/stream/<val:re:d[\d]+>')
def _httpStream(val="d")
  print("val: %s" % val)
  (rest of code here)

However, after the very first image is served (I see the image flash on the browser screen and in on the developer console I see http://192.168.0.18:8080/stream/d; I also see the "GET /stream/d" on the rPi terminal from which I launched the app) then the next get request which looks like: http://192.168.0.18:8080/stream/d1633472974918 fails with a 404 error, I presume because the route isn't matching? I have also tried by simply using a route like: @route(/stream/) but I still get the same error. The imageRefresh function updates the img.src like this:

img.src=http + '/d' + d.getTime();

I've read the bottle docs a number of times and I've tested my regex with a "regex Regular Expression Tool" I installed from the Windows store. The regex tool shows it matches.


Solution

  • I finally figured it out! Firstly, I put this particular route first in the program. Then, I changed the routes to be:

    
        @app.route('/stream/<time>')
        
        @app.route('/stream/<time:re:d.*>')
    
    

    This took care of the 404 problem and now I'm able to stream images captured from a camera. Thanks for the suggestions above, I also upgraded to 0.13-dev and changed my simplified my regex.