python 和php的数据通信_python服务端 和 php客户端通信一 | 学步园

本文介绍了如何使用Thrift进行Python服务端与PHP客户端的数据通信。通过创建Thrift文件定义服务接口,然后在Python端实现服务并启动服务器,PHP端则通过Thrift库连接到服务器并调用服务获取数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 通过thrift

但是数据量太大thrift不支持

下载thrift包安装,比如我的是/usr/local/thrift-0.8.0

/usr/local/thrift-0.8.0/lib类库

/usr/local/thrift-0.8.0/test是你需要的教程 可以通过教材方便使用thrift

通信thrift文件

helloworld .thrift

你要写什么格式可以参照  test里面

service HelloWorld{

string sayHello(1:string content, 2:string xpath)

}

thrift -gen python xxx.py

thrift -gen php xxx.py

python服务端 。

test.py

import sys

2 sys.path.append('/var/www/thrift/gen-py')

3 from helloworld import HelloWorld

4 from helloworld.ttypes import *

5 from thrift.transport import TSocket

6 from thrift.transport import TTransport

7 from thrift.protocol import TBinaryProtocol

8 from thrift.server import TServer

9 import socket

10 from lxml.html.soupparser import fromstring

11

12 class HelloWorldHandler:

13     def __init__(self):

14         self.log = {}

15

16     #def sayHello(self, content, xpath):

17         #content = """

why len(y) ==< 1sample."""

18     def sayHello(self, content, xpath):

19         root = fromstring(content)

20         find_text = root.xpath(xpath)

21         return find_text[0].text

22

23 handler = HelloWorldHandler()

24 processor = HelloWorld.Processor(handler)

25 transport = TSocket.TServerSocket('127.0.0.1', 393939)

26 tfactory = TTransport.TBufferedTransportFactory()

27 pfactory = TBinaryProtocol.TBinaryProtocolFactory()

28 server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

29 print 'start server'

30 server.serve()

test.php

2 $GLOBALS['THRIFT_ROOT'] = dirname(__FILE__).'/Thrift';

3 require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';

4 require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';

5 require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';

6 require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';

7 require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';

8

9 // Your gen-php dir

10 $GEN_DIR = dirname(__FILE__).'/gen-php';

11 require_once $GEN_DIR . '/helloworld/HelloWorld.php';

12 require_once $GEN_DIR . '/helloworld/helloworld_types.php';

13

14 // Set server host and port

15 $host = "127.0.0.1";

16 $port = 393939;

17

18 try {

19

20     //Thrift connection handling

21     $socket = new TSocket( $host , $port, true );

22     $transport = new TBufferedTransport($socket, 1024, 1024);

23     $protocol = new TBinaryProtocol($transport);

24     #echo "

";

25     #print_r($socket);

26     #print_r($transport);

27     #print_r($protocol);

28     // get our example client

29     $client = new HelloWorldClient($protocol);

30     $transport->open();

31

32     #$in = new ThriftTest_Page();

33     // Get current timestamp from server

34     $content = file_get_contents("http://www.baidu.com/");

35     $content = mb_convert_encoding($content, "UTF-8");

36 //  $content = "

why len(y) ==< 1sample.";

37 //  die;

38     $xpath = "//title";

39     #$in->content = $content;

40     #$in->xpath = $xpath;

41

42     $return = $client->sayHello($content, $xpath);

43 //  $return = $client->sayHello($content, $xpath);

44     echo $return;

45

46     $transport->close(); } catch (TException $tx) {

49     print 'Something went wrong: '.$tx->getMessage()."\n";

50 }

51 ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值