Skip to main content
 首页 » 编程设计

python之如何在apache上部署python webservice

2025年05月04日21Renyi-Fan

我是 Python 的新手。我有一个简单的 web 服务,如下所示:

enter code here 
 
import soaplib 
from soaplib.core.service import rpc, DefinitionBase 
from soaplib.core.model.primitive import String, Integer 
from soaplib.core.server import wsgi 
from soaplib.core.model.clazz import Array 
from soaplib.core.service import soap 
 
class HelloWorldService(DefinitionBase): 
 
    @soap(String,Integer,_returns=Array(String)) 
    def say_hello(self,name,times): 
        results = [] 
        for i in range(0,times): 
            results.append('Hello, %s'%name) 
        return results 
if __name__=='__main__': 
 
    try: 
 
        from wsgiref.simple_server import make_server 
        soap_application = soaplib.core.Application([HelloWorldService], 'tns') 
        wsgi_application = wsgi.Application(soap_application) 
        server = make_server('10.44.138.231', 9999, wsgi_application) 
        server.serve_forever() 
 
    except ImportError: 
 
        print "Error: example server code requires Python >= 2.5" 

当我在本地访问服务时速度非常快,但从局域网中的另一台主机访问服务时会变得非常慢。

所以我想把这个程序部署到apache中,但是好像很难,我在google里搜索了很久,现在弄得我很累。谁能帮帮我,谢谢

请您参考如下方法:

我建议使用 mod_wsgi (而不是 mod_python),因为 WSGI 是托管 Python 网络应用程序的标准方式。

在你的情况下,你需要在全局范围内有一个名为 application 的函数:

        # .... 
        return results 
 
soap_application = soaplib.core.Application([HelloWorldService], 'tns') 
application = wsgi.Application(soap_application) 
 
if __name__ == "__main__": 
    # .... 

然后您在 Apache 中启用 mod_wsgi 并添加指令(WSGIScriptAlias 是主要的)。 help pages如果您之前配置过 Apache,则可以合理访问。