2020# SOFTWARE.
2121
2222import os
23+ import sys
2324import threading
2425import json
2526
3637
3738qng_wrapper = QngWrapperWindows () if (os .name == 'nt' ) else QngWrapperLinux ()
3839
39- # Original API ----------------------------------------------
40-
41- @app .route ('/api/randint32' )
42- def randint32 ():
43- return Response (str (qng_wrapper .randint32 ()), content_type = 'text/plain' )
44-
45-
46- @app .route ('/api/randuniform' )
47- def randuniform ():
48- return Response (str (qng_wrapper .randuniform ()), content_type = 'text/plain' )
49-
50-
51- @app .route ('/api/randnormal' )
52- def randnormal ():
53- return Response (str (qng_wrapper .randnormal ()), content_type = 'text/plain' )
54-
55- @app .route ('/api/randhex' )
56- def randhex ():
57- try :
58- length = int (request .args .get ('length' ))
59- if length < 1 :
60- return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
61- return Response (qng_wrapper .randbytes (length ).hex (), content_type = 'text/plain' )
62- except (TypeError , ValueError ) as e :
63- return Response (str (e ), status = 400 , content_type = 'text/plain' )
64-
65- @app .route ('/api/randbytes' )
66- def randbytes ():
67- try :
68- length = int (request .args .get ('length' ))
69- if length < 1 :
70- return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
71- return Response (qng_wrapper .randbytes (length ), content_type = 'application/octet-stream' )
72- except (TypeError , ValueError ) as e :
73- return Response (str (e ), status = 400 , content_type = 'text/plain' )
74-
75- # JSON API ----------------------------------------------
76-
77- @app .route ('/api/json/randint32' )
78- def randjsonint32 ():
79- try :
80- length = int (request .args .get ('length' ))
81- if length < 1 :
82- return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
83- int32array = []
84- for x in range (0 , length ):
85- int32array .append (qng_wrapper .randint32 ())
86- return Response (json .dumps ({"type" : "string" , "format" : "int32" , "length" :length , "data" : int32array , "success" : "true" }), content_type = 'text/plain' )
87- except (TypeError , ValueError ) as e :
88- return Response (json .dumps ({"error" : str (e ), "success" :"false" }), status = 400 , content_type = 'text/plain' )
89-
90- @app .route ('/api/json/randuniform' )
91- def randjsonuniform ():
92- try :
93- length = int (request .args .get ('length' ))
94- if length < 1 :
95- return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
96- uniformarray = []
97- for x in range (0 , length ):
98- uniformarray .append (qng_wrapper .randuniform ())
99- return Response (json .dumps ({"type" : "string" , "format" : "uniform" , "length" :length , "data" : uniformarray , "success" : "true" }), content_type = 'text/plain' )
100- except (TypeError , ValueError ) as e :
101- return Response (json .dumps ({"error" : str (e ), "success" :"false" }), status = 400 , content_type = 'text/plain' )
102-
103- @app .route ('/api/json/randnormal' )
104- def randjsonnormal ():
105- try :
106- length = int (request .args .get ('length' ))
107- if length < 1 :
108- return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
109- normarray = []
110- for x in range (0 , length ):
111- normarray .append (qng_wrapper .randnormal ())
112- return Response (json .dumps ({"type" : "string" , "format" : "normal" , "length" :length , "data" : normarray , "success" : "true" }), content_type = 'text/plain' )
113- except (TypeError , ValueError ) as e :
114- return Response (json .dumps ({"error" : str (e ), "success" :"false" }), status = 400 , content_type = 'text/plain' )
115-
116- @app .route ('/api/json/randhex' )
117- def randjsonhex ():
118- try :
119- length = int (request .args .get ('length' ))
120- size = int (request .args .get ('size' ))
121- if length < 1 :
122- return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
123- if size < 1 :
124- return Response ('size must be greater than 0' , status = 400 , content_type = 'text/plain' )
125- hexarray = []
126- for x in range (0 , length ):
127- hexarray .append (qng_wrapper .randbytes (size ).hex ())
128- return Response (json .dumps ({"type" : "string" , "format" : "hex" , "length" :length , "size" : size , "data" : hexarray , "success" : "true" }), content_type = 'text/plain' )
129- except (TypeError , ValueError ) as e :
130- return Response (json .dumps ({"error" : str (e ), "success" :"false" }), status = 400 , content_type = 'text/plain' )
131-
132- @app .route ('/api/clear' )
133- def clear ():
134- qng_wrapper .clear ()
135- return Response (status = 204 )
136-
137- # Websockets ----------------------------------------------
138-
139- @sockets .route ('/ws' )
140- def ws (websocket ):
141- subscribed = [False ]
142- while not websocket .closed :
143- threading .Thread (target = handle_ws_message , args = (websocket .receive (), websocket , subscribed )).start ()
144-
145- def handle_ws_message (message , websocket , subscribed ):
146- try :
147- split_message = message .strip ().upper ().split ()
148- if split_message [0 ] == 'RANDINT32' :
149- websocket .send (str (qng_wrapper .randint32 ()))
150- elif split_message [0 ] == 'RANDUNIFORM' :
151- websocket .send (str (qng_wrapper .randuniform ()))
152- elif split_message [0 ] == 'RANDNORMAL' :
153- websocket .send (str (qng_wrapper .randnormal ()))
154- elif split_message [0 ] == 'RANDBYTES' :
155- length = int (split_message [1 ])
40+ def main ():
41+ # Commandline Arguments (servername, port)
42+
43+ argNo = len (sys .argv ) - 1
44+
45+ if argNo < 2 :
46+ print ("--------------------------------------------" )
47+ print ("Please provide arguments: <servername> <port>" )
48+ print ("--------------------------------------------" )
49+ else :
50+ servername = sys .argv [1 ]
51+ port = int (sys .argv [2 ])
52+ print ("----------------------------------------------------------------------------------------" )
53+ print ("Serving Entropy as pod \" " , servername , "\" on http://localhost:" , port , "/api/..." , sep = '' )
54+ print ("----------------------------------------------------------------------------------------" )
55+ serve (servername , port )
56+
57+ def serve (servername , port ):
58+
59+ # Original API ----------------------------------------------
60+
61+ @app .route ('/api/randint32' )
62+ def randint32 ():
63+ return Response (str (qng_wrapper .randint32 ()), content_type = 'text/plain' )
64+
65+
66+ @app .route ('/api/randuniform' )
67+ def randuniform ():
68+ return Response (str (qng_wrapper .randuniform ()), content_type = 'text/plain' )
69+
70+
71+ @app .route ('/api/randnormal' )
72+ def randnormal ():
73+ return Response (str (qng_wrapper .randnormal ()), content_type = 'text/plain' )
74+
75+ @app .route ('/api/randhex' )
76+ def randhex ():
77+ try :
78+ length = int (request .args .get ('length' ))
79+ if length < 1 :
80+ return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
81+ return Response (qng_wrapper .randbytes (length ).hex (), content_type = 'text/plain' )
82+ except (TypeError , ValueError ) as e :
83+ return Response (str (e ), status = 400 , content_type = 'text/plain' )
84+
85+ @app .route ('/api/randbytes' )
86+ def randbytes ():
87+ try :
88+ length = int (request .args .get ('length' ))
89+ if length < 1 :
90+ return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
91+ return Response (qng_wrapper .randbytes (length ), content_type = 'application/octet-stream' )
92+ except (TypeError , ValueError ) as e :
93+ return Response (str (e ), status = 400 , content_type = 'text/plain' )
94+
95+ # JSON API ----------------------------------------------
96+
97+ @app .route ('/api/json/randint32' )
98+ def randjsonint32 ():
99+ try :
100+ length = int (request .args .get ('length' ))
101+ if length < 1 :
102+ return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
103+ int32array = []
104+ for x in range (0 , length ):
105+ int32array .append (qng_wrapper .randint32 ())
106+ return Response (json .dumps ({"server" : servername , "type" : "string" , "format" : "int32" , "length" :length , "data" : int32array , "success" : "true" }), content_type = 'text/plain' )
107+ except (TypeError , ValueError ) as e :
108+ return Response (json .dumps ({"error" : str (e ), "success" :"false" }), status = 400 , content_type = 'text/plain' )
109+
110+ @app .route ('/api/json/randuniform' )
111+ def randjsonuniform ():
112+ try :
113+ length = int (request .args .get ('length' ))
114+ if length < 1 :
115+ return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
116+ uniformarray = []
117+ for x in range (0 , length ):
118+ uniformarray .append (qng_wrapper .randuniform ())
119+ return Response (json .dumps ({"server" : servername , "type" : "string" , "format" : "uniform" , "length" :length , "data" : uniformarray , "success" : "true" }), content_type = 'text/plain' )
120+ except (TypeError , ValueError ) as e :
121+ return Response (json .dumps ({"error" : str (e ), "success" :"false" }), status = 400 , content_type = 'text/plain' )
122+
123+ @app .route ('/api/json/randnormal' )
124+ def randjsonnormal ():
125+ try :
126+ length = int (request .args .get ('length' ))
127+ if length < 1 :
128+ return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
129+ normarray = []
130+ for x in range (0 , length ):
131+ normarray .append (qng_wrapper .randnormal ())
132+ return Response (json .dumps ({"server" : servername , "type" : "string" , "format" : "normal" , "length" :length , "data" : normarray , "success" : "true" }), content_type = 'text/plain' )
133+ except (TypeError , ValueError ) as e :
134+ return Response (json .dumps ({"error" : str (e ), "success" :"false" }), status = 400 , content_type = 'text/plain' )
135+
136+ @app .route ('/api/json/randhex' )
137+ def randjsonhex ():
138+ try :
139+ length = int (request .args .get ('length' ))
140+ size = int (request .args .get ('size' ))
156141 if length < 1 :
157- raise ValueError ()
158- websocket .send (qng_wrapper .randbytes (length ))
159- elif split_message [0 ] == 'SUBSCRIBEINT32' :
160- if not subscribed [0 ]:
161- subscribed [0 ] = True
162- while subscribed [0 ] and not websocket .closed :
163- websocket .send (str (qng_wrapper .randint32 ()))
164- elif split_message [0 ] == 'SUBSCRIBEUNIFORM' :
165- if not subscribed [0 ]:
166- subscribed [0 ] = True
167- while subscribed [0 ] and not websocket .closed :
168- websocket .send (str (qng_wrapper .randuniform ()))
169- elif split_message [0 ] == 'SUBSCRIBENORMAL' :
170- if not subscribed [0 ]:
171- subscribed [0 ] = True
172- while subscribed [0 ] and not websocket .closed :
173- websocket .send (str (qng_wrapper .randnormal ()))
174- elif split_message [0 ] == 'SUBSCRIBEBYTES' :
175- chunk = int (split_message [1 ])
176- if chunk < 1 :
177- raise ValueError ()
178- if not subscribed [0 ]:
179- subscribed [0 ] = True
180- while subscribed [0 ] and not websocket .closed :
181- websocket .send (qng_wrapper .randbytes (chunk ))
182- elif split_message [0 ] == 'SUBSCRIBEHEX' :
183- chunk = int (split_message [1 ])
184- if chunk < 1 :
185- raise ValueError ()
186- if not subscribed [0 ]:
187- subscribed [0 ] = True
188- while subscribed [0 ] and not websocket .closed :
189- websocket .send (qng_wrapper .randbytes (chunk ).hex ())
190- elif split_message [0 ] == 'UNSUBSCRIBE' :
191- subscribed [0 ] = False
192- websocket .send ('UNSUBSCRIBED' )
193- elif split_message [0 ] == 'CLEAR' :
194- qng_wrapper .clear ()
195- except (IndexError , ValueError , BlockingIOError ):
196- pass
197- except Exception as e :
198- websocket .close (code = 1011 , message = str (e ))
199-
200- @app .errorhandler (Exception )
201- def handle_exception (e ):
202- return Response (e .description , status = e .code , content_type = 'text/plain' )
203-
204- server = pywsgi .WSGIServer (('0.0.0.0' , 62456 ), application = app , handler_class = WebSocketHandler )
205- server .serve_forever ()
142+ return Response ('length must be greater than 0' , status = 400 , content_type = 'text/plain' )
143+ if size < 1 :
144+ return Response ('size must be greater than 0' , status = 400 , content_type = 'text/plain' )
145+ hexarray = []
146+ for x in range (0 , length ):
147+ hexarray .append (qng_wrapper .randbytes (size ).hex ())
148+ return Response (json .dumps ({"server" : servername , "type" : "string" , "format" : "hex" , "length" :length , "size" : size , "data" : hexarray , "success" : "true" }), content_type = 'text/plain' )
149+ except (TypeError , ValueError ) as e :
150+ return Response (json .dumps ({"error" : str (e ), "success" :"false" }), status = 400 , content_type = 'text/plain' )
151+
152+ @app .route ('/api/clear' )
153+ def clear ():
154+ qng_wrapper .clear ()
155+ return Response (status = 204 )
156+
157+ # Websockets ----------------------------------------------
158+
159+ @sockets .route ('/ws' )
160+ def ws (websocket ):
161+ subscribed = [False ]
162+ while not websocket .closed :
163+ threading .Thread (target = handle_ws_message , args = (websocket .receive (), websocket , subscribed )).start ()
164+
165+ def handle_ws_message (message , websocket , subscribed ):
166+ try :
167+ split_message = message .strip ().upper ().split ()
168+ if split_message [0 ] == 'RANDINT32' :
169+ websocket .send (str (qng_wrapper .randint32 ()))
170+ elif split_message [0 ] == 'RANDUNIFORM' :
171+ websocket .send (str (qng_wrapper .randuniform ()))
172+ elif split_message [0 ] == 'RANDNORMAL' :
173+ websocket .send (str (qng_wrapper .randnormal ()))
174+ elif split_message [0 ] == 'RANDBYTES' :
175+ length = int (split_message [1 ])
176+ if length < 1 :
177+ raise ValueError ()
178+ websocket .send (qng_wrapper .randbytes (length ))
179+ elif split_message [0 ] == 'SUBSCRIBEINT32' :
180+ if not subscribed [0 ]:
181+ subscribed [0 ] = True
182+ while subscribed [0 ] and not websocket .closed :
183+ websocket .send (str (qng_wrapper .randint32 ()))
184+ elif split_message [0 ] == 'SUBSCRIBEUNIFORM' :
185+ if not subscribed [0 ]:
186+ subscribed [0 ] = True
187+ while subscribed [0 ] and not websocket .closed :
188+ websocket .send (str (qng_wrapper .randuniform ()))
189+ elif split_message [0 ] == 'SUBSCRIBENORMAL' :
190+ if not subscribed [0 ]:
191+ subscribed [0 ] = True
192+ while subscribed [0 ] and not websocket .closed :
193+ websocket .send (str (qng_wrapper .randnormal ()))
194+ elif split_message [0 ] == 'SUBSCRIBEBYTES' :
195+ chunk = int (split_message [1 ])
196+ if chunk < 1 :
197+ raise ValueError ()
198+ if not subscribed [0 ]:
199+ subscribed [0 ] = True
200+ while subscribed [0 ] and not websocket .closed :
201+ websocket .send (qng_wrapper .randbytes (chunk ))
202+ elif split_message [0 ] == 'SUBSCRIBEHEX' :
203+ chunk = int (split_message [1 ])
204+ if chunk < 1 :
205+ raise ValueError ()
206+ if not subscribed [0 ]:
207+ subscribed [0 ] = True
208+ while subscribed [0 ] and not websocket .closed :
209+ websocket .send (qng_wrapper .randbytes (chunk ).hex ())
210+ elif split_message [0 ] == 'UNSUBSCRIBE' :
211+ subscribed [0 ] = False
212+ websocket .send ('UNSUBSCRIBED' )
213+ elif split_message [0 ] == 'CLEAR' :
214+ qng_wrapper .clear ()
215+ except (IndexError , ValueError , BlockingIOError ):
216+ pass
217+ except Exception as e :
218+ websocket .close (code = 1011 , message = str (e ))
219+
220+ @app .errorhandler (Exception )
221+ def handle_exception (e ):
222+ return Response (e .description , status = e .code , content_type = 'text/plain' )
223+
224+ server = pywsgi .WSGIServer (('0.0.0.0' , port ), application = app , handler_class = WebSocketHandler )
225+ server .serve_forever ()
226+
227+ if __name__ == "__main__" :
228+ main ()
0 commit comments