侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python flask 如何修改默认端口号的方法步骤

Python  /  管理员 发布于 7年前   310

场景:按照github文档上启动一个flask的app,默认是用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: offTraceback (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 * -nrwdocs/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 youdocs/deploying/wsgi-standalone.rst:38:  $ uwsgi --http 127.0.0.1:5000 --module myproject:appdocs/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 thedocs/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 thedocs/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 theflask/app.py:925:    _port = 5000flask/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中有指定。那么根据启动命令flask run 实际上是启动了一个http server,然后监听了一个本地端口,等待连接。那么看看是否有相应的参数。

renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django/flask$ flask --helpTraceback (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 --helpUsage: 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这个参数,是指定端口的,默认是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的端口就成功了,由此可知,当出现一个我们不知道该如何解决问题的时候,我们可以尝试着自己分析源码,得到想要的结果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    如何通过50行Python代码获取公众号全部文章
    下一条:
    python pandas获取csv指定行 列的操作方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客