场景:按照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: 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.
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,

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 --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.
-p, --port INTEGER The port to bind to.
--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的端口就成功了,由此可知,当出现一个我们不知道该如何解决问题的时候,我们可以尝试着自己分析源码,得到想要的结果。

python flask 如何修改默认端口号的更多相关文章

  1. SpringBoot修改默认端口号

    SpringBoot修改默认端口号 server.port=8088 学习了:https://blog.csdn.net/zknxx/article/details/53433592 java -ja ...

  2. MySql修改默认端口号,修改my.ini的端口号

    MySql默认端口号为3306,如果安装多个或者冲突需要修改端口号,修改my.ini的端口号就可以了,文件一般情况下在安装目录下.下面是具体说明: 方法/步骤 先在服务里停止mysql的服务器,再找到 ...

  3. react项目和next项目修改默认端口号

    creat-react-app生成的项目默认端口号是3000,如下可以更改: 在package.json中修改 "start":"react-scripts start& ...

  4. SpringBoot修改默认端口号,session超时时间

    有时候我们可能需要启动不止一个SpringBoot,而SpringBoot默认的端口号是8080,所以这时候我们就需要修改SpringBoot的默认端口了.修改SpringBoot的默认端口有两种方式 ...

  5. PostgreSQL - 修改默认端口号

    升级PostgreSQL遇到的问题 之前将PostgreSQL从9.5升级到了10.3版本,安装时将端口设置成了5433,(默认是5432),后来发现在使用psql来restore db会发生语法错误 ...

  6. spring boot 系列之二:spring boot 如何修改默认端口号和contextpath

    上一篇文件我们通过一个实例进行了spring boot 入门,我们发现tomcat端口号和上下文(context path)都是默认的, 如果我们对于这两个值有特殊需要的话,需要自己制定的时候怎么办呢 ...

  7. 禁止root登陆sshd/并修改默认端口号

    1,新建一个用户: #useradd xxx 2,为新用户设置密码: #passwd xxx 3,修改sshd配置文件 #vi /etc/ssh/sshd_config 查找“#PermitRootL ...

  8. mysql 查看并修改默认端口号

    1. 登录mysql [root@test /]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands en ...

  9. SQL Server修改默认端口号1433

    方法1: 1) SqlServer服务使用两个端口:TCP-1433.UDP-1434. 其中1433用于供SqlServer对外提供服务,1434用于向请求者返回SqlServer使用了那个TCP/ ...

随机推荐

  1. 小程序App.js 传递数据给实例(app异步数据问题)

    在最开始初始化的时候,都会触发app.js 这个里面的onload生命方法, 在这个方法里面我们可以获取之前的存储数据/异步请求等等操作, 但是这些操作一般都是需要稍许时间.也就是说在其他界面加载结束 ...

  2. react简书笔记一 环境, git 和 项目 关联

    1.. 建立git项目  ( 码云, github 都可以 ), 具体步骤: https://www.cnblogs.com/andy-lehhaxm/p/10720717.html 1.1  git ...

  3. Descriptio Resource Path LocationType Archive for required library: 'D:/apache-maven/apache-maven-3.6.0/mavenrepository/org/springframework/spring-aspects/4.3.7.RELEASE/spring-aspects-4.3.7.RELEASE.

    eclipse创建了一个maven项目后,在导入依赖包后返现项目有个红色刺眼的感叹号,再看控制台有个Problem提示 Descriptio Resource Path LocationType Ar ...

  4. 第二章 python变量及文件

    1.环境变量 1.配置环境变量不是必须的2.配置环境变量的目的:为终端提供执行环境 2.Python代码执行 1.交互式       -- 控制台直接编写运行Python代码       -- 1.打 ...

  5. springboot 返回json字符串格式化问题

    在idea中yml文件中添加以下注解就可以格式化json字符串效果 spring: jackson: serialization: indent-output: true 原返回json格式为: {& ...

  6. python2 和 python3共存,并且分开使用

    目录 一.本文使用的python版本安装 二.安装python2 和 Python3 三.修改名称,配置环境,实现共存 1.将C:\Python36中的python.exe修改为python3.exe ...

  7. ERP新人防坑指南

    本文作为初入ERP行业的新人的防坑指南,讲解了一些常见犯的错,这样也少走一些弯路,如果你是老鸟,请绕过 :-) 本文关联的代码使用kotlin编写,请自行转换为c#.java等你熟悉的语言,表述的坑在 ...

  8. IntelliJ IDEA2018.3 最新破解方法

    IntelliJ IDEA2018.3 最新破解方法 输入    http://idea.java.sx/   即可,亲测可用.如果资金允许还是希望大家能支持正版,尊重原创 ------------- ...

  9. nodejs和mongodb常见的小错误

    1.直接使用nodejs会遇到以下问题: module.js:340    throw err;         ^Error: Cannot find module 'mongodb' 解决方式如下 ...

  10. angular实操

    一.创建angular工程(一定要在管理员权限下运行,Windows可忽略) 1.安装angular CLi 在终端窗口打开想要创建的工程所在文件夹,如:Cd Desktop\Angular-proj ...