一.简介
从前,有个很流行的接口叫CORBA,在java的世界里用的比较广泛,但是我要做的是用python来操作它,按照惯例,先搭环境。 需要准备的东西: 1.fedora 20 2.python2.7 3.omniORB-4.2.0.tar.bz2 4.omniORBpy-4.2.0.tar.bz2 注:omniORBpy使用了omniORB的c++库,所以4.2.x版的omniORBpy只能用4.2.x版的omniORB。二.安装配置omniORB-4.2.0
1.先解压缩包:
tar -xvf omniORB-4.2.0.tar.bz2
2.安装和配置
你可以编译安装到源代码目录中,但是不建议这样做,一般都是新建一个不同目录来编译安装。本文中新建一个build目录。
cd <span style="font-family: Arial, Helvetica, sans-serif;">[omniORB directory]</span>注:配置中的[configure options]有很多选项,最常用的是 --prefix,用来选择安装位置。默认的安装位置是:/usr/local。可以自定义,如:
mkdir build
cd build
../configure [configure options] #配置
make #编译
make install #安装
../configure --prefix=/home/xxx/my_omniorb同时也可以用PYTHON来指定要使用的python,如:
../configure PYTHON=/usr/bin/python2.7注:有的系统可能make和make install操作需要sudo来执行,视情况而定吧。到这里么omniORB就安装完成了哦。
三.安装配置mniORBpy-4.2.0.tar.bz2
1.同样先解压:tar -xvf omniORBpy-4.2.0.tar.bz22.安装
cd [omniORBpy directory]与上边的基本一致,选择默认安装位置以及使用的PYTHON。 3.配置 如果你安装的时候使用的是自动配置的脚本,即用../configure,并且使用的是默认的安装路径。 则在系统中配置如下环境变量:
mkdir build
cd build
../configure [configure options]
make
make install
export PYTHONPATH=/usr/local/lib/python2.7/site-packages注:我用的是python2.7版本,其他的版本的修改一下即可。 4.后续 按照文档已经全部配置完毕,按道理现在就可以正常使用了,但是坑爹的来了,你一执行必报错,而且网上相关资料非常少,很难搜索,这也是我写这篇博客的原因。
四.使用omniORBpy
1.上边都已经安装配置好了,你打开python解释器,输入:import CORBA很抱歉。完美的结果木有出现,报错啦:
或者
from omniORB import CORBA
>>> import CORBA这种错误就很难搜索和解决了,经过数天的奋战终于悟出解决方法:将你安装目录下的64位库下的python也加入环境变量:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/CORBA.py", line 3, in <module>
import sys, omniORB.CORBA
File "/usr/local/lib/python2.7/site-packages/omniORB/__init__.py", line 46, in <module>
import _omnipy
ImportError: No module named _omnipy
export PYTHONPATH=/usr/local/lib64/python2.7/site-packages2.上边的问题解决了,重新试一下看看,妈蛋,又报错了:
>>> import CORBA仔细瞅瞅错误信息,导入错误,没找到这个库文件或者目录。好像跟安装python操作Oracle数据库的模块cx_Oracle有点类似啊,解决办法是把库文件所在目录加入到环境变量的LD_LIBRARY_PATH中,库文件的目录也是在omniORB的默认安装目录中找,这里我的是:/usrs/local/lib,所以要做的是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/CORBA.py", line 3, in <module>
import sys, omniORB.CORBA
File "/usr/local/lib/python2.7/site-packages/omniORB/__init__.py", line 46, in <module>
import _omnipy
ImportError: libomniORB4.so.2: cannot open shared object file: No such file or directory
export LD_LIBRARY_PATH=/usr/local/lib在试试,哇塞,终于好了:
Python 2.7.5 (default, Jun 25 2014, 10:19:55)结束,睡觉!!
[GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import CORBA
>>> from omniORB import CORBA
>>>
2015年2月8日更新:使用omniORB的命名服务NameService
在使用omniORB的命名服务omniNames的时候又出错了:[zpf@localhost ~]$ omniNames好吧,这个错误一看就明白,要访问的文件不存在,去看看,发现/var目录下根本没木有omninames这个文件夹。 再带参数启动一下看详细信息:
omniNames: (0) 2015-02-08 20:13:07.284041: Error: cannot open data file '/var/omninames/omninames-localhost.localdomain.dat': No such file or directory
[zpf@localhost ~]$
[zpf@localhost ~]$ omniNames -start还是报文件不存在,这里还提示说要设定一下环境变量指导这个目录:
omniNames: (0) 2015-02-08 20:16:07.790368: Data file: '/var/omninames/omninames-localhost.localdomain.dat'.
omniNames: (0) 2015-02-08 20:16:07.791915: Starting omniNames for the first time.
omniNames: (0) 2015-02-08 20:16:07.792334: Error: cannot create initial data file '/var/omninames/omninames-localhost.localdomain.dat': No such file or directory
omniNames: (0) 2015-02-08 20:16:07.792564: You can set the environment variable OMNINAMES_DATADIR to specify the directory where the data files are kept.
[zpf@localhost ~]$
[zpf@localhost ~]$ mkdir /var/omninames普通用户还没有创建的权限,那就用sudo吧,或者切换到root用户也可以:
mkdir: cannot create directory ‘/var/omninames’: Permission denied
sudo mkdir /var/omninames文件夹创建好了,那么设置环境变量:
OMNINAMES_DATADIR=/var/omninames好了,都设置好,开始执行吧:
export OMNINAMES_DATADIR
[zpf@localhost omninames]$ omniNames哦,文件夹建好了,但是里边没文件,先来创建一下文件:
omniNames: (0) 2015-02-08 20:28:52.104343: Error: cannot open data file '/var/omninames/omninames-localhost.localdomain.dat': No such file or directory
[zpf@localhost omninames]$ omniNames -start你妹的,没权限创建文件,那就sudo吧:
omniNames: (0) 2015-02-08 20:29:05.391403: Data file: '/var/omninames/omninames-localhost.localdomain.dat'.
omniNames: (0) 2015-02-08 20:29:05.393639: Starting omniNames for the first time.
omniNames: (0) 2015-02-08 20:29:05.394117: Error: cannot create initial data file '/var/omninames/omninames-localhost.localdomain.dat': Permission denied
omniNames: (0) 2015-02-08 20:29:05.394535: You can set the environment variable OMNINAMES_DATADIR to specify the directory where the data files are kept.
[zpf@localhost omninames]$ sudo omniNames -start加上sudo后这个都不是命令了,那我们就只能切换到root用户,用最高权限来干事情,我的电脑我做主:
sudo: omniNames: command not found
[zpf@localhost omninames]$ su root嗯?又找不到库文件了? 这个问题是不是似曾相识?对的,上边刚碰到过嘛,想一想当时我们设置的环境变量是在当前用户下,现在是在root用户下那个环境变量已经失效了,好吧,先让环境变量生效:
Password:
[root@localhost omninames]# omniNames -start
omniNames: error while loading shared libraries: libomniORB4.so.2: cannot open shared object file: No such file or directory
[root@localhost omninames]#
[root@localhost omninames]# source ~/.bashrc这样还是不行,要先在本地用户的初始目录下,然后切换到root用户,再生效:
[root@localhost zpf]# source ./.bashrc然后再试试启动:
[root@localhost zpf]# omniNames -startOK了,可以看到,启动成功,文件也创建成功。 以后就可以不带参数直接执行:
omniNames: (0) 2015-02-08 20:39:40.846482: Data file: '/var/omninames/omninames-localhost.localdomain.dat'.
omniNames: (0) 2015-02-08 20:39:40.846777: Starting omniNames for the first time.
omniNames: (0) 2015-02-08 20:39:40.847501: Wrote initial data file '/var/omninames/omninames-localhost.localdomain.dat'.
omniNames: (0) 2015-02-08 20:39:40.847869: Read data file '/var/omninames/omninames-localhost.localdomain.dat' successfully.
omniNames: (0) 2015-02-08 20:39:40.849364: Root context is IOR:010000002b00000049444c3a6f6d672e6f72672f436f734e616d696e672f4e616d696e67436f6e746578744578743a312e30000001000000000000006c000000010102000a00000031302e302e322e313500f90a0b0000004e616d6553657276696365000300000000000000080000000100000000545441010000001c0000000100000001000100010000000100010509010100010000000901010003545441080000000c59d75401000be6
omniNames: (0) 2015-02-08 20:39:40.849802: Checkpointing Phase 1: Prepare.
omniNames: (0) 2015-02-08 20:39:40.850082: Checkpointing Phase 2: Commit.
omniNames: (0) 2015-02-08 20:39:40.850351: Checkpointing completed.
[root@localhost zpf]# omniNames那么在普通用户下怎么样,可以执行吗,来试试:
omniNames: (0) 2015-02-08 20:41:01.909215: Data file: '/var/omninames/omninames-localhost.localdomain.dat'.
omniNames: (0) 2015-02-08 20:41:01.909899: Read data file '/var/omninames/omninames-localhost.localdomain.dat' successfully.
omniNames: (0) 2015-02-08 20:41:01.910054: Root context is IOR:010000002b00000049444c3a6f6d672e6f72672f436f734e616d696e672f4e616d696e67436f6e746578744578743a312e30000001000000000000006c000000010102000a00000031302e302e322e313500f90a0b0000004e616d6553657276696365000300000000000000080000000100000000545441010000001c0000000100000001000100010000000100010509010100010000000901010003545441080000000c59d75401000be6
omniNames: (0) 2015-02-08 20:41:01.910200: Checkpointing Phase 1: Prepare.
omniNames: (0) 2015-02-08 20:41:01.910460: Checkpointing Phase 2: Commit.
omniNames: (0) 2015-02-08 20:41:01.914563: Checkpointing completed.
[zpf@localhost ~]$ omniNames似乎又失败了,不能将IOR写入文件中。加上sudo试试:
omniNames: (0) 2015-02-08 20:42:24.266673: Data file: '/var/omninames/omninames-localhost.localdomain.dat'.
omniNames: (0) 2015-02-08 20:42:24.268084: Read data file '/var/omninames/omninames-localhost.localdomain.dat' successfully.
omniNames: (0) 2015-02-08 20:42:24.268573: Root context is IOR:010000002b00000049444c3a6f6d672e6f72672f436f734e616d696e672f4e616d696e67436f6e746578744578743a312e30000001000000000000006c000000010102000a00000031302e302e322e313500f90a0b0000004e616d6553657276696365000300000000000000080000000100000000545441010000001c0000000100000001000100010000000100010509010100010000000901010003545441080000000c59d75401000be6
omniNames: (0) 2015-02-08 20:42:24.269191: Error: cannot open data file '/var/omninames/omninames-localhost.localdomain.dat' for writing.
[zpf@localhost ~]$ sudo omniNames又是这样子,都不是命令了,看来不行,再试试带参数:
[sudo] password for zpf:
sudo: omniNames: command not found
[zpf@localhost ~]$ omniNames -start文件已经存在,所以参数失效,以后就是直接命令启动读写文件,不需要创建了。 看来只能在root用户下启动Naming Service命名服务,然后让程序来使用,普通用户下是无法启动的。 注:要使用命名服务就先切换到root用户启动命名服务,然后让它后台运行,我们普通用户程序来调用使用即可。
omniNames: (0) 2015-02-08 20:43:26.732348: Error: data file '/var/omninames/omninames-localhost.localdomain.dat' exists. Can't use -start option.