从相同的脚本Python运行2个web.py服务器

问题描述 投票:1回答:1

Python的新手。我试图从同一脚本运行2个具有不同端口的web.py服务器。基本上我想同时启动2个脚本并能够同时访问两个端口。如果我去终端并单独启动每个脚本,则可以使用,但我不想要。

SCRIPT 1

#!/usr/bin/python
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages/')
import web
import time
urls = (
    '/', 'index'
)
class index:

    def GET(self):
        f = open('LiveFlow.txt', 'r')
        lineList = f.readlines()
        contents = lineList[-1]
        return contents

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

SCRIPT 2

#!/usr/bin/python
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages/')
import web
import time

class MyApplication(web.application):
          def run(self, port=8080, *middleware):
              func = self.wsgifunc(*middleware)
              return web.httpserver.runsimple(func, ('0.0.0.0', port))

urls = (

    '/', 'index'

)

class index:


    def GET(self):

        f2 = open('FlowMeterOutput.txt', 'r')
        lineList1 = f2.readlines()
        contents1 = lineList1[-1]

        return contents1

if __name__ == "__main__":

    app = web.application(urls, globals())
    web.httpserver.runsimple(app.wsgifunc(), ("0.0.0.0", 8888))
python raspberry-pi web.py
1个回答
0
投票

您应该能够通过在自己的线程中像这样运行它们来做到这一点:

import threading

# ... two servers ...

def server1():
    app = web.application(urls, globals())
    app.run()

def server2():
    app = web.application(urls, globals())
    web.httpserver.runsimple(app.wsgifunc(), ("0.0.0.0", 8888))

if __name__ == "__main__":
    t1 = threading.Thread(target=server1)
    t2 = threading.Thread(target=server2)

    t1.start()
    t2.start()
    t1.join()
    t2.join()

而且,您似乎正在使用python 2.7,目前它已经很旧了。除非出于某些特殊原因,否则您应该使用Python3。如果不是全部,则大多数代码都可以在Python 3中正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.