Phil
Phil

Reputation: 3149

How do you setup a pysimplesoap server to run on apache2 with wsgi?

I have a soap server which I have been running as a stand-alone application i.e. by just simply executing python mysoapserver.py

However, I would like it to be accessed via apache2 using wsgi.

Below are some code excerpts of the current code:

The imports:

from pysimplesoap.server import SoapDispatcher, SOAPHandler, WSGISOAPHandler

Code Excerpts

dispatcher = SoapDispatcher(
'TransServer',
location = "http://127.0.0.1:8050/",
action = 'http://127.0.0.1:8050/', # SOAPAction
namespace = "http://example.com/sample.wsdl", prefix="ns0",
trace = True,
ns = True)

#Function
def settransactiondetails(sessionId,msisdn,amount,language):
    #Some Code here
    #And more code here
    return {'sessionId':sid,'responseCode':0}

# register the user function
dispatcher.register_function('InitiateTransfer', settransactiondetails,
    returns={'sessionId': str,'responseCode':int}, 
    args={'sessionId': str,'msisdn': str,'amount': str,'language': str})

logging.info("Starting server...")
httpd = HTTPServer(("", 8050),SOAPHandler)
httpd.dispatcher = dispatcher
httpd.serve_forever()

How would I need to change the code above so as to make it accessible on apache2 via wsgi. You can also include the changes that I would need to make on the /etc/apache2/sites-available/default file.

Upvotes: 0

Views: 2428

Answers (1)

jhnwsk
jhnwsk

Reputation: 953

The wsgi specification says that all you need to do in your python script is simply expose your wsgi app in a variable named application like so:

#add this after you define the dispatcher
application = WSGISOAPHandler(dispatcher)

Then place your script somewhere safe for apache like /usr/local/www/wsgi-scripts/ and in your sites-available add a WSGIScriptAlias directive which will tell Apache wsgi script handler where to look for your script and the application it should run within it.

WSGIScriptAlias /your_app_name /usr/local/www/wsgi-scripts/your_script_file

<Directory /usr/local/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>

And it should work fine assuming you have mod_wsgi installed and pysimplesoap in your pythonpath. Also remember that when using mod_wsgi you should probably change your dispatcher.location and dispatcher.action use the paths that Apache uses. This information will stay in your wsdl definition whether you use Apache or not.

If you want to keep the possibility of running your app stand-alone, replace your HTTPServer section

logging.info("Starting server...")
httpd = HTTPServer(("", 8050),SOAPHandler)
httpd.dispatcher = dispatcher
httpd.serve_forever()

with this:

if __name__=="__main__":
    print "Starting server..."
    from wsgiref.simple_server import make_server
    httpd = make_server('', 8050, application)
    httpd.serve_forever()

If you need more information consult the doc for wsgi in simple soap and the mod_wsgi guide.

Upvotes: 3

Related Questions