Skip to content

Commit

Permalink
Refactor optional imports, make 'resource' optional.
Browse files Browse the repository at this point in the history
Ticket #2: #2 - win32
support. The 'resource' module is not available under Windows. We only
use it for daemonizing so make it optional and disable daemonizing
support on Windows for now.

Also, refactor how the optional imports to turn them into data instead
of code and iterate through them.

Add early warnings to indicate when modules are missing and
functionality is disabled.
  • Loading branch information
kanaka committed May 18, 2011
1 parent c858711 commit c659bcb
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
'''

import sys, socket, struct, traceback, select
import os, resource, errno, signal # daemonizing
import os, sys, errno, signal, socket, struct, traceback, select
from cgi import parse_qsl
from base64 import b64encode, b64decode

Expand Down Expand Up @@ -49,16 +48,16 @@
from sha import sha as sha1

# Degraded functionality if these imports are missing
try:
# Required for HyBi encode/decode
import numpy, ctypes
except ImportError:
numpy = ctypes = None
for mod, sup in [('numpy', 'HyBi protocol'),
('ctypes', 'HyBi protocol'), ('ssl', 'TLS/SSL/wss'),
('resource', 'daemonizing')]:
try:
globals()[mod] = __import__(mod)
except ImportError:
globals()[mod] = None
print("WARNING: no '%s' module, %s support disabled" % (
mod, sup))

try:
import ssl
except:
ssl = None

class WebSocketServer(object):
"""
Expand Down Expand Up @@ -98,6 +97,7 @@ def __init__(self, listen_host='', listen_port=None,
self.listen_port = listen_port
self.ssl_only = ssl_only
self.daemon = daemon
self.handler_id = 1

# Make paths settings absolute
self.cert = os.path.abspath(cert)
Expand All @@ -112,16 +112,19 @@ def __init__(self, listen_host='', listen_port=None,
if self.web:
os.chdir(self.web)

self.handler_id = 1
# Sanity checks
if ssl and self.ssl_only:
raise Exception("No 'ssl' module and SSL-only specified")
if self.daemon and not resource:
raise Exception("Module 'resource' required to daemonize")

# Show configuration
print("WebSocket server settings:")
print(" - Listen on %s:%s" % (
self.listen_host, self.listen_port))
print(" - Flash security policy server")
if self.web:
print(" - Web server")
if ssl and self.ssl_only:
raise Exception("No 'ssl' module and SSL only specified")
if ssl:
if os.path.exists(self.cert):
print(" - SSL/TLS support")
Expand Down

0 comments on commit c659bcb

Please sign in to comment.