在 Sinatra 中获取客户端的 IP 地址?

发布于 2024-08-02 10:56:06 字数 151 浏览 2 评论 0原文

这是一个非常简单的问题,但我在任何地方都找不到任何提及这一点的信息。

我如何从 Sinatra 中获取客户端的 IP 地址?

get '/' do
    "Your IP address is #{....}"
end

This is a really simple question, but I cannot find any mention of this, anywhere..

How do I get the client's IP address from in Sinatra?

get '/' do
    "Your IP address is #{....}"
end

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

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

发布评论

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

评论(2

错々过的事 2024-08-09 10:56:06

Sinatra 提供了一个 request 对象,这是您应该使用的客户端请求数据的接口。

使用 request.ip 是查找客户端 IP 地址的首选方法:

get '/' do
  "Your IP address is #{request.ip}"
end

Sinatra provides a request object, which is the interface to the client request data that you should be using.

Using request.ip is the preferred method to find the client's IP address:

get '/' do
  "Your IP address is #{request.ip}"
end
许仙没带伞 2024-08-09 10:56:06

无论如何,我要发布答案..所以:

get '/' do
"Your IP address is #{ @env['REMOTE_ADDR'] }"
end

Sinatra 使用 Rack ::Request API,因此您可以使用其中的许多可用内容。
还有一个指向 Sinatra 文档 的链接。

I was coming to post the answer anyway.. so:

get '/' do
"Your IP address is #{ @env['REMOTE_ADDR'] }"
end

Sinatra uses the Rack::Request API, so you can use a lot of things available in it.
Also a link to the Sinatra doc's.

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