场景:按照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.p<span style="color:transparent">本文来源gaodai#ma#com搞*!代#%^码$网!</span>y", 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 "