简体   繁体   中英

Running 2 web.py servers from the same script Python

New to Python. I am trying to run 2 web.py servers with different ports on the from the same script. Basically i want to start the 2 scripts at the same time and be able to access both ports simultaneously. It works if i go to the terminal and start each script individually but i dont want that.

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))

You should be able to do this by running them in their own threads like so:

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()

Also, you seem to be using python 2.7 which is very old at this point. Unless there is some specific reason why you are doing is, you should be using Python 3. Most of your code should work fine in Python 3, if not all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM