×

flask项目部署到服务器

flask项目部署到服务器(怎么使用python flask搭建静态服务器)

admin admin 发表于2024-02-13 13:39:46 浏览27 评论0

抢沙发发表评论

这篇文章给大家聊聊关于flask项目部署到服务器,以及怎么使用python flask搭建静态服务器对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。

本文目录

怎么使用python flask搭建静态服务器

Frozen-Flask freezes a Flask application into a set of static files. The result can be hosted without any server-side software other than a traditional web server.

Note: This project used to be called Flask-Static.

Installation

Install the extension with one of the following commands:

$ easy_install Frozen-Flask

or alternatively if you have pip installed:

$ pip install Frozen-Flask

or you can get the source code from github.

Context

This documentation assumes that you already have a working Flask application. You can run it and test it with the development server:

from myapplication import appapp.run(debug=True)

Frozen-Flask is only about deployment: instead of installing Python, a WGSI server and Flask on your server, you can use Frozen-Flask to freeze your application and only have static HTML files on your server.

Getting started

Create a Freezer instance with your app object and call its freeze() method. Put that in a freeze.py script (or call it whatever you like):

from flask_frozen import Freezerfrom myapplication import appfreezer = Freezer(app)if __name__ == ’__main__’:freezer.freeze()

This will create a build directory next to your application’s static and templatesdirectories, with your application’s content frozen into  static files.

Note

Frozen-Flask considers it “owns” its build directory. By default, it will silently overwrite files in that directory, and remove those it did not create.

The configuration allows you to change the destination directory, or control what files are removed if at all.

This build will most likely be partial since Frozen-Flask can only guess so much about your application.

Finding URLs

Frozen-Flask works by simulating requests at the WSGI level and writing the responses to aptly named files. So it needs to find out which URLs exist in your application.

