如何在OPenshi上简单地部署Twisted服务器

2024-04-16 07:35:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在pythonshell中安装了正确的环境设置python2.7.5、Twisted和imports。在

我有一个非常简单的服务器实例来显示在本地机器上工作的登录页。在

from twisted.web import http

class MyRequestHandler(http.Request):
    pages={
    '/': '<h1>Geo-Address Server</h1>Twisted Server is Up and Running..',
    '/test': '<h1>Test</h1>Test page',
    }
    def process(self):
        print self.path
        if self.pages.has_key(self.path):
            self.write(self.pages[self.path])
        else:
            self.setResponseCode(http.NOT_FOUND)
            self.write("<h1>Not Found</h1>Sorry, page does not exist")
        self.finish()

class MyHttp(http.HTTPChannel):
    requestFactory=MyRequestHandler

class MyHttpFactory(http.HTTPFactory):
    protocol=MyHttp

if __name__=='__main__':
    from twisted.internet import reactor
    reactor.listenTCP(8080, MyHttpFactory())
    reactor.run()

但是,在Openshift服务器上部署它无法运行。如果我试着运行脚本

^{pr2}$

我得到:

reactor.listenTCP(8080, MyHttpFactory()) File "/var/lib/openshift/5378ea844382ec89da000432/python/virtenv/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 495, in listenTCP p.startListening() File "/var/lib/openshift/5378ea844382ec89da000432/python/virtenv/lib/python2.7/site-packages/twisted/internet/tcp.py", line 979, in startListening raise CannotListenError(self.interface, self.port, le) twisted.internet.error.CannotListenError: Couldn't listen on any:8080: [Errno 13] Permission denied.

通读SO,大多数人只是说绑定到端口8080(我已经做过了),但是我仍然得到相同的错误。在


Tags: pathfromself服务器httplibtwistedpages
1条回答
网友
1楼 · 发布于 2024-04-16 07:35:25

正如kb所说

Please note: We don't allow arbitrary binding of ports on the externally accessible IP address.

It is possible to bind to the internal IP with port range: 15000 - 35530. All other ports are reserved for specific processes to avoid conflicts. Since we're binding to the internal IP, you will need to use port forwarding to access it: https://openshift.redhat.com/community/blogs/getting-started-with-port-forwarding-on-openshift

因此,只需找到$OPENSHIFT_PYTHON_IP即可

echo $OPENSHIFT_PYTHON_IP

然后将该地址添加到reactor侦听器的接口

^{pr2}$

另一种(也是最好的)方法是在代码中选择值。这样,如果IP动态更改,您仍然可以获得当前IP

import os

local_hostname =os.getenv("OPENSHIFT_INTERNAL_IP")
..
..
reactor.listenTCP(15000, MyHttpFactory(), interface=local_hostname)

注意:您只能绑定到端口范围(15000-35530)

相关问题 更多 >