


















#include "RecSys.h"
#include <iostream>
#include <string>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
#include <fcgi_stdio.h>
#include <fcgiapp.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace std;
using std::string;
using boost::shared_ptr;
inline void send_response(
FCGX_Request& request, const std::string& resp_str) {
FCGX_FPrintF(request.out, "Content-type: text/html;charset=utf-8\r\n\r\n");
FCGX_FPrintF(request.out, "%s", resp_str.c_str());
FCGX_Finish_r(&request);
}
int main(int argc, char **argv){
// 1.初始化cgi
FCGX_Init();
FCGX_Request request;
FCGX_InitRequest(&request, 0, 0);
// 2.连接connect server rpc
boost::shared_ptr<TSocket> socket(new TSocket("localhost",9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
transport->open();
RecSysClient client(protocol);
while(FCGX_Accept_r(&request) >= 0) {
// http page -> client
std::string send_data = FCGX_GetParam("QUERY_STRING", request.envp);
string receive_data;
// client >> server
// server >> client
client.rec_data(receive_data,send_data);
cout << "receive http params: " << send_data << std::endl;
cout << "receive server data: " << receive_data << endl;
/*send_response(request, return_str);*/
send_response(request, receive_data);
}
transport->close();
return 0;
}
client.cpp文件代码修改为:#include "RecSys.h"
#include <iostream>
#include <string>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
#include <fcgi_stdio.h>
#include <fcgiapp.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace std;
using std::string;
using boost::shared_ptr;
inline void send_response(
FCGX_Request& request, const std::string& resp_str) {
FCGX_FPrintF(request.out, "Content-type: text/html;charset=utf-8\r\n\r\n");
FCGX_FPrintF(request.out, "%s", resp_str.c_str());
FCGX_Finish_r(&request);
}
int main(int argc, char **argv){
// 1.初始化cgi
FCGX_Init();
FCGX_Request request;
FCGX_InitRequest(&request, 0, 0);
// 2.连接connect server rpc
boost::shared_ptr<TSocket> socket(new TSocket("localhost",9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
transport->open();
RecSysClient client(protocol);
while(FCGX_Accept_r(&request) >= 0) {
// http page -> client
std::string send_data = FCGX_GetParam("QUERY_STRING", request.envp);
string receive_data;
// client >> server
// server >> client
client.rec_data(receive_data,send_data);
cout << "receive http params: " << send_data << std::endl;
cout << "receive server data: " << receive_data << endl;
/*send_response(request, return_str);*/
send_response(request, receive_data);
}
transport->close();
return 0;
}
G++ = g++
CFLAGS = -g -Wall
INCLUDES = -I./ -I/usr/local/include/thrift
LIBS = -L/usr/local/lib/*.so -lthrift
SER_OBJECT = RecSys.cpp RecSys_constants.cpp RecSys_types.cpp RecSys_server.skeleton.cpp
CLI_OBJECT = RecSys.cpp client.cpp
server: $(SER_OBJECT)
$(G++) $(CFLAGS) $(INCLUDES) $(SER_OBJECT) $(LIBS) -o server
client: $(CLI_OBJECT)
$(G++) $(CFLAGS) $(INCLUDES) $(CLI_OBJECT) $(LIBS) -o client
.PHONY: clean
clean:
rm -f server client
命令:make server、make client、make clean等会对应执行名相应的命令
安装目录/sbin/nginxnetstat -antup | grep nginx,端口为:80./servernetstat -antup | grep 8088启动之前先编译client.cpp文件,命令:make client
命令:/usr/local/src/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088 -f /test/thrift_test/python_thrift_demo/gen-cpp/client
打开浏览器,查看,页面(也就是客户端)会返回server端输出的内容,同时server端会接收到客户端(也就是浏览器)发送的信息
如图:
带参数:
客户端:
server端:
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。