Get the App
SLTechnology News&Howtos  ›  Servers  › 

What is the implementation of WSGI interface and RESTul service imitating Openstack

Shulou Source: shulou.com Published: 2022-05-31 23:29:39 10月02日 Update

In this issue, the editor will bring you about the WSGI interface and RESTul service implementation of imitating Openstack. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

The WSGI interface of Openstack implements the Controller class and the Router class through webob,pastedeploy,routes Here, the simple test program is implemented by imitating the WSG interface of Openstack. First of all, the testroutes.py file import loggingimport osimport webob.dec import webob.excfrom paste.deploy import loadappfrom wsgiref.simple_server import make_server import routes.middleware # Environment variable used to pass the request contextCONTEXT_ENV = 'openstack.context'# Environment variable used to pass the request paramsPARAMS_ENV =' openstack.params'LOG = logging.getLogger (_ _ name__) class Controller (object): @ webob.dec.wsgify def _ call__ (self) Req): arg_dict = req.environ ['wsgiorg.routing_args'] [1] action = arg_dict.pop (' action') del arg_dict ['controller'] context = req.environ.get (CONTEXT_ENV) {}) context ['query_string'] = dict (req.params.iteritems ()) context [' headers'] = dict (req.headers.iteritems ()) context ['path'] = req.environ [' PATH_INFO'] params = req.environ.get (PARAMS_ENV, {}) for name in ['REMOTE_USER' 'AUTH_TYPE']: try: context [name] = req.environ [name] except KeyError: try: del context [name] except KeyError: pass params.update (arg_dict) # TODO (termie): do some basic normalization on methods method = getattr (self) Action) result = method (context, * * params) return webob.Response ('OK') def getMessage (self,context) User_id): return {'Message':' TestMessage'} pass class Router (object): def _ _ init__ (self): self._mapper = routes.Mapper () self._mapper.connect ('/ test/ {user_id}', controller=Controller (), action='getMessage' Conditions= {'method': [' GET']}) self._router = routes.middleware.RoutesMiddleware (self._dispatch, self._mapper) @ webob.dec.wsgify def _ _ call__ (self Req): return self._router @ staticmethod @ webob.dec.wsgify def _ dispatch (req): match = req.environ ['wsgiorg.routing_args'] [1] if not match: return webob.exc.HTTPNotFound () app = match [' controller'] return app @ classmethod def app_factory (cls Global_config, * * local_config): return cls () if _ _ name__ = ='_ main__': configfile='testroutes.ini' appname= "main" wsgi_app = loadapp ("config:%s"% os.path.abspath (configfile), appname) httpd = make_server ('localhost', 8282, wsgi_app) httpd.serve_forever ()

And then the testroutes.ini file.

[DEFAULT] name= Huang [composite: main] use=egg:Paste#urlmap/= show [show: show] pipeline = Root [app: root] paste.app_factory = testroutes:Router.app_factory

Thus, the ini file is configured with the root directory / according to the mode of pastedeploy, pointing to pipeline show,pipeline and pointing to app root. App points to the app_factory function of Router and returns Router (). Initializing _ _ init__- > _ _ call__ returns self._routers according to the calling procedure. According to the mapping rules written under _ _ init__, paths such as / test/123 can be identified, and its handler calls the "action" function of "Controller", that is, getMessage.

To run the test, type 127.0. 01.15 8282 Unix TestUniver 123 in the browser.

Return OK, as shown in

Return webob.Response ('OK') the above is what the WSGI interface and RESTul service implementation of imitating Openstack is shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Interfaces functions pointing files services content analysis testing professional small and medium that is rich content that is articles more root directories patterns browsers thus we can see Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Tech Info MySQL Microsoft NVidia