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

时间:2017-05-24 04:13:04

标签: windows ubuntu flask server virtualbox

我尝试从host ='0.0.0.0'运行它,但它仍然无法访问。我可以从我的虚拟机中ping我的Windows机器ip 192.168.1.109,但我无法从我的Windows端从ifconfig 10.0.2.15 ping我的Ubuntu VirtualMachine ip。如果有帮助,我正在使用virtualbox。

run.py

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

初始化的.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

2 个答案:

答案 0 :(得分:1)

当我在主界面中更改此行时,它对我有用

app.run()

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

答案 1 :(得分:0)

您必须重新配置来宾,以便它可以获得自己的IP地址:

<强>步骤

  
      
  1. 关闭来宾计算机。
  2.   
  3. 在来宾的VirtualBox设置中,选择“网络”并切换网络   适配器类型为“Bridged”。
  4.   
  5. 重新启动来宾。
  6.   
  7. 完成。
  8.