如何在apache2上使用wsgi设置pysimplesoap服务器?
我有一个SOAP服务器,现在是作为一个独立的应用程序运行的,也就是直接执行 python mysoapserver.py
来启动它。
不过,我想让它通过apache2来访问,使用wsgi这个技术。
下面是当前代码的一些片段:
导入部分:
from pysimplesoap.server import SoapDispatcher, SOAPHandler, WSGISOAPHandler
代码片段
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()
我需要如何修改上面的代码,才能让它通过apache2和wsgi来访问?你也可以告诉我需要在 /etc/apache2/sites-available/default
文件中做哪些更改。
1 个回答
3
wsgi规范说明,在你的Python脚本中,只需要把你的wsgi应用放在一个名为application的变量里,像这样:
#add this after you define the dispatcher
application = WSGISOAPHandler(dispatcher)
然后把你的脚本放在Apache可以安全访问的地方,比如/usr/local/www/wsgi-scripts/
。接着在你的站点配置中添加一个WSGIScriptAlias指令,这样Apache就知道去哪里找你的脚本,以及要运行哪个应用。
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>
只要你安装了mod_wsgi,并且pysimplesoap在你的Python路径中,这样应该就能正常工作了。另外,记得在使用mod_wsgi时,可能需要把dispatcher.location
和dispatcher.action
改成Apache使用的路径。这些信息会保留在你的wsdl定义中,无论你是否使用Apache。
如果你想让你的应用能够独立运行,可以把你的HTTPServer部分替换成:
logging.info("Starting server...")
httpd = HTTPServer(("", 8050),SOAPHandler)
httpd.dispatcher = dispatcher
httpd.serve_forever()
用这个:
if __name__=="__main__":
print "Starting server..."
from wsgiref.simple_server import make_server
httpd = make_server('', 8050, application)
httpd.serve_forever()
如果你需要更多信息,可以查看简单SOAP中wsgi的文档和mod_wsgi指南。