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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:31:41  来源:igfitidea点击:

web.py - specify address and port

pythonweb.py

提问by Jakub M.

How to specify listening address and port in web.py? Something like:

如何在web.py 中指定监听地址和端口?就像是:

web.application( urls, host="33.44.55.66", port=8080 )

Edit

编辑

I would like to avoid using the default web.py command line parsing

我想避免使用默认的 web.py 命令行解析

采纳答案by sidi

From API docmentation of web.py:

来自web.py 的API文档

 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"