mirror of
https://github.com/lightbroker/llmsecops-research.git
synced 2026-07-06 04:57:50 +02:00
initial port from private repo
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import cgi
|
||||
|
||||
|
||||
class PathDispatcher:
|
||||
def __init__(self):
|
||||
self.routes = {}
|
||||
|
||||
def notfound_404(self, env, start_response):
|
||||
start_response('404 Not Found', [ ('Content-Type', 'text/plain') ])
|
||||
return [b'Not Found']
|
||||
|
||||
def __call__(self, env, start_response):
|
||||
path = env.get('PATH_INFO')
|
||||
params = cgi.FieldStorage(env.get('wsgi.output'), environ=env)
|
||||
method = env.get('REQUEST_METHOD').lower()
|
||||
env['params'] = { key: params.getvalue(key) for key in params }
|
||||
handler = self.routes.get((method,path), self.notfound_404)
|
||||
return handler(env, start_response)
|
||||
|
||||
def register(self, method, path, function):
|
||||
self.routes[method.lower(), path] = function
|
||||
return function
|
||||
@@ -0,0 +1,24 @@
|
||||
from PathDispatcher import PathDispatcher
|
||||
from wsgiref.simple_server import make_server
|
||||
|
||||
|
||||
class RestApiServer:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def response_function(self, environ, start_response):
|
||||
start_response('200 OK', [('Content-Type','text/html')])
|
||||
yield str(f'testing...\n').encode('utf-8')
|
||||
|
||||
def listen(self):
|
||||
port = 9999
|
||||
dispatcher = PathDispatcher()
|
||||
dispatcher.register('GET', '/hello', self.response_function)
|
||||
wsgi_srv = make_server('', port, dispatcher)
|
||||
print(f'listening on port {port}...')
|
||||
wsgi_srv.serve_forever()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
srv = RestApiServer()
|
||||
srv.listen()
|
||||
Reference in New Issue
Block a user