diff options
| author | Simeon Simeonov | 2013-06-04 20:27:11 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2013-06-04 20:27:11 +0200 |
| commit | 751ab339ba5a38cbcd3a17620ab78e973fd5664f (patch) | |
| tree | f8f3d3b73f27957c663b256c3a7dc3f3680b084d /beinc_server.py | |
| parent | 3fcb4d9fbcc80b7bb2669eb03f838bb15fa9d5c6 (diff) | |
Beinc structure and design developed
Diffstat (limited to 'beinc_server.py')
| -rwxr-xr-x | beinc_server.py | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/beinc_server.py b/beinc_server.py new file mode 100755 index 0000000..a39dd05 --- /dev/null +++ b/beinc_server.py | |||
| @@ -0,0 +1,167 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -*- | ||
| 3 | |||
| 4 | import argparse | ||
| 5 | import getpass | ||
| 6 | import json | ||
| 7 | import os | ||
| 8 | import sys | ||
| 9 | |||
| 10 | import cherrypy | ||
| 11 | |||
| 12 | |||
| 13 | __author__ = 'Simeon Simeonov' | ||
| 14 | __version__ = '1.0-beta' | ||
| 15 | __license__ = "GPL3" | ||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | class WebNotifyServer(object): | ||
| 20 | |||
| 21 | def __init__(self, config): | ||
| 22 | """ | ||
| 23 | """ | ||
| 24 | self.__config = config | ||
| 25 | self.__instances = dict() | ||
| 26 | self.__queues = dict() | ||
| 27 | |||
| 28 | try: | ||
| 29 | for instance in self.__config['server']['instances']: | ||
| 30 | self.__queues[instance['name']] = list() | ||
| 31 | self.__instances[instance['name']] = instance | ||
| 32 | |||
| 33 | except Exception as e: | ||
| 34 | sys.stderr.write('Unable to initialize queues: {0}'.format(e)) | ||
| 35 | sys.exit(1) | ||
| 36 | |||
| 37 | |||
| 38 | @cherrypy.expose | ||
| 39 | def index(self): | ||
| 40 | """ | ||
| 41 | default dispatcher | ||
| 42 | """ | ||
| 43 | return 'index' | ||
| 44 | |||
| 45 | |||
| 46 | @cherrypy.expose | ||
| 47 | def default(self, *args): | ||
| 48 | """ | ||
| 49 | default dispatcher | ||
| 50 | """ | ||
| 51 | return 'default' | ||
| 52 | |||
| 53 | |||
| 54 | @cherrypy.expose | ||
| 55 | def push(self, *args, **kwargs): | ||
| 56 | |||
| 57 | if not args: | ||
| 58 | raise cherrypy.HTTPError(400) | ||
| 59 | |||
| 60 | print('args: {0}'.format(args)) | ||
| 61 | print('kwargs: {0}'.format(kwargs)) | ||
| 62 | |||
| 63 | instance = self.__get_instance(args[0]) | ||
| 64 | |||
| 65 | if not kwargs.get('password') == instance['password']: | ||
| 66 | raise cherrypy.HTTPError('403 Forbidden', | ||
| 67 | 'Wrong instance or password') | ||
| 68 | |||
| 69 | return 'OK' | ||
| 70 | |||
| 71 | |||
| 72 | def __get_instance(self, name): | ||
| 73 | """ | ||
| 74 | returns a matching 'instance' dictionary | ||
| 75 | |||
| 76 | cherrypy.HTTPError - "403 Forbidden" is raised if no | ||
| 77 | instance matches 'name' | ||
| 78 | """ | ||
| 79 | |||
| 80 | try: | ||
| 81 | for instance in self.__config['server']['instances']: | ||
| 82 | if name == instance['name']: | ||
| 83 | return instance | ||
| 84 | |||
| 85 | except Exception as e: | ||
| 86 | raise cherrypy.HTTPError('403 Forbidden', | ||
| 87 | 'Wrong instance or password: {0}'.format(e)) | ||
| 88 | |||
| 89 | raise cherrypy.HTTPError("403 Forbidden", "Wrong instance or password") | ||
| 90 | |||
| 91 | |||
| 92 | # mappings = [ | ||
| 93 | # (r'^/push/', push), | ||
| 94 | # (r'^.*$', default), | ||
| 95 | # ] | ||
| 96 | |||
| 97 | |||
| 98 | def main(): | ||
| 99 | |||
| 100 | parser = argparse.ArgumentParser(description='The following options are available') | ||
| 101 | |||
| 102 | |||
| 103 | parser.add_argument('-d', | ||
| 104 | action='store_true', | ||
| 105 | dest='daemonize', | ||
| 106 | default=False, | ||
| 107 | help='daemonize the server process') | ||
| 108 | |||
| 109 | parser.add_argument('-H', '--hostname', | ||
| 110 | metavar='HOSTNAME', | ||
| 111 | type=str, | ||
| 112 | dest='hostname', | ||
| 113 | default='127.0.0.1', | ||
| 114 | help="Server IP / hostname") | ||
| 115 | |||
| 116 | parser.add_argument('-p', '--port', | ||
| 117 | metavar='PORT', | ||
| 118 | type=int, | ||
| 119 | dest='port', | ||
| 120 | default=9998, | ||
| 121 | help="Server port") | ||
| 122 | |||
| 123 | parser.add_argument('-c', '--config-file', | ||
| 124 | metavar='FILE', | ||
| 125 | type=str, | ||
| 126 | default=os.path.expanduser('~/.beinc_server.json'), | ||
| 127 | dest='config_file', | ||
| 128 | help="config file") | ||
| 129 | |||
| 130 | parser.add_argument('-v', '--version', | ||
| 131 | action='version', | ||
| 132 | version='%(prog)s {0}'.format(__version__), | ||
| 133 | help='display program-version and exit') | ||
| 134 | |||
| 135 | args = parser.parse_args() | ||
| 136 | |||
| 137 | |||
| 138 | try: | ||
| 139 | with open(args.config_file, 'r') as fp: | ||
| 140 | config_dict = json.load(fp) | ||
| 141 | |||
| 142 | except Exception as e: | ||
| 143 | sys.stderr.write('Unable to parse {0}: {1}'.format(args.config_file, e)) | ||
| 144 | |||
| 145 | |||
| 146 | cherrypy.config.update({ | ||
| 147 | 'server.socket_host': args.hostname, | ||
| 148 | 'server.socket_port': args.port, | ||
| 149 | 'server.ssl_module': config_dict['server']['general']['ssl_module'].encode('utf-8'), | ||
| 150 | 'server.ssl_certificate': config_dict['server']['general']['ssl_certificate'], | ||
| 151 | 'server.ssl_private_key': config_dict['server']['general']['ssl_private_key'], | ||
| 152 | }) | ||
| 153 | |||
| 154 | try: | ||
| 155 | cherrypy.quickstart(WebNotifyServer(config_dict)) | ||
| 156 | |||
| 157 | except Exception as e: | ||
| 158 | sys.stderr.write("WebServer error: {0}".format(e)) | ||
| 159 | sys.exit(1) | ||
| 160 | |||
| 161 | |||
| 162 | sys.exit(0) | ||
| 163 | |||
| 164 | |||
| 165 | if __name__ == "__main__": | ||
| 166 | main() | ||
| 167 | |||
