apache + python cgiでxmlrpc

最終更新日時:2011-08-19 21:56

初版公開日時:2011-08-19 21:56

タグ: linux python

apacheの設定をします。mod_pythonで実行させる場合はお好みで設定してください。(だぶん動くと思う)

# apt-get update
# apt-get install apache2

サーバのcgiプログラムを書きます。"/usr/lib/cgi-bin/test-svr.py"に755権限で配置します。

#!/usr/bin/python

import os
from SimpleXMLRPCServer import CGIXMLRPCRequestHandler

class MyFuncs:
    def add(self, x, y):
        return x + y
    def arrays(self, x, y):
        return [x, y]

handler = CGIXMLRPCRequestHandler()
handler.register_instance(MyFuncs())

handler.register_introspection_functions()
handler.handle_request()

クライアントのプログラムを書きます。


#!/usr/bin/python

import xmlrpclib

server = xmlrpclib.ServerProxy('http://localhost/cgi-bin/test-svr.cgi')

list = server.system.listMethods()
print list

res = server.add(9, 3)
print res
print

res = server.arrays(9, 3)
print res
for i in res:
  print i
print

クライアントプログラムを実行します。


$ python test-cl.py
['add', 'arrays', 'system.listMethods', 'system.methodHelp', 'system.methodSignature']
12

[9, 3]
9
3

auther: dictoss

地元の北海道から流れて大阪へ行き、現在は東京でお仕事中。 仕事ではCentOS、家ではDebianとFreeBSDを使い何かしようと画策中。

github.com

© 2005-2024 dictoss.

counter: 494231