欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python flask 如何修改默認(rèn)端口號的方法步驟

 更新時間:2019年07月12日 10:05:46   作者:都是一家人  
這篇文章主要介紹了python flask 如何修改默認(rèn)端口號的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

場景:按照github文檔上啟動一個flask的app,默認(rèn)是用5000端口,如果5000端口被占用,啟動失敗。

樣例代碼:

from flask import Flask
 
app = Flask(__name__)
 
@app.route('/')
def hello():
  return 'Hello, World!' 

啟動的腳本:

$ env FLASK_APP=hello.py flask run 

出錯信息如下:

renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django$ env FLASK_APP=index.py flask run
 * Serving Flask app "index.py"
 * Environment: production
  WARNING: Do not use the development server in a production environment.
  Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
 File "/usr/local/bin/flask", line 11, in <module>
  sys.exit(main())
 File "/usr/local/lib/python2.7/dist-packages/flask/cli.py", line 894, in main
  cli.main(args=args, prog_name=name)
 File "/usr/local/lib/python2.7/dist-packages/flask/cli.py", line 557, in main
  return super(FlaskGroup, self).main(*args, **kwargs)
 File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 717, in main
  rv = self.invoke(ctx)
 File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1137, in invoke
  return _process_result(sub_ctx.command.invoke(sub_ctx))
 File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 956, in invoke
  return ctx.invoke(self.callback, **ctx.params)
 File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 555, in invoke
  return callback(*args, **kwargs)
 File "/usr/local/lib/python2.7/dist-packages/click/decorators.py", line 64, in new_func
  return ctx.invoke(f, obj, *args, **kwargs)
 File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 555, in invoke
  return callback(*args, **kwargs)
 File "/usr/local/lib/python2.7/dist-packages/flask/cli.py", line 771, in run_command
  threaded=with_threads, ssl_context=cert)
 File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 814, in run_simple
  inner()
 File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 774, in inner
  fd=fd)
 File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 660, in make_server
  passthrough_errors, ssl_context, fd=fd)
 File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 577, in __init__
  self.address_family), handler)
 File "/usr/lib/python2.7/SocketServer.py", line 417, in __init__
  self.server_bind()
 File "/usr/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
  SocketServer.TCPServer.server_bind(self)
 File "/usr/lib/python2.7/SocketServer.py", line 431, in server_bind
  self.socket.bind(self.server_address)
 File "/usr/lib/python2.7/socket.py", line 228, in meth
  return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use 

問題:

那么該怎么指定新的端口呢?又如何查看5000端口指定的位置呢?

源碼分析,首先clone github上的flask框架,然后直接grep命令查看5000的位置。

renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django/flask$ grep 5000 * -nrw
docs/patterns/appdispatch.rst:28:  run_simple('localhost', 5000, application, use_reloader=True)
docs/patterns/appdispatch.rst:48:    run_simple('localhost', 5000, app,
docs/config.rst:417:   * Running on http://127.0.0.1:5000/
docs/config.rst:453:   * Running on http://127.0.0.1:5000/
docs/quickstart.rst:51:   * Running on http://127.0.0.1:5000/
docs/quickstart.rst:66:   * Running on http://127.0.0.1:5000/
docs/quickstart.rst:72:Now head over to `http://127.0.0.1:5000/ <http://127.0.0.1:5000/>`_, and you
docs/deploying/wsgi-standalone.rst:38:  $ uwsgi --http 127.0.0.1:5000 --module myproject:app
docs/deploying/wsgi-standalone.rst:55:  http_server = WSGIServer(('', 5000), app)
docs/server.rst:26:*http://localhost:5000/*.
docs/cli.rst:97:   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
docs/cli.rst:143:   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
docs/tutorial/static.rst:58:Go to http://127.0.0.1:5000/auth/login and the page should look like the
docs/tutorial/factory.rst:169:   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
docs/tutorial/factory.rst:174:Visit http://127.0.0.1:5000/hello in a browser and you should see the
docs/tutorial/templates.rst:174:then go to http://127.0.0.1:5000/auth/register.
examples/javascript/README.rst:39:Open http://127.0.0.1:5000 in a browser.
examples/tutorial/README.rst:62:Open http://127.0.0.1:5000 in a browser.
<strong>flask/app.py:878:    :param port: the port of the webserver. Defaults to ``5000`` or the
flask/app.py:925:    _port = 5000
flask/cli.py:738:@click.option('--port', '-p', default=5000,</strong>
README.rst:43:   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
tests/test_basic.py:369:    SERVER_NAME='localhost:5000',
tests/test_basic.py:377:  rv = client.get('/', 'http://localhost:5000/')
tests/test_basic.py:385:    SERVER_NAME='127.0.0.1:5000',
tests/test_basic.py:393:  rv = client.get('/', 'http://127.0.0.1:5000/')
tests/test_basic.py:1448:    SERVER_NAME='localhost.localdomain:5000'
tests/test_basic.py:1463:  rv = client.get('/', 'http://localhost.localdomain:5000')
tests/test_basic.py:1466:  rv = client.get('/', 'https://localhost.localdomain:5000')
tests/test_reqctx.py:72:    SERVER_NAME='localhost.localdomain:5000'
tests/test_reqctx.py:85:        'http://localhost.localdomain:5000/'
tests/test_reqctx.py:89:        'http://foo.localhost.localdomain:5000/'
tests/test_reqctx.py:97:      "('localhost.localdomain:5000') does not match the " 

可以看到在flask/app.py 以及cli.py中有指定。那么根據(jù)啟動命令flask run 實際上是啟動了一個http server,然后監(jiān)聽了一個本地端口,等待連接。那么看看是否有相應(yīng)的參數(shù)。

renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django/flask$ flask --help
Traceback (most recent call last):
 File "/usr/local/lib/python2.7/dist-packages/flask/cli.py", line 529, in list_commands
  rv.update(info.load_app().cli.list_commands(ctx))
 File "/usr/local/lib/python2.7/dist-packages/flask/cli.py", line 384, in load_app
  'Could not locate a Flask application. You did not provide '
NoAppException: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
Usage: flask [OPTIONS] COMMAND [ARGS]...
 
 A general utility script for Flask applications.
 
 Provides commands from Flask, extensions, and the application. Loads the
 application defined in the FLASK_APP environment variable, or from a
 wsgi.py file. Setting the FLASK_ENV environment variable to 'development'
 will enable debug mode.
 
  $ export FLASK_APP=hello.py
  $ export FLASK_ENV=development
  $ flask run
 
Options:
 --version Show the flask version
 --help   Show this message and exit.
 
Commands:
 routes Show the routes for the app.
 run   Runs a development server.
 shell  Runs a shell in the app context.
renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django/flask$ flask run --help
Usage: flask run [OPTIONS]
 
 Run a local development server.
 
 This server is for development purposes only. It does not provide the
 stability, security, or performance of production WSGI servers.
 
 The reloader and debugger are enabled by default if FLASK_ENV=development
 or FLASK_DEBUG=1.
 
Options:
 -h, --host TEXT         The interface to bind to.
 <strong> -p, --port INTEGER       The port to bind to.</strong>
 --cert PATH           Specify a certificate file to use HTTPS.
 --key FILE           The key file to use when specifying a
                 certificate.
 --reload / --no-reload     Enable or disable the reloader. By default
                 the reloader is active if debug is enabled.
 --debugger / --no-debugger   Enable or disable the debugger. By default
                 the debugger is active if debug is enabled.
 --eager-loading / --lazy-loader
                 Enable or disable eager loading. By default
                 eager loading is enabled if the reloader is
                 disabled.
 --with-threads / --without-threads
                 Enable or disable multithreading.
 --help             Show this message and exit. 

可以看到有-p這個參數(shù),是指定端口的,默認(rèn)是5000,那么嘗試修改一下。

renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django$ env FLASK_APP=index.py flask run -p 5001
 * Serving Flask app "index.py"
 * Environment: production
  WARNING: Do not use the development server in a production environment.
  Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5001/ (Press CTRL+C to quit) 

還一個5001的端口就成功了,由此可知,當(dāng)出現(xiàn)一個我們不知道該如何解決問題的時候,我們可以嘗試著自己分析源碼,得到想要的結(jié)果。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python構(gòu)建簡單線性回歸模型

    Python構(gòu)建簡單線性回歸模型

    這篇文章主要介紹了Python構(gòu)建簡單線性回歸模型,線性回歸表示發(fā)現(xiàn)函數(shù)使用線性組合表示輸入變量。簡單線性回歸很容易理解,使用了基本的回歸技術(shù),一旦理解了這些基本概念,可以更好地學(xué)習(xí)其他類型的回歸模型
    2022-08-08
  • python清理子進(jìn)程機制剖析

    python清理子進(jìn)程機制剖析

    python的機制會自動清理已經(jīng)完成任務(wù)的子進(jìn)程的,下面通過本文給大家分享python清理子進(jìn)程機制剖析,需要的朋友參考下吧
    2017-11-11
  • Python日志模塊logging簡介

    Python日志模塊logging簡介

    這篇文章主要介紹了Python日志模塊logging簡介,本文講解了Logger、Handler、Formatter、日志配置管理、通過文件配置管理日志等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Python魔法方法功能與用法簡介

    Python魔法方法功能與用法簡介

    這篇文章主要介紹了Python魔法方法功能與用法,結(jié)合具體實例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計中魔法方法的概念、功能、原理、用法及相關(guān)操作注意事項,需要的朋友可以參考下
    2019-04-04
  • Python關(guān)于print的操作(倒計時、轉(zhuǎn)圈顯示、進(jìn)度條)

    Python關(guān)于print的操作(倒計時、轉(zhuǎn)圈顯示、進(jìn)度條)

    這篇文章主要介紹了Python關(guān)于print的操作(倒計時、轉(zhuǎn)圈顯示、進(jìn)度條),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • python枚舉類型定義與使用講解

    python枚舉類型定義與使用講解

    在python中枚舉是一種類(Enum,IntEnum),存放在enum模塊中。枚舉類型可以給一組標(biāo)簽賦予一組特定的值,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • celery實現(xiàn)動態(tài)設(shè)置定時任務(wù)

    celery實現(xiàn)動態(tài)設(shè)置定時任務(wù)

    這篇文章主要為大家詳細(xì)介紹了celery實現(xiàn)動態(tài)設(shè)置定時任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • Python并發(fā)編程線程消息通信機制詳解

    Python并發(fā)編程線程消息通信機制詳解

    這篇文章主要為大家介紹了Python并發(fā)編程之線程消息通信機制的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10
  • Python?multiprocessing.value實現(xiàn)多進(jìn)程數(shù)據(jù)共享的示例

    Python?multiprocessing.value實現(xiàn)多進(jìn)程數(shù)據(jù)共享的示例

    本文介紹了Python中的multiprocessing.value,通過示例代碼展示了如何使用這個類實現(xiàn)多進(jìn)程數(shù)據(jù)共享,以及使用場景和注意事項等內(nèi)容,感興趣的朋友跟隨小編一起看看吧
    2023-07-07
  • Python編譯過程和執(zhí)行原理解析

    Python編譯過程和執(zhí)行原理解析

    這篇文章主要介紹了Python編譯過程和執(zhí)行原理解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07

最新評論