python flask 如何修改默认端口号的方法步骤
场景:按照github文档上启动一个flask的app,默认是用5000端口,如果5000端口被占用,启动失败。
样例代码:
fromflaskimportFlask
app=Flask(__name__)
@app.route('/')
defhello():
return'Hello,World!'
启动的脚本:
$envFLASK_APP=hello.pyflaskrun
出错信息如下:
renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django$envFLASK_APP=index.pyflaskrun *ServingFlaskapp"index.py" *Environment:production WARNING:Donotusethedevelopmentserverinaproductionenvironment. UseaproductionWSGIserverinstead. *Debugmode:off Traceback(mostrecentcalllast): File"/usr/local/bin/flask",line11,insys.exit(main()) File"/usr/local/lib/python2.7/dist-packages/flask/cli.py",line894,inmain cli.main(args=args,prog_name=name) File"/usr/local/lib/python2.7/dist-packages/flask/cli.py",line557,inmain returnsuper(FlaskGroup,self).main(*args,**kwargs) File"/usr/local/lib/python2.7/dist-packages/click/core.py",line717,inmain rv=self.invoke(ctx) File"/usr/local/lib/python2.7/dist-packages/click/core.py",line1137,ininvoke return_process_result(sub_ctx.command.invoke(sub_ctx)) File"/usr/local/lib/python2.7/dist-packages/click/core.py",line956,ininvoke returnctx.invoke(self.callback,**ctx.params) File"/usr/local/lib/python2.7/dist-packages/click/core.py",line555,ininvoke returncallback(*args,**kwargs) File"/usr/local/lib/python2.7/dist-packages/click/decorators.py",line64,innew_func returnctx.invoke(f,obj,*args,**kwargs) File"/usr/local/lib/python2.7/dist-packages/click/core.py",line555,ininvoke returncallback(*args,**kwargs) File"/usr/local/lib/python2.7/dist-packages/flask/cli.py",line771,inrun_command threaded=with_threads,ssl_context=cert) File"/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py",line814,inrun_simple inner() File"/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py",line774,ininner fd=fd) File"/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py",line660,inmake_server passthrough_errors,ssl_context,fd=fd) File"/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py",line577,in__init__ self.address_family),handler) File"/usr/lib/python2.7/SocketServer.py",line417,in__init__ self.server_bind() File"/usr/lib/python2.7/BaseHTTPServer.py",line108,inserver_bind SocketServer.TCPServer.server_bind(self) File"/usr/lib/python2.7/SocketServer.py",line431,inserver_bind self.socket.bind(self.server_address) File"/usr/lib/python2.7/socket.py",line228,inmeth returngetattr(self._sock,name)(*args) socket.error:[Errno98]Addressalreadyinuse
问题:
那么该怎么指定新的端口呢?又如何查看5000端口指定的位置呢?
源码分析,首先clonegithub上的flask框架,然后直接grep命令查看5000的位置。
renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django/flask$grep5000*-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:*Runningonhttp://127.0.0.1:5000/
docs/config.rst:453:*Runningonhttp://127.0.0.1:5000/
docs/quickstart.rst:51:*Runningonhttp://127.0.0.1:5000/
docs/quickstart.rst:66:*Runningonhttp://127.0.0.1:5000/
docs/quickstart.rst:72:Nowheadoverto`http://127.0.0.1:5000/ `_,andyou
docs/deploying/wsgi-standalone.rst:38:$uwsgi--http127.0.0.1:5000--modulemyproject:app
docs/deploying/wsgi-standalone.rst:55:http_server=WSGIServer(('',5000),app)
docs/server.rst:26:*http://localhost:5000/*.
docs/cli.rst:97:*Runningonhttp://127.0.0.1:5000/(PressCTRL+Ctoquit)
docs/cli.rst:143:*Runningonhttp://127.0.0.1:5000/(PressCTRL+Ctoquit)
docs/tutorial/static.rst:58:Gotohttp://127.0.0.1:5000/auth/loginandthepageshouldlooklikethe
docs/tutorial/factory.rst:169:*Runningonhttp://127.0.0.1:5000/(PressCTRL+Ctoquit)
docs/tutorial/factory.rst:174:Visithttp://127.0.0.1:5000/helloinabrowserandyoushouldseethe
docs/tutorial/templates.rst:174:thengotohttp://127.0.0.1:5000/auth/register.
examples/javascript/README.rst:39:Openhttp://127.0.0.1:5000inabrowser.
examples/tutorial/README.rst:62:Openhttp://127.0.0.1:5000inabrowser.
flask/app.py:878::paramport:theportofthewebserver.Defaultsto``5000``orthe
flask/app.py:925:_port=5000
flask/cli.py:738:@click.option('--port','-p',default=5000,
README.rst:43:*Runningonhttp://127.0.0.1:5000/(PressCTRL+Ctoquit)
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')doesnotmatchthe"
可以看到在flask/app.py以及cli.py中有指定。那么根据启动命令flaskrun实际上是启动了一个httpserver,然后监听了一个本地端口,等待连接。那么看看是否有相应的参数。
renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django/flask$flask--help Traceback(mostrecentcalllast): File"/usr/local/lib/python2.7/dist-packages/flask/cli.py",line529,inlist_commands rv.update(info.load_app().cli.list_commands(ctx)) File"/usr/local/lib/python2.7/dist-packages/flask/cli.py",line384,inload_app 'CouldnotlocateaFlaskapplication.Youdidnotprovide' NoAppException:CouldnotlocateaFlaskapplication.Youdidnotprovidethe"FLASK_APP"environmentvariable,anda"wsgi.py"or"app.py"modulewasnotfoundinthecurrentdirectory. Usage:flask[OPTIONS]COMMAND[ARGS]... AgeneralutilityscriptforFlaskapplications. ProvidescommandsfromFlask,extensions,andtheapplication.Loadsthe applicationdefinedintheFLASK_APPenvironmentvariable,orfroma wsgi.pyfile.SettingtheFLASK_ENVenvironmentvariableto'development' willenabledebugmode. $exportFLASK_APP=hello.py $exportFLASK_ENV=development $flaskrun Options: --versionShowtheflaskversion --helpShowthismessageandexit. Commands: routesShowtheroutesfortheapp. runRunsadevelopmentserver. shellRunsashellintheappcontext. renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django/flask$flaskrun--help Usage:flaskrun[OPTIONS] Runalocaldevelopmentserver. Thisserverisfordevelopmentpurposesonly.Itdoesnotprovidethe stability,security,orperformanceofproductionWSGIservers. ThereloaderanddebuggerareenabledbydefaultifFLASK_ENV=development orFLASK_DEBUG=1. Options: -h,--hostTEXTTheinterfacetobindto. -p,--portINTEGERTheporttobindto. --certPATHSpecifyacertificatefiletouseHTTPS. --keyFILEThekeyfiletousewhenspecifyinga certificate. --reload/--no-reloadEnableordisablethereloader.Bydefault thereloaderisactiveifdebugisenabled. --debugger/--no-debuggerEnableordisablethedebugger.Bydefault thedebuggerisactiveifdebugisenabled. --eager-loading/--lazy-loader Enableordisableeagerloading.Bydefault eagerloadingisenabledifthereloaderis disabled. --with-threads/--without-threads Enableordisablemultithreading. --helpShowthismessageandexit.
可以看到有-p这个参数,是指定端口的,默认是5000,那么尝试修改一下。
renjg@renjg-HP-Compaq-Pro-6380-MT:~/WorkSpace/python/django$envFLASK_APP=index.pyflaskrun-p5001 *ServingFlaskapp"index.py" *Environment:production WARNING:Donotusethedevelopmentserverinaproductionenvironment. UseaproductionWSGIserverinstead. *Debugmode:off *Runningonhttp://127.0.0.1:5001/(PressCTRL+Ctoquit)
还一个5001的端口就成功了,由此可知,当出现一个我们不知道该如何解决问题的时候,我们可以尝试着自己分析源码,得到想要的结果。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。