Python web.py - 指定地址和端口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14444913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
web.py - specify address and port
提问by Jakub M.
采纳答案by sidi
From API docmentation of web.py:
module web.httpserver
function runsimple(func,server_address=('0.0.0.0', 8080))
Runs CherryPy WSGI server hosting WSGI app func. The directory static/ is hosted statically.
运行 CherryPy WSGI 服务器托管 WSGI 应用程序功能。目录 static/ 是静态托管的。
Example code
示例代码
import web
class MyApplication(web.application):
def run(self, port=8080, *middleware):
func = self.wsgifunc(*middleware)
return web.httpserver.runsimple(func, ('0.0.0.0', port))
if __name__ == "__main__":
app = MyApplication(urls, globals())
app.run(port=8888)
回答by Dan
If you're using web.py's built-in webserver, you can just append the port to the command:
如果您使用 web.py 的内置网络服务器,则只需将端口附加到命令:
python app.py 8080
I haven't tried ever with the listening address, but perhaps it will accept 1.2.3.4:8080 as the format.
我还没有尝试过监听地址,但也许它会接受 1.2.3.4:8080 作为格式。
回答by Prajapathy3165
if __name__ == "__main__":
web.httpserver.runsimple(app.wsgifunc(), ("0.0.0.0", 8888))
回答by Peablog
you can see the follow code in wsgi.py:
您可以在 wsgi.py 中看到以下代码:
server_addr = validip(listget(sys.argv, 1, ''))
if os.environ.has_key('PORT'): # e.g. Heroku
server_addr = ('0.0.0.0', intget(os.environ['PORT']))
return httpserver.runsimple(func, server_addr)
so, you can set the web server port by add environ variable:
因此,您可以通过添加环境变量来设置 Web 服务器端口:
import os
os.environ["PORT"] = "80"

![Python 捕获“socket.error: [Errno 111] 连接被拒绝”异常](/res/img/loading.gif)