Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
#include <boost/asio/ip/tcp.hpp>
#include "jsoncpp/json/json.h"
#include <zlib.h>
#include <boost/python.hpp>
using namespace std;
std::string decompress_string(const std::string& str)
z_stream zs; // z_stream is zlib's control structure
memset(&zs, 0, sizeof(zs));
if (inflateInit(&zs) != Z_OK)
throw(std::runtime_error("inflateInit failed while decompressing."));
zs.next_in = (Bytef*);
zs.avail_in = str.size();
int ret;
char outbuffer[32768];
std::string outstring;
// get the decompressed bytes blockwise using repeated calls to inflate
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = inflate(&zs, 0);
if (outstring.size() < zs.total_out) {
zs.total_out - outstring.size());
} while (ret == Z_OK);
if (ret != Z_STREAM_END) { // an error occurred that was not EOF
std::ostringstream oss;
oss << "Exception during zlib decompression: (" << ret << ") "
<< zs.msg;
return outstring;
std::string query_google(string url, string referer, string region, string useragent, string priority, string key)
Json::Value details;
details["url"] = url;
details["referer"] = referer;
details["region"] = region;
details["useragent"] = useragent;
details["priority"] = priority;
Json::Value request;
request["key"] = key;
request["request"] = details;
const char* host = "";
const char* port = "9005";
using namespace boost::asio;
io_service io_service;
ip::tcp::resolver resolver(io_service);
ip::tcp::resolver::query query(host, port);
ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
ip::tcp::resolver::iterator end;
ip::tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
socket.connect(*endpoint_iterator++, error);
Json::StyledWriter writer;
boost::array<char, 120000> buf;
std::string response;
size_t len = 0;
while (len = socket.read_some(boost::asio::buffer(buf), error)) {
std::string sbuf(buf.begin(), len);
return decompress_string(response);
using namespace boost::python;
def("query_google", query_google);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.