是否可以通过HttpRequest参数获取IP地址?
这是我的代码:
#[get("/collect")]
pub async fn collect(req: HttpRequest) -> impl Responder {
println!("collect {:?}", req);
HttpResponse::Ok()
}[dependencies]
actix-web = "3"发布于 2021-04-08 00:17:46
如果服务前面没有代理,应该可以从请求peer_addr()中检索到它。
否则,您可以检索请求connection_info(),并从中检索realip_remote_addr()。
示例:
#[get("/collect")]
pub async fn collect(req: HttpRequest) -> impl Responder {
if let Some(val) = req.peer_addr() {
println!("Address {:?}", val.ip());
};
HttpResponse::Ok()
}https://stackoverflow.com/questions/66989780
复制相似问题