Skip to content
mec_simulator.py 1.6 KiB
Newer Older
Yann Garcia's avatar
Yann Garcia committed
# Very basic MEC PLAT simulator

import http.server
import socketserver
import mec_http_server
import threading
import sys, os, time
import msvcrt

# MEC simulator IPv4 address
#ADDRESS_BINDING = '172.28.4.87'
#ADDRESS_BINDING = '192.168.1.21'
ADDRESS_BINDING = '192.168.0.17'
Yann Garcia's avatar
Yann Garcia committed
#ADDRESS_BINDING = '127.0.0.1'
# MEC simulator IPv4 listening port
PORT_NUMBER = 8081

# TODO Add Linux support for detecting keyboard input from the terminal?
def getch():
    """ Capture keybord entry. """
    return msvcrt.getch()
# End of function getch

if __name__ == '__main__':
    exit = False
    try:
        # Create a web server and define the handler to manage the incoming request
#        server = socketserver.TCPServer((ADDRESS_BINDING, PORT_NUMBER), mec_http_server.myHandler)
        server = http.server.HTTPServer((ADDRESS_BINDING, PORT_NUMBER), mec_http_server.myHandler)
        server.wbufsize = -1
        print ('Started httpserver on port ', PORT_NUMBER)
Yann Garcia's avatar
Yann Garcia committed
        # Wait forever for incoming http requests in background
        #server.serve_forever(poll_interval=2)
Yann Garcia's avatar
Yann Garcia committed
        threading.Thread(target=server.serve_forever).start()
Yann Garcia's avatar
Yann Garcia committed
        # Use keybord entries to trigBger notifications
Yann Garcia's avatar
Yann Garcia committed
        while exit == False:
            ch = getch()
            if ch == b'\x03':
                exit = True
            elif ch == b'q':
                exit = True
Yann Garcia's avatar
Yann Garcia committed
            else:
                print(">>> ch= ", ch)
        server.shutdown()
        server.socket.close()
    except KeyboardInterrupt:
        print ('^C received, shutting down the web server')
        server.shutdown()
        server.socket.close()
# End of __main__