-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
42 lines (33 loc) · 1.06 KB
/
server.py
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import multiprocessing
import http.server
import zmq
from websocket_server import WebsocketServer
import settings
DIR = os.path.dirname(os.path.realpath(__file__))
def receiver():
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect(f"tcp://127.0.0.1:{settings.ZMQ_PORT}")
socket.setsockopt(zmq.SUBSCRIBE, b'')
wss = WebsocketServer(host='', port=settings.WS_PORT)
wss.run_forever(True)
while True:
string = socket.recv_string()
wss.send_message_to_all(string)
def startHTTP():
class Handler(http.server.CGIHTTPRequestHandler):
def log_message(self, format, *args):
#Stop annoying logging to stderr
return
class Server(http.server.ThreadingHTTPServer):
def finish_request(self, request, client_address):
#Set docroot
self.RequestHandlerClass(request, client_address, self, directory=DIR+settings.HTTP_DIR)
with Server(('',settings.HTTP_PORT), Handler) as httpd:
httpd.serve_forever()
#Start HTTP Server
multiprocessing.Process(target=startHTTP).start()
receiver()