python flask 如何修改默认端口号的方法步骤
作者:都是一家人 发布时间:2021-07-04 16:35:14
标签:python,flask,默认端口号
场景:按照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.
<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中有指定。那么根据启动命令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.
<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的端口就成功了,由此可知,当出现一个我们不知道该如何解决问题的时候,我们可以尝试着自己分析源码,得到想要的结果。
来源:https://www.cnblogs.com/Spider-spiders/p/10335568.html
0
投稿
猜你喜欢
- 如今大部分网站仍然采用表格嵌套内容的方式来制作网站,虽然此方法对于我们来说比较熟悉、比较上手;但是,它却阻碍了一种更好的、更有亲和力的、更灵
- 一、os函数目录1 os.access(path, mode) 检验权限模式2 os.chdir(path) 改变当前工
- 如下所示:# 计算一个字符串中所有数字的和def numsum(s):sum = 0
- ModuleNotFoundError: No module named ‘dlib'尝试命令,但没成功pip3 install d
- 本文实例讲述了Symfony模板的快捷变量用法。分享给大家供大家参考,具体如下:在模板里,有一些symfony变量可以直接使用。通过这些快捷
- 导读前面几章我们以经介绍了怎么批量对excel和ppt操作今天我们说说对word文档的批量操作应用python-docx允许您创建新文档以及
- //1、运行到C盘根目录 //2、输入:SET ORACLE_SID = 你的SID名称 3、输入:sqlplus/nolog 4、输入:c
- 一、os__file__ 获取当前运行的.py文件所在的路径(D:\PycharmProjects\
- 移动互联网被称为“第五次科技革命”,而随着iPhone和Android等智能手机的日渐流行和iPad等平板电脑的出现,移动互联网的潜力和趋势
- 看了群主最后成像的图片,应该是循环了36次画方框,每次有10度的偏移。当然不能提前看答案,自己试着写代码。之前有用过海龟画图来画过五角星、奥
- 解释器模式(Interpreter Pattern)是什么解释器模式是一种行为型模式,它定义了一种语言文法,并且定义了一个解释器,用来解释这
- python中的变量定义是很灵活的,很容易搞混淆,特别是对于class的变量的定义,如何定义使用类里的变量是我们维护代码和保证代码稳定性的关
- 下载并选择MySQL的安装包由于MySQL一直在升级,所以当您阅读本文时,或许在其官方网站上已提供了新的正式版供你下载,因此,以下提供的链接
- 1. 用Dreamweaver 4.0制作闪动的Flash按钮选择菜单Insert→Interactive Images→Flash But
- 实例化对象名._类名__私有属性名 class Flylove:price = 123 def __init__(self):s
- 很多SQL Server程序员对子查询(subqueries)的使用感到困惑,尤其对于嵌套子查询(即子查询中包含一个子查询)。现在,就让我们
- 年前帮manager 招GUI设计实习生 (PS. 这个实习生职位依然open,欢迎有兴趣的同学来投,邮箱jj.ying [at] hp.c
- 一般情况下,导出超时可能都是以下三种情况:一、sql语句复杂,查询时间过长;二、处理查询后数据逻辑冗余;三、数据量过大导致响应超时。接下来分
- php获取域名的google收录示例function get_index($domain){ $url="http://www.g
- 在数据库应用的设计中,我们往往会需要获取某些表的记录总数,用于判断表的记录总数是否过大,是否需要备份数据等。我们通常的做法是:select