简体   繁体   中英

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.

Maybe the problem is that you have specified two routes but only one specifies variable name val and this is an inconsistency. I am no expert in Bottle , but you might try the following:

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

This now allows 0 or more digits after the 'd'.

By the way, your regex could also have been more simply specified as d\d* .

Update

I created the following simple program...

from bottle import route, run

@route('/stream/<val:re:d[\d]*>')
def hello(val):
    return val

run(host='localhost', port=8080, debug=True)

... and everything worked as expected. I should add that specifying two routes with a default value for argument val also works:

from bottle import route, run

@route('/stream/d>')
@route('/stream/<val:re:d[\d]+>')
def hello(val="d"):
    return val

run(host='localhost', port=8080, debug=True)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM