0

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.

Phil
  • 2,859
  • 5
  • 20
  • 21

1 Answers1

3

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.

jhnwsk
  • 971
  • 12
  • 15
  • Thanks for this. But what what code would I need to add / replace for Apache to be able to run this application? I don't believe that it's the " from wsgiref.simple_server import make_server httpd = make_server('', 8050, application) httpd.serve_forever()" That you've shared. That looks like I would still be running it as a stand alone application rather than using Apache. – Phil Apr 17 '14 at 09:25
  • @Phil you are correct, I added this code so that you retain the possibility to run your script stand-alone. For Apache to run this application the only python code you need is the `application` variable definition. That's how wsgi and mod_wsgi work - the `WSGIScriptAlias` directive tells Apache where it should look for a script containing this application definition to run. You also need to have mod_wsgi installed of course :) You can read some more about it here http://modwsgi.readthedocs.org/en/latest/configuration-directives/WSGIScriptAlias.html – jhnwsk Apr 17 '14 at 09:35
  • @Phil I've edited my answer according to your comment, hopefully it's more informative now. – jhnwsk Apr 17 '14 at 09:42
  • Thank you @jhnwsk - I have now understood this. Thanks a lot for the help! I'll try it out today and get back to you. You have earned the bounty! :-) – Phil Apr 18 '14 at 06:59