为什么我不能从 Internet 访问这个 Rust 简单服务器? [英] Why can I not access this Rust simple server from the Internet?

查看:26
本文介绍了为什么我不能从 Internet 访问这个 Rust 简单服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下服务器:

extern crate simple_server;
use crate::simple_server::*;

fn main() {
    let host = "127.0.0.1";
    let port = "7878";

    let server = Server::new(|request, mut response| {
        println!("Request received. {} {}", request.method(), request.uri());
        println!("=============BODY=================");
        let mut v: Vec<u8> = Vec::new();
        for b in request.body().iter() {
            v.push(*b);
        }
        let body_as_str = String::from_utf8(v);

        match body_as_str {
            Ok(strr) => println!("{}", strr),
            Err(e) => println!("{}", e),
        }

        //ROUTING
        match (request.method(), request.uri().path()) {
            (&Method::GET, "/") => {
                Ok(response.body("<h1>Hi!</h1><p>Hello hacker!</p>".as_bytes().to_vec())?)
            }
            (&Method::POST, "/") => {
                Ok(response.body("<h1>Hi!</h1><p>Hello hacker!</p>".as_bytes().to_vec())?)
            }
            (_, _) => {
                response.status(StatusCode::NOT_FOUND);
                Ok(response.body("<h1>404</h1><p>Not found!<p>".as_bytes().to_vec())?)
            }
        }
    });
    println!("Listening on: {} port {}", host, port);
    server.listen(host, port);
}

我无法访问要从 Internet 转到 http://my_server_ip:7878/ 的页面.

I'm not able to access the page going to http://my_server_ip:7878/ from the Internet.

我的服务器防火墙设置允许所有在 7878 上,而且,其他简单的服务器在配置为侦听该端口时工作得很好,这让我认为这是我特定的代码问题防锈应用.运行良好的服务器的一个例子是 npm 上的静态服务器.

I have my server's firewall setup to allow all on 7878 and also, other simple servers work just fine when configured to listen on that port, which makes me think this is a code problem with my specific Rust application. An example of a server which works fine is static-server on npm.

但是,当我通过转到 "127.0.0.1:7878" 在我的构建机器上测试此应用程序时,它工作正常.

However, when I test this application on my build machine by going to "127.0.0.1:7878", it works fine.

推荐答案

通过将 "127.0.0.1" 用于 host 变量,您拒绝了任何不存在的连接来自这个地址.例如,使用 "0.0.0.0",未指定的地址.

By using "127.0.0.1" for the host variable you denied any connection that doesn't come from this address. Use "0.0.0.0", the unspecified address, for example.

这篇关于为什么我不能从 Internet 访问这个 Rust 简单服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