简体   繁体   English

python 瓶路线与正则表达式不匹配 - 404 错误

[英]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.我正在尝试设置一个 python 相机应用程序,该应用程序可以流式传输到浏览器并正在使用瓶子。 My javascript in the index.html is this:我在索引中的 javascript.html 是这样的:

<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. imageRefresh function 使用 setTimeout 并获取图像 url 并向其附加时间值,以便它是唯一的,并且浏览器将向服务器发出请求。 This process worked fine with a server I found on the internet called microwebsrv which is on github at: https://github.com/jczic/MicroWebSrv;此过程与我在 Internet 上找到的名为 microwebsrv 的服务器运行良好,该服务器位于 github 上: 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?但是,在提供第一个图像之后(我在浏览器屏幕和开发人员控制台上看到图像 flash 我看到http://192.168.0.18:8080/stream/d;我还看到“GET /stream/ d" 在我启动应用程序的 rPi 终端上)然后下一个获取请求,如下所示: http://192.168.0.18:8080/stream/d1633472974918失败并出现 404 错误,我想是因为路由不匹配? I have also tried by simply using a route like: @route(/stream/) but I still get the same error.我也尝试过简单地使用类似的路由:@route(/stream/) 但我仍然得到同样的错误。 The imageRefresh function updates the img.src like this: imageRefresh function 像这样更新 img.src:

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.我已经多次阅读瓶子文档,并使用从 Windows 商店安装的“正则表达式正则表达式工具”测试了我的正则表达式。 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.也许问题是您指定了两条路线,但只有一条指定了变量名val ,这是不一致的。 I am no expert in Bottle , but you might try the following:我不是Bottle方面的专家,但您可以尝试以下方法:

@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'.这现在允许在 'd' 之后有 0 位或更多位。

By the way, your regex could also have been more simply specified as d\d* .顺便说一句,您的正则表达式也可以更简单地指定为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:我应该补充一点,为参数val指定两个具有默认值的路由也可以:

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.这解决了 404 问题,现在我能够从相机捕获的 stream 图像。 Thanks for the suggestions above, I also upgraded to 0.13-dev and changed my simplified my regex.感谢上述建议,我还升级到 0.13-dev 并更改了简化的正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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