简体   繁体   English

Windows主机无法访问在虚拟框中运行的烧瓶服务器

[英]flask server running in virtual box is not accessible from the windows host

I have tried running it from host='0.0.0.0' and it is still inaccessible. 我尝试从host ='0.0.0.0'运行它,但仍然无法访问。 I can ping my windows machine ip 192.168.1.109 from my virtualmachine, but I can not ping my Ubuntu VirtualMachine ip from ifconfig 10.0.2.15 from my windows side. 我可以从虚拟机ping我的Windows机器IP 192.168.1.109,但不能从Windows端从ifconfig 10.0.2.15 ping我的Ubuntu VirtualMachine ip。 I am using virtualbox if that helps. 如果有帮助,我正在使用virtualbox。

run.py 运行

#!flask/bin/python
from app import app
app.run(host='0.0.0.0',port=5000, debug=True)

init .py 初始化 .py

import os
from flask import Flask
from flask.json import JSONEncoder
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_mail import Mail
from flask_babel import Babel, lazy_gettext
from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, \
    MAIL_PASSWORD
from .momentjs import momentjs

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
lm = LoginManager()
lm.init_app(app)
lm.login_view = 'login'
lm.login_message = lazy_gettext('Please log in to access this page.')
mail = Mail(app)
babel = Babel(app)


class CustomJSONEncoder(JSONEncoder):
    """This class adds support for lazy translation texts to Flask's
    JSON encoder. This is necessary when flashing translated texts."""
    def default(self, obj):
        from speaklater import is_lazy_string
        if is_lazy_string(obj):
            try:
                return unicode(obj)  # python 2
            except NameError:
                return str(obj)  # python 3
        return super(CustomJSONEncoder, self).default(obj)

app.json_encoder = CustomJSONEncoder

if not app.debug and MAIL_SERVER != '':
    import logging
    from logging.handlers import SMTPHandler
    credentials = None
    if MAIL_USERNAME or MAIL_PASSWORD:
        credentials = (MAIL_USERNAME, MAIL_PASSWORD)
    mail_handler = SMTPHandler((MAIL_SERVER, MAIL_PORT),
                               'no-reply@' + MAIL_SERVER, ADMINS,
                               'microblog failure', credentials)
    mail_handler.setLevel(logging.ERROR)
    app.logger.addHandler(mail_handler)

if not app.debug and os.environ.get('HEROKU') is None:
    import logging
    from logging.handlers import RotatingFileHandler
    file_handler = RotatingFileHandler('tmp/microblog.log', 'a',
                                       1 * 1024 * 1024, 10)
    file_handler.setLevel(logging.INFO)
    file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
    app.logger.addHandler(file_handler)
    app.logger.setLevel(logging.INFO)
    app.logger.info('microblog startup')

if os.environ.get('HEROKU') is not None:
    import logging
    stream_handler = logging.StreamHandler()
    app.logger.addHandler(stream_handler)
    app.logger.setLevel(logging.INFO)
    app.logger.info('microblog startup')

app.jinja_env.globals['momentjs'] = momentjs

from app import views, models

It worked for me when I changed this line in the main 当我在主界面中更改此行时,它对我有用

app.run()

to

app.run(host='192.168.163.128', port=5000)

You have to reconfigure the guest so that it can get its own IP address: 您必须重新配置来宾,使其可以获取自己的IP地址:

Steps : 步骤

  1. Shutdown the guest machine. 关闭来宾计算机。
  2. In guest's VirtualBox settings,choose Network and switch the network adapter type to "Bridged". 在来宾的VirtualBox设置中,选择“网络”,然后将网络适配器类型切换为“桥接”。
  3. Restart the guest. 重新启动来宾。
  4. Done. 做完了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows上托管公开可见的Flask服务器 - How to host publicly visible flask server on windows 使用Windows与Virtual Box作为主机和VM guest虚拟机来管理Vagrant和Ansible - Use Windows with Virtual Box as Host and VM guest for managing with Vagrant and Ansible 从Windows上访问在Vagrant框上运行的rails服务器 - Access rails server running on Vagrant box from Windows 在Windows 8 Server上使用虚拟帐户运行服务 - Running service with virtual account on Windows 8 Server 从 Windows 7 上的 Linux 虚拟机访问文件 - Access files from Linux virtual box on Windows 7 Flask服务器无法在Windows中打开到本地网络,host = 0.0.0.0无法正常工作 - Flask server not open to local network in windows, host=0.0.0.0 not working 如何从 windows Z05B6053C415A2134EAD6 容器中的 .NET 应用程序连接到在主机上运行的 SQL 服务器 - How do you connect to SQL Server running on the host from a .NET app in a windows docker container 从 Windows 主机上的虚拟机打开文件 - opening file from virtual machine on Windows host machine 在主机内运行虚拟盒子时是否需要执行更新? - Do I need to perform updates when I'm running a Virtual Box inside the host? Windows7中Apache上的虚拟主机 - virtual host on Apache in windows7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM