- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我确信对此有答案,但我似乎找不到。另外,请务必注意,我是 Python 的新手。
我最近克隆了这个使用 python 和 wsgi 的 repo https://github.com/hypothesis/via用于路由。
我想要的是在 url 路径中有一个参数(没有查询字符串):
meow.com/cat_articles/:article_id # i.e. meow.com/cat_articles/677
我怎样才能做到这一点?
作为引用,我的最终目标是将我自己的路径添加到此文件:
最佳答案
如何将这样的路由添加到应用程序取决于该应用程序使用哪个或哪些库来实现 WSGI。我看到您链接到的 app.py
文件正在使用 werkzeug
(以及 static
)。
这里有一些在 werkzeug
中使用占位符进行路由的有用引用:
我几乎没有使用过 werkzeug
并且不会声称这绝对是最好的方法,但是一个选择是通过 werkzeug
添加另一个 WSGI 应用程序到 wsgi.DispatcherMiddleware
在该文件的底部调用。
这里有一些拼凑的示例代码,可让您在您共享的 app.py
文件的上下文中开始。尝试删除 this line 之后的所有内容并用此代码替换它:
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import HTTPException
my_url_map = Map([
# Note that this rule builds on the `/cat_articles` prefix used in the `DispatcherMiddleware` call further down
Rule('/<article_id>', endpoint='get_cat_article')
])
def cat_app(environ, start_response):
urls = my_url_map.bind_to_environ(environ)
try:
endpoint, args = urls.match()
except HTTPException, e:
return e(environ, start_response)
if endpoint == 'get_cat_article':
# Note that werkzeug also provides objects for building responses: http://werkzeug.pocoo.org/docs/0.14/wrappers
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Finally, a cat-centric article about {0}'.format(args['article_id'])]
else:
start_response('404 Not Found', [('Content-Type', 'text/plain')])
return ['Nothing is here...']
application = RequestHeaderSanitiser(app)
application = ResponseHeaderSanitiser(application)
application = Blocker(application)
application = UserAgentDecorator(application, 'Hypothesis-Via')
application = wsgi.DispatcherMiddleware(application, {
'/cat_articles': cat_app,
'/favicon.ico': static.Cling('static/favicon.ico'),
'/robots.txt': static.Cling('static/robots.txt'),
'/static': static.Cling('static/'),
'/static/__pywb': static.Cling(resource_filename('pywb', 'static/')),
'/static/__shared/viewer/web/viewer.html': redirect_old_viewer,
'/h': redirect_strip_matched_path,
})
使用该代码,路径 /cat_articles/plants
应该返回:
最后是一篇以猫为中心的关于植物的文章
关于python - 如何使用 wsgi 在 URL 路径中包含变量? (不是查询字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51777367/
我正在尝试了解 WSGI 的功能并需要一些帮助。 到目前为止,我知道它是一种服务器和应用程序之间的中间件,用于将不同的应用程序框架(位于服务器端)与应用程序连接,前提是相关框架具有 WSGI 适配器。
正如之前多个问题/答案中所强调的,即this和 this我已将 WSGISocketPrefix 配置为 /etc/httpd/run/wsgi /etc/httpd/run目录具有root:apac
正如之前多个问题/答案中所强调的,即this和 this我已将 WSGISocketPrefix 配置为 /etc/httpd/run/wsgi /etc/httpd/run目录具有root:apac
我刚刚开始使用协同程序,并阅读了有关 gevent 和 greenlets 的内容。为了进行测试,我通过 gevents pywsgi 模块提供了这段代码: from gevent.pywsgi im
我正在尝试了解 WSGI 的工作原理。我知道我可以阅读规范,但我仍然想知道如何创建 spawning应用?一个完整的“ Hello World ”。 有人可以给我举个例子吗?有了一切,文件命名,创建模
我正在构建一个简单的 Web 服务,要求对所有请求进行签名。签名哈希是使用包括请求正文的请求数据生成的。我的愿望是拥有一个验证请求签名的中间件组件,如果签名无效则以错误响应。问题是中间件需要使用 en
为什么会出现此错误? Daemon process called 'dom_server' cannot be accessed by this WSGI application: /home/sta
HTTP格式 HTTP GET请求的格式: ? 1
我读过很多类似 this one 的帖子详细说明如何使用 WSGI 动态返回图像。但是,我看到的所有示例都是以二进制格式打开图像,读取它然后返回该数据(这对我来说很好用)。 我一直在尝试使用内存中的
我正在尝试使用 Apache 为我的网页提供服务,并且想知道 wsgi.py 和 django.wsgi 之间的区别。 两者都有文档,但我不确定应该实现哪一个。我在 Django 1.4、Apache
我正在尝试使用 uwsgi 运行 Django 应用程序。我发现的大多数指令都引用了“--wsgi-file”和“--module”来指定应用程序,但是“uwsgi”没有提到这些选项,当我尝试使用它们
我对以下术语感到非常困惑, 1.wsgi 2.python_wsgi 3.wsgi服务器 4. mod_wsgi 5. python web服务器网关接口(interface)(PEP3333) 这些
我通过如下设置 apache 服务器在 apache2/Mac OS X 上使用 mod_wsgi。 Order allow,deny Allow from all WSGIScri
对于我们的网络服务,我编写了一些逻辑来防止 multipart/form-data POST 大于 4mb。 它归结为以下内容(我已经剥离了所有 WebOb 用法并将其简化为普通的 WSGI 代码):
我有一个 Flask Web 服务器,为使用 Flask-login 的用户进行基本帐户身份验证。如果我自己运行网络服务器,帐户身份验证将按预期工作。使用 Apache 和 mod-wsgi 运行 F
我在 centos 6.9 机器上使用 httpd 和 mod_wsgi 和 httpd 这里是相关文件,我正在尝试在 apache 2.15 版本上部署 django,尝试谷歌但无法解决问题,任何帮
我将在虚拟主机的 ubuntu 服务器上设置一个 django 应用程序。 我已经配置了我的虚拟主机,所以出现了一些问题! 但是由于您在此处看到的一些问题,它仍然给我 500 内部错误: mod_ws
我有一个相当简单、朴素的 Python/WSGI/Pyramid 网络服务器。 它在使用 pyramid.config.Configurator().make_wsgi_app() 构建的服务器上使用
我试图在 Amazon 的 EC2 实例上发布我的网站,但我一直收到 500 错误。我真的不知道为什么。 //日志文件 [Sun Feb 17 23:12:48.066802 2013] mo
我正在尝试在 ubuntu 上的 apache2 上安装 mod_wsgi。所以我安装了 libapache2-mod-wsgi 包,我用 a2enmod 激活了他。 我有一个网站 (language
我是一名优秀的程序员,十分优秀!