The following URLs can be found automatically:

  • Static files handled by Flask for your application or any of its blueprints.

  • Views with no variable parts in the URL, if they accept the GET method.

  • New in version 0.6: Results of calls to flask.url_for() made by your application in the request for another URL. In other words, if you use url_for() to create links in your application, these links will be “followed”.

  • This means that if your application has an index page at the URL / (without parameters) and every other page can be found from there by recursively following links built with url_for(), then Frozen-Flask can discover all URLs automatically and you’re done.

    Otherwise, you may need to write URL generators.

    URL generators

    Let’s say that your application looks like this:

  • @app.route(’/’)def products_list():
  •    return render_template(’index.html’, products=models.Product.all())@app.route(’/product_《int:product_id》/’)def product_details():
  •    product = models.Product.get_or_404(id=product_id)
  •    return render_template(’product.html’, product=product)
  • If, for some reason, some products pages are not linked from another page (or these links are not built by url_for()), Frozen-Flask will not find them.

    To tell Frozen-Flask about them, write an URL generator and put it after creating your Freezer instance and before calling freeze():

  • @freezer.register_generatordef product_details():
  •    for product in models.Product.all():
  •        yield {’product_id’: product.id}
  • Frozen-Flask will find the URL by calling url_for(endpoint, **values) whereendpoint is the name of the generator function and values is each dict yielded by the function.

    You can specify a different endpoint by yielding a (endpoint, values) tuple instead of just values, or you can by-pass url_for and simply yield URLs as strings.

    Also, generator functions do not have to be Python generators using yield, they can be any callable and return any iterable object.

    All of these are thus equivalent:

  • @freezer.register_generatordef product_details():  # endpoint defaults to the function name
  •    # `values` dicts
  •    yield {’product_id’: ’1’}
  •    yield {’product_id’: ’2’}@freezer.register_generatordef product_url_generator():  # Some other function name
  •    # `(endpoint, values)` tuples
  •    yield ’product_details’, {’product_id’: ’1’}
  •    yield ’product_details’, {’product_id’: ’2’}@freezer.register_generatordef product_url_generator():
  •    # URLs as strings
  •    yield ’/product_1/’
  •    yield ’/product_2/’@freezer.register_generatordef product_url_generator():
  •    # Return a list. (Any iterable type will do.)
  •    return [
  •        ’/product_1/’,
  •        # Mixing forms works too.
  •        (’product_details’, {’product_id’: ’2’}),
  •    ]
  • Generating the same URL more than once is okay, Frozen-Flask will build it only once. Having different functions with the same name is generally a bad practice, but still work here as they are only used by their decorators. In practice you will probably have a module for your views and another one for the freezer and URL generators, so having the same name is not a problem.

    Testing URL generators

    The idea behind Frozen-Flask is that you can use Flask directly to develop and test your application. However, it is also useful to test your URL generators and see that nothing is missing, before deploying to a production server.

    You can open the newly generated static HTML files in a web browser, but links probably won’t work. The FREEZER_RELATIVE_URLS configuration can fix this, but adds a visible index.html to the links. Alternatively, use the run() method to start an HTTP server on the build result, so you can check that everything is fine before uploading:

  • if __name__ == ’__main__’:
  •    freezer.run(debug=True)
  • Freezer.run() will freeze your application before serving and when the reloader kicks in. But the reloader only watches Python files, not templates or static files. Because of that, you probably want to use Freezer.run() only for testing the URL generators. For everything else use the usual app.run().

    Flask-Script may come in handy here.

    Controlling What Is Followed

    Frozen-Flask follows links automatically or with some help from URL generators. If you want to control what gets followed, then URL generators should be used with the Freezer’s with_no_argument_rules and log_url_for flags. Disabling these flags will force Frozen-Flask to use URL generators only. The combination of these three elements determines how much Frozen-Flask will follow.

    Configuration

    Frozen-Flask can be configured using Flask’s configuration system. The following configuration values are accepted:

  • FREEZER_BASE_URL

  • ***隐藏网址***

  • FREEZER_RELATIVE_URLS

  • If set to True, Frozen-Flask will patch the Jinja environment so that url_for() returns relative URLs. Defaults to False. Python code is not affected unless you use relative_url_for() explicitly. This enables the frozen site to be browsed without a web server (opening the files directly in a browser) but appends a visible index.html to URLs that would otherwise end with /.

    New in version 0.10.

  • FREEZER_DEFAULT_MIMETYPE

  • The MIME type that is assumed when it can not be determined from the filename extension. If you’re using the Apache web server, this should match the DefaultTypevalue of Apache’s configuration.  Defaults to application/octet-stream.

    New in version 0.7.

  • FREEZER_IGNORE_MIMETYPE_WARNINGS

  • If set to True, Frozen-Flask won’t show warnings if the MIME type returned from the server doesn’t match the MIME type derived from the filename extension. Defaults to False.

    New in version 0.8.

  • FREEZER_DESTINATION

  • Path to the directory where to put the generated static site. If relative, interpreted as relative to the application root, next to the static and templates directories. Defaults to build.

  • FREEZER_REMOVE_EXTRA_FILES

  • If set to True (the default), Frozen-Flask will remove files in the destination directory that were not built during the current freeze. This is intended to clean up files generated by a previous call to Freezer.freeze() that are no longer needed. Setting this to False is equivalent to setting FREEZER_DESTINATION_IGNORE to .

    New in version 0.5.

  • FREEZER_DESTINATION_IGNORE

  • A list (defaults empty) of fnmatch patterns. Files or directories in the destination that match any of the patterns are not removed, even if FREEZER_REMOVE_EXTRA_FILES is true. As in .gitignore files, patterns apply to the whole path if they contain a slash /, to each slash-separated part otherwise. For example, this could be set to [’.git

python程序怎么部署到云服务器

你需要登录云服务器管理控制台,记录被我打马赛克位置的公网ip。登录服务器windows推荐下putty进行连接远程服务器,linux和macos不需要那么麻烦,ssl就行了。putty在下图的host name中输入你的公网IP,点击open。putty界面输入密码,看不见输入不要慌,linux就是这样保护安全的。Paste_Image.png登录成功界面见下图,然后想怎么操作就怎么操作了。登陆成功界面运行我的python文件推荐FileZilla进行文件传输(不具体说明了),我上传了我的flask网站项目我的项目记住服务器同时有Python2和Python3,python运行的时候记得使用合适的版本,我用的是虚拟环境(自行百度如何创建虚拟环境)在运行python之前先安装一个screen,他可以将一个程序在后台运行sudo apt-get install screen运行python文件,成功后按下ctrl+A和ctrl+D可以隐藏。隐藏后可以输入screen -r 恢复。运行成功

怎么使用python flask搭建静态服务器的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于怎么使用python flask搭建静态服务器、怎么使用python flask搭建静态服务器的信息别忘了在本站进行查找哦。