-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviking_server.py
More file actions
executable file
·67 lines (51 loc) · 1.98 KB
/
viking_server.py
File metadata and controls
executable file
·67 lines (51 loc) · 1.98 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
#
#
# simple web socket server...
#
# https://github.com/opiate/SimpleWebSocketServer
# thanks @opiate
#
# @gitrc 2014
#
import signal, sys, ssl, logging
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer, SimpleSSLWebSocketServer
from optparse import OptionParser
from pprint import pprint
shared_secret = 'key007'
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
class SimpleChat(WebSocket):
def handleMessage(self):
if self.data is None:
self.data = ''
payload = str(self.data)
if payload.startswith(shared_secret):
payload = payload.replace(shared_secret, '')
for client in self.server.connections.itervalues():
if client != self:
try:
client.sendMessage(payload)
except Exception as n:
print n
#def handleConnected(self):
#print self.address, 'connected'
#def handleClose(self):
#print self.address, 'closed'
if __name__ == "__main__":
parser = OptionParser(usage="usage: %prog [options]", version="%prog 1.0")
parser.add_option("--host", default='', type='string', action="store", dest="host", help="hostname (localhost)")
parser.add_option("--port", default=8888, type='int', action="store", dest="port", help="port (8000)")
parser.add_option("--ssl", default=0, type='int', action="store", dest="ssl", help="ssl (1: on, 0: off (default))")
parser.add_option("--cert", default='./cert.pem', type='string', action="store", dest="cert", help="cert (./cert.pem)")
parser.add_option("--ver", default=ssl.PROTOCOL_TLSv1, type=int, action="store", dest="ver", help="ssl version")
(options, args) = parser.parse_args()
cls = SimpleChat
if options.ssl == 1:
server = SimpleSSLWebSocketServer(options.host, options.port, cls, options.cert, options.cert, version=options.ver)
else:
server = SimpleWebSocketServer(options.host, options.port, cls)
def close_sig_handler(signal, frame):
server.close()
sys.exit()
signal.signal(signal.SIGINT, close_sig_handler)
server.serveforever()