mec_simulator.py 1.4 KB
Newer Older
Yann Garcia's avatar
Yann Garcia committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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.5'
#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)
        print ('Started httpserver on port ' , PORT_NUMBER)
        # Wait forever for incoming http requests in background
        #server.serve_forever()
        threading.Thread(target=server.serve_forever).start()
        # Use keybord entries to trigger notifications
        while exit == False:
            ch = getch()
            if ch == b'\x03':
                exit = True
            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__