设置 4 个“漂亮”的资源占用最少的方法网址?

发布于 2024-08-16 07:32:40 字数 614 浏览 2 评论 0原文

我正在计划用 Python 编写一个供我自己使用的 API。也许有一天我会让它免费访问,但现在我只打算在我的爱好网站上使用它。在此阶段,我需要一些关于如何为接收 GET 和 POST 请求的文件设置 URL 的建议。

假设我的一个文件名为 function_A.py 并以这种方式使用:

www.example.com/api/function_A.py?a=something&k=other+thing

我的问题是,如何以最不占用资源的方式在 API 中设置“漂亮”的 URL?

我发现大多数 API 都有典型的 URL 格式,例如 http://www.example.com/api/read 而不是 http://www.example.com/api/read.py

我的选择可能仅限于使用 Django 的 mod_rewrite v/s urls.py - 还是还有其他更简单的选择?

这是针对后端/API 的,我宁愿将开销保持在最低限度。我只想以这种方式处理 4 个 URL,并且不希望每次调用 URL 时都出现正则表达式。

I'm planning an API for my own use, in Python. Perhaps someday I will make it freely accessible, but now I'm only planning to use it on my hobby site. At this stage I need some advice on how to set up the URLs, for files that receive both GET and POST requests.

Suppose one of my files is called function_A.py and used in this fashion:

www.example.com/api/function_A.py?a=something&k=other+thing

My question is, how does one set up 'pretty' URLs in an API, in the least resource-intensive way?

I see most APIs have a typical URL format like http://www.example.com/api/read instead of http://www.example.com/api/read.py

My options are probably limited to mod_rewrite v/s urls.py using Django - or is there another simpler option?

This is for the backend/API and I'd rather keep overhead to a minimum. I only want to handle 4 URLs in this fashion, and do not want to have a regex occur every time the URL is called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

成熟稳重的好男人 2024-08-23 07:32:40

MYYN 是正确的。使用 Ruby on Rails 或 Django 等框架是创建 RESTful API 的最简单方法。

但是,如果您不想使用框架,则可以使用 Apache 中的 mod_rewrite 执行相同的操作。事实上,这正是大多数在 Apache 上运行的框架/应用程序所做的事情。

例如,Wordpress 使用如下所示的 .htaccess 文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

这会将所有请求定向到 index.php,在那里可以对它们进行解析和处理。

像 RoR 或 Django 这样的框架实际上做同样的事情:所有请求都被重定向到单个文件/类/函数。

The MYYN is correct. Using a framework such as Ruby on Rails or Django is the easiest way to create a RESTful API.

However, if you don't want to use a framework, you can do the same thing using mod_rewrite in Apache. In fact, this is precisely what most frameworks/applications running on Apache do.

For example, Wordpress uses an .htaccess file like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This directs all requests to index.php, where they can be parsed and processed.

Frameworks like RoR or Django do practically the same thing: all requests are redirected to a single file/class/function.

薄情伤 2024-08-23 07:32:40

我的猜测是,具有漂亮 URL(又名 REST)的 Web 服务 API 是通过某种 Web 框架(Werkzeug、web.py、django、pylons、cherrypy、plain wsgi,...)构建的。

您所指的模块负责 url 匹配/处理以确定请求的正确路由。

从后者的文档中:

路由解决了 Web 开发中经常出现的一个有趣问题,即如何将 URL 映射到应用程序的操作?也就是说,你怎么说应该通过 /blog/2008/01/08 访问它,而 /login 应该这样做?很多Web框架都有固定的调度系统;例如,/A/B/C表示读取B目录下的文件C,或者调用C方法模块 AB 中的类 B。这些工作正常,直到您需要重构代码并意识到移动方法会更改其公共 URL 并使用户的书签无效。同样,如果您想重新组织您的网址并将一个部分变成一个小部分,您必须更改您经过仔细测试的逻辑代码。

My guess is, that web service APIs with pretty URLs (aka REST) are built via some kind of webframework (Werkzeug, web.py, django, pylons, cherrypy, plain wsgi, ...).

The module you are referring to takes care of url matching/handling to determine the correct route for a request.

From the documentation of the latter:

Routes tackles an interesting problem that comes up frequently in web development, how do you map URLs to your application’s actions? That is, how do you say that this should be accessed as /blog/2008/01/08, and /login should do that? Many web frameworks have a fixed dispatching system; e.g., /A/B/C means to read file C in directory B, or to call method C of class B in module A.B. These work fine until you need to refactor your code and realize that moving a method changes its public URL and invalidates users’ bookmarks. Likewise, if you want to reorganize your URLs and make a section into a subsection, you have to change your carefully-tested logic code.

赢得她心 2024-08-23 07:32:40

是的,.htaccess 是您可以从 Apache Server 中漂亮的外部 URL 映射到您的服务平台接受的一些肮脏 URL 的方式。

一些复杂的平台也确实有办法生成更好的 URL。

Yes, .htaccess is the way that you can map from a pretty, external, URL in Apache Server to some grubby URL that your service platform accepts.

Some complex platforms do have means of producing nicer URLs as well.

雨落□心尘 2024-08-23 07:32:40

没有魔法。您恰好正在使用一个在设计时就考虑到基于文件的 URL 的 Web 服务器。

大多数站点不再使用基于文件的 URL,而是自行调度。这就是 Django 所做的事情,这就是 ASP.NET MVC 所做的事情。

您需要查找的是 Apache 的 mod_rewrite 。它将允许您将非基于文件的 URL 映射到 Python 文件。

或者,考虑使用已经执行此操作的 Python Web 框架。 Web.pyDjango 很好。

There's no magic. You just so happen to be using a web server that was designed with file-based URLs in mind.

Most sites don't bother with file-based URLs anymore and instead do their own dispatch. This is what Django does, this is what ASP.NET MVC does.

What you need to lookup is mod_rewrite for Apache. It will allow you to map non file-based URLs to your Python files.

Alternatively, look at using a Python web framework that already does this. Web.py and Django are nice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文