宿主机无法访问虚拟机中启动的Flask服务,默认访问路径为:http://127.0.0.1:5000/

一、问题回溯

虚拟机中通过“flask run”启动web服务,默认访问路径为:http://127.0.0.1:5000/,宿主机无法访问。

二、问题分析

1、宿主机与虚拟机之间是否能够ping通;——可以,如果不行需要修改虚拟机的网络设置,基本都是改为桥接模式。

2、宿主机是否可以通过SecureCRT、Xshell等工具连接服务器(虚拟机);——可以

3、由此基本可以确认是防火墙的问题了;——确实如此,但是问题不能完全解决

4、新问题,IP:127.0.0.1,宿主机会默认访问自己本地,启动Flask时,需要带参数启动。

三、问题解决:

1、防火墙控制

1.1 关闭防火墙:(不推荐,关闭防火墙毕竟不安全)

禁用: systemctl stop firewalld

查看状态: systemctl status firewalld

启动: systemctl start firewalld

停止(重启后依然关闭): systemctl disable firewalld

1.2 添加防火墙白名单(推荐)

root用户添加5000端口的访问权限,这里添加后永久生效

[root@localhost ~]# firewall-cmd --zone=public --add-port=5000/tcp --permanent    

[root@localhost ~]# firewall-cmd --reload

查看5000端口访问权限情况

[root@localhost ~]# firewall-cmd --zone=public --query-port=5000/tcp

关闭5000访问权限

[root@localhost ~]# firewall-cmd --zone= public --remove-port=5000/tcp --permanent

2、带参数据启动

# 172.20.10.4为虚拟机IP

[johnny@localhost flask]$ flask run -h 172.20.10.4 -p 5000(其中IP为虚拟机的IP)

(venv) [johnny@localhost flask]$ flask run -h 172.20.10.4 -p 5000

要在Flask应用中创建一个简单的用户登录页面,并通过浏览器访问,你可以按照以下步骤操作: 1. 首先,安装Flask库,如果尚未安装,可以使用pip命令: ``` pip install flask ``` 2. 创建一个名为`app.py`的文件,用于设置基本的Flask应用程序结构: ```python from flask import Flask, render_template, request, redirect, url_for from werkzeug.security import check_password_hash app = Flask(__name__) # 假设有一个简单的用户数据库,这里仅为演示,实际应用应存储在数据库中 users = { 'admin': { 'password': check_password_hash('hashed_password', 'admin123') } } @app.route('/', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') if username in users and check_password_hash(users[username]['password'], password): return redirect(url_for('success')) else: return "用户名或密码错误" # 显示登录表单 return render_template('login.html', title='登录') @app.route('/success') def success(): return "登录成功!" if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000) ``` 在这个例子中,我们创建了两个路由:一个用于显示登录页面(`/`),另一个用于处理登录请求(`/login`)。`login`函数会检查提交的用户名和密码是否匹配,然后重定向到成功页面。 3. 创建一个`templates`目录,里面包含一个名为`login.html`的模板文件,它将包含HTML表单,如下所示: ```html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <h1>登录</h1> {% with messages = get_flashed_messages() %} {% if messages %} <ul> {% for message in messages %} <li>{{ message }}</li> {% endfor %} </ul> {% endif %} {% endwith %} <form method="post" action="{{ url_for('login') }}"> <label for="username">用户名:</label><br> <input type="text" id="username" name="username"><br> <label for="password">密码:</label><br> <input type="password" id="password" name="password"><br> <input type="submit" value="登录"> </form> </body> </html> ``` 4. 现在你可以运行`app.py`,在浏览器中打开`http://127.0.0.1:5000/`,应该能看到登录页面。填写用户名和密码后点击登录,如果输入正确则会被重定向到成功页面。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值