Skip to content

JMassapina/python-service

 
 

Repository files navigation

Table of Contents

About

python-service is useful to call remote python code from a a client python script in a very simple way.

License

Server.py is licensed under GNU Affero GPL version 3 or later. http://www.gnu.org/licenses/agpl.html An special service 'affero' is provided to ease compliance.

The rest of the code is GNU Lesser GPL version 3 or later. http://www.gnu.org/licenses/lgpl.html In few words, you may freely use, modify and distribute this software as long as you redistribute it and any changes you did to the licenced software with the same conditions. Unlike the GNU GPL, the GNU Lesser GPL those conditions do not apply on the code that uses the licenced code.

Dependencies

  • Client: urllib2, urllib, urlparse, httplib, mimetypes
  • Server: webob, decorator
  • Tests: wsgi_intercept, unittest, urllib2, urllib, urlparse, httplib, mimetypes

Installation

No installation procedure is provided yet. Just copy the needed scripts side to your own files.

  • With your client: ServiceStub.py, HttpFormPost.py
  • With your server: Service.py

How to setup the server

  • Setup a module (ie. MyModule.py) with functions and variables
 version = "3.2"
 def myFunction(param) :
  	return "result: %s"%param

  • Create a Service object passing the module name as parameter.
 application = Service.Reload(Service.Service("MyModule"))

  • Pass the Service as the application object of a wgsi server.
    • With mod_wsgi (apache), just name it 'application'
    • You can use other dummy servers to test:
 from wsgiref.simple_server import make_server
 httpd = make_server(
 	'localhost', # Host name.
 	8051, # port
 	application, # application object
 	)
 httpd.serve_forever()

  • Check that it works by opening in a web server
 	http://localhost:8051/MyModule/myFunction?param=value

How to setup the client

  • Create a subclass of ServiceStub representing the service
  • Implement methods like the ones in MyModule by calling
remoteCall, ie, for
 		def myFunction(self, param) :
 			return self.remoteCall( "myFunction", param=param)

  • Instantiate the stub in your client code and use it
 	service = MyModule("https://mydomain.com:8051/MyModule")
 	service.myFunction(3)

Goodies

Default parameters

Any parameter can be set optional by setting a default in the server. You can accept any parameter by adding a **kwd parameter.

Accessing the request object

If you want to access the request, just add a first parameter named 'request' to your server function. It is a webob.Request object.

Sending files to the server

If you pass a file object to the stub, the file content will be passed as attached content and the server receives it as cgi.FieldStorage object.

Changing the content-type

By default, the content-type is 'text/plain', if you want to change it you can set a content_type property on the server function.

 def myFunction(param) :
 	return "result: %s"%param
 myFunction.content_type = 'text/html'

Forcing HTTP errors

Just raise any subclass of Service.HttpError in Service. Not all errors are defined, you can just subclass it yourself.

TO-DO's

  • Add unit tests for the ServiceStub
  • Adding decorators
  • Addding easy to construct response objects
  • Signing client message

About

Simple web service to remotely call Python module functions and data so that they look a local object. No generators, minimal stubbing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 95.1%
  • HTML 4.9%