PHP通过ice调用python程序
PHP能调用python程序是不是一件很爽的事情,有很多大型的处理简单用PHP是不能完成,我们就可以通过ice中间件调用python来完成。首先创建一个Slice文件Hello.ice
#ifndef HELLO_ICE
#define HELLO_ICE
module Demo
{
interface Hello
{
["cpp:const"] idempotent void sayHello(int delay);
["cpp:const"] idempotent string returnHello();
string test(string name);
#define HELLO_ICE
module Demo
{
interface Hello
{
["cpp:const"] idempotent void sayHello(int delay);
["cpp:const"] idempotent string returnHello();
string test(string name);
void shutdown();
};
};
};
};
#endif
有了接口文件,开始创建一个python服务端Server.py,代码如下
import sys, traceback, time, Ice
s12全球总决赛举办地 Ice.loadSlice(“Hello.ice”)
import Demo
class HelloI(Demo.Hello):
def sayHello(self, delay, current=None):
s12全球总决赛举办地 Ice.loadSlice(“Hello.ice”)
import Demo
class HelloI(Demo.Hello):
def sayHello(self, delay, current=None):
if delay != 0:
time.sleep(delay / 1000.0)
print "Hello World! aaaaaaaa"
def returnHello(self, current=None):
print current
return “hello”
def test(self, name, current=None):
return name
def shutdown(self, current=None):
Communicator().shutdown()
class Server(Ice.Application):
time.sleep(delay / 1000.0)
print "Hello World! aaaaaaaa"
def returnHello(self, current=None):
print current
return “hello”
def test(self, name, current=None):
return name
def shutdown(self, current=None):
Communicator().shutdown()
class Server(Ice.Application):
def run(self, args):
if len(args) > 1: #文件编译不能带参数
print self.appName() + ": too many arguments"
return 1
kali linux虚拟机安装教程
adapter = selfmunicator().createObjectAdapter("Hello")
adapter.add(HelloI(), selfmunicator().stringToIdentity("hello"))
adapter.activate()
selfmunicator().waitForShutdown() #挂起等待
return 0
sys.stdout.flush()
app = Server()
it(app.main(sys.argv, "config.server"))
if len(args) > 1: #文件编译不能带参数
print self.appName() + ": too many arguments"
return 1
kali linux虚拟机安装教程
adapter = selfmunicator().createObjectAdapter("Hello")
adapter.add(HelloI(), selfmunicator().stringToIdentity("hello"))
adapter.activate()
selfmunicator().waitForShutdown() #挂起等待
return 0
sys.stdout.flush()
app = Server()
it(app.main(sys.argv, "config.server"))
如果不知道ice怎么用的孩子可以搜索“ice教程.pdf”,我也还在入门中。下面给出PHP
代码,有很多方法,用于属性ice操作,聪明的你肯定不 难。其中值得说明的是oneway相当于异步,twoway相当于同步。如果有返回值就不能使用oneway。最常见的方法是:通过一个oneway开启 处理,再通过一个twoway获取事务状态。
Ice_loadProfile(“hello”);
//
// Change this to true if SSL is configured for the PHP extension.
css的介绍 //
$have_ssl = false;
$res=”";
if(isset($_POST["submitted"]))
{
echo "<HR>
java string转json
//
// Change this to true if SSL is configured for the PHP extension.
css的介绍 //
$have_ssl = false;
$res=”";
if(isset($_POST["submitted"]))
{
echo "<HR>
java string转json
";
echo "<P>Status:<BR><B>
";
try
{
if($have_ssl)
{
$p = $ICE->stringToProxy("hello:tcp -p 10000:udp -p 10000:ssl -p 10001");
}
else
{
$p = $ICE->stringToProxy("hello:tcp -p 60012:udp -p 60012");
}
if(isset($_POST["mode"]))
echo "<P>Status:<BR><B>
";
try
{
if($have_ssl)
{
$p = $ICE->stringToProxy("hello:tcp -p 10000:udp -p 10000:ssl -p 10001");
}
else
{
$p = $ICE->stringToProxy("hello:tcp -p 60012:udp -p 60012");
}
if(isset($_POST["mode"]))
{
if($_POST["mode"] == "oneway")
{
$p = $p->ice_oneway();
}
elseif($_POST["mode"] == "datagram")
{
$p = $p->ice_datagram();
}
}
$delay = 0;
if(isset($_POST["secure"]) and $_POST["secure"] == "yes")
{
if($_POST["mode"] == "oneway")
{
$p = $p->ice_oneway();
}
elseif($_POST["mode"] == "datagram")
{
$p = $p->ice_datagram();
}
}
$delay = 0;
if(isset($_POST["secure"]) and $_POST["secure"] == "yes")
{
$p = $p->ice_secure(true);
}
if(isset($_POST["timeout"]) and $_POST["timeout"] == "yes")
{
$p = $p->ice_timeout(2000);
}
if(isset($_POST["delay"]) and $_POST["delay"] == "yes")
{
$delay = 2500;
}
if($p->ice_isTwoway())
{
$hello = $p->ice_checkedCast("::Demo::Hello");
}
}
if(isset($_POST["timeout"]) and $_POST["timeout"] == "yes")
{
$p = $p->ice_timeout(2000);
}
if(isset($_POST["delay"]) and $_POST["delay"] == "yes")
{
$delay = 2500;
}
if($p->ice_isTwoway())
{
$hello = $p->ice_checkedCast("::Demo::Hello");
}
else
{
$hello = $p->ice_uncheckedCast("::Demo::Hello");
}
if(isset($_POST["sayHello"]))
{
$hello->sayHello($delay);
$res=$hello->test(“this string return from python”); //我Eng很烂
}
python入门教程app elseif(isset($_POST["shutdown"]))
{
$hello->shutdown();
}
{
$hello = $p->ice_uncheckedCast("::Demo::Hello");
}
if(isset($_POST["sayHello"]))
{
$hello->sayHello($delay);
$res=$hello->test(“this string return from python”); //我Eng很烂
}
python入门教程app elseif(isset($_POST["shutdown"]))
{
$hello->shutdown();
}
echo "OK
delay同义词 ";
}
catch(Ice_LocalException $ex)
{
echo "<pre>
";
print_r($ex);
echo "</pre>
";
}
echo "</B></P>
";
echo "<HR>
";
delay同义词 ";
}
catch(Ice_LocalException $ex)
{
echo "<pre>
";
print_r($ex);
echo "</pre>
";
}
echo "</B></P>
";
echo "<HR>
";
}
?>
<P>
<?=$res;?>
</P>
<P>
<FORM method="POST" action="<?php echo basename($_SERVER["PHP_SELF"]); ?>">
<P>Mode:
<INPUT type="radio" name="mode" value="twoway"
<?php if(!isset($_POST["mode"]) or $_POST["mode"] == "twoway") echo " checked "; ?>
> Twoway
<INPUT type="radio" name="mode" value="oneway"
<?php if(isset($_POST["mode"]) and $_POST["mode"] == "oneway") echo " check
?>
<P>
<?=$res;?>
</P>
<P>
<FORM method="POST" action="<?php echo basename($_SERVER["PHP_SELF"]); ?>">
<P>Mode:
<INPUT type="radio" name="mode" value="twoway"
<?php if(!isset($_POST["mode"]) or $_POST["mode"] == "twoway") echo " checked "; ?>
> Twoway
<INPUT type="radio" name="mode" value="oneway"
<?php if(isset($_POST["mode"]) and $_POST["mode"] == "oneway") echo " check
ed "; ?>
> Oneway
<INPUT type="radio" name="mode" value="datagram"
<?php if(isset($_POST["mode"]) and $_POST["mode"] == "datagram") echo " checked "; ?>
> Datagram
</P>
<P>Options:
<INPUT type="checkbox" name="secure" value="yes"
<?php if(isset($_POST["secure"]) and $_POST["secure"] == "yes") echo " checked "; ?>
<?php if(!$have_ssl) echo " disabled "; ?>
> Secure
<INPUT type="checkbox" name="timeout" value="yes"
<?php if(isset($_POST["timeout"]) and $_POST["timeout"] == "yes") echo " check
> Oneway
<INPUT type="radio" name="mode" value="datagram"
<?php if(isset($_POST["mode"]) and $_POST["mode"] == "datagram") echo " checked "; ?>
> Datagram
</P>
<P>Options:
<INPUT type="checkbox" name="secure" value="yes"
<?php if(isset($_POST["secure"]) and $_POST["secure"] == "yes") echo " checked "; ?>
<?php if(!$have_ssl) echo " disabled "; ?>
> Secure
<INPUT type="checkbox" name="timeout" value="yes"
<?php if(isset($_POST["timeout"]) and $_POST["timeout"] == "yes") echo " check
ed "; ?>
> Timeout
<INPUT type="checkbox" name="delay" value="yes"
<?php if(isset($_POST["delay"]) and $_POST["delay"] == "yes") echo " checked "; ?>
> Delay
</P>
<P>
<INPUT type="hidden" name="submitted" value="yes">
<INPUT type="submit" name="sayHello" value="Say Hello">
<INPUT type="submit" name="shutdown" value="Shutdown">
</FORM>
> Timeout
<INPUT type="checkbox" name="delay" value="yes"
<?php if(isset($_POST["delay"]) and $_POST["delay"] == "yes") echo " checked "; ?>
> Delay
</P>
<P>
<INPUT type="hidden" name="submitted" value="yes">
<INPUT type="submit" name="sayHello" value="Say Hello">
<INPUT type="submit" name="shutdown" value="Shutdown">
</FORM>
要使整个过程可以运行,把Hello.ice文件移动到php配置的ice的放置目录下,如,在/etc/php.d中查看ice.ini获取ice文件 的存放地址。现在通过终端运行:python Server.py &(这个&是让程序在后台执行,可以通过jobs命令查看,通过kill % 编号关闭)。到此完
了
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论