Can Python "httplib" module be directly embedded in C++ ?

Hello, I am trying to use python to do my XML post ? Can I do this by directly calling import on the httplib like this and setting parameters ? ex: boost::python::object http_module((boost::python::handle<>(boost::python::borrowed(PyImport_Ad dModule("httplib"))))); boost::python::<....some kind of python compile time...interface...> h = < some kind of cast...> http_module.HTTP("http://www.abc.com"); h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() h.send(body) Or should the actual code for this object reside in a text file and I should call exec_file ? Thanks in Advance. - Raja

on Tue Aug 14 2007, "Raja Cherukuri" <raja-AT-transerainc.com> wrote:
Hello, I am trying to use python to do my XML post ? Can I do this by directly calling import on the httplib like this and setting parameters ?
ex: boost::python::object http_module((boost::python::handle<>(boost::python::borrowed(PyImport_Ad dModule("httplib")))));
boost::python::object http_module = boost::python::import("httplib");
boost::python::<....some kind of python compile time...interface...> h = < some kind of cast...>
http_module.HTTP("http://www.abc.com");
http_module.attr("HTTP")("http://www.abc.com");
h.putrequest('POST', selector)
ditto, etc.
h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() h.send(body)
Or should the actual code for this object reside in a text file and I should call exec_file ?
You can do that too. HTH, -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com

Thanks it works fine...how do check the return response... Using extract<string> or <extract<char*> doesn't seem to help.. Am I doing something wrong ? Here is the code: boost::python::object response = gpPythonHttp->attr("urlopen")(m_sURI.c_str(),strXMLOutput.c_str()); boost::python::exec("result = read()", response); std::string res = boost::python::extract<std::string>(response["result"]); -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of David Abrahams Sent: Tuesday, August 14, 2007 4:15 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Can Python "httplib" module be directly embeddedin C++ ? on Tue Aug 14 2007, "Raja Cherukuri" <raja-AT-transerainc.com> wrote:
Hello, I am trying to use python to do my XML post ? Can I do this by directly calling import on the httplib like this and setting parameters ?
ex: boost::python::object
http_module((boost::python::handle<>(boost::python::borrowed(PyImport_Ad
dModule("httplib")))));
boost::python::object http_module = boost::python::import("httplib");
boost::python::<....some kind of python compile time...interface...> h = < some kind of cast...>
http_module.HTTP("http://www.abc.com");
http_module.attr("HTTP")("http://www.abc.com");
h.putrequest('POST', selector)
ditto, etc.
h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() h.send(body)
Or should the actual code for this object reside in a text file and I should call exec_file ?
You can do that too. HTH, -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

on Fri Aug 17 2007, "Raja Cherukuri" <raja-AT-transerainc.com> wrote:
Thanks it works fine...how do check the return response... Using extract<string> or <extract<char*> doesn't seem to help..
What does it do?
Am I doing something wrong ?
You might try to figure out what's in response by printing it. Maybe respose["result"] is not a string, or else maybe it's unicode...
Here is the code:
boost::python::object response = gpPythonHttp->attr("urlopen")(m_sURI.c_str(),strXMLOutput.c_str()); boost::python::exec("result = read()", response); std::string res = boost::python::extract<std::string>(response["result"]);
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com

I am trying to connect to server that responds with XML. Here is the error I see: Traceback (most recent call last): File "<string>", line 1, in ? TypeError: unsubscriptable object I tried the same thing on windows using serverHttp object in MSXML and get the response Correctly in windows machine. On Linux I am trying to the samething.. Post to a Url and read the response XML. - Raja -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of David Abrahams Sent: Saturday, August 18, 2007 1:15 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Can Python "httplib" module be directlyembeddedin C++ ? on Fri Aug 17 2007, "Raja Cherukuri" <raja-AT-transerainc.com> wrote:
Thanks it works fine...how do check the return response... Using extract<string> or <extract<char*> doesn't seem to help..
What does it do?
Am I doing something wrong ?
You might try to figure out what's in response by printing it. Maybe respose["result"] is not a string, or else maybe it's unicode...
Here is the code:
boost::python::object response = gpPythonHttp->attr("urlopen")(m_sURI.c_str(),strXMLOutput.c_str()); boost::python::exec("result = read()", response); std::string res = boost::python::extract<std::string>(response["result"]);
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

on Mon Aug 20 2007, "Raja Cherukuri" <raja-AT-transerainc.com> wrote:
I am trying to connect to server that responds with XML. Here is the error I see: Traceback (most recent call last): File "<string>", line 1, in ? TypeError: unsubscriptable object
Again, try printing response: print response also, print type(response) That should be revealing. This is not a Boost.Python problem. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com

Thanks it works...was calling extract on the wrong object... I made it work with "urllib2" module also.. Here is the sample of working code, that others can use or improve upon. Example of doing a HTTP post....using python interface from C++ and reading the return response ------------------------------------------------------------------------ ----------------------- // do the following initially... Py_Initialize() ; boost::python::object http = boost::python::import("urllib2"); // the following code can be called any number of times... try { std::strPostBody("<test/>"); // posting xml using http-post..can be other stuff.. boost::python::object response = http .attr("urlopen")(strURI.c_str(),strPostBody.c_str()); boost::python::object read = response.attr("read")(); std::string strResponse = boost::python::extract<char*>(read); } catch(...) { PyErr_Print(); PyErr_Clear(); } - Raja -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of David Abrahams Sent: Monday, August 20, 2007 2:02 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Can Python "httplib" module be directlyembeddedinC++ ? on Mon Aug 20 2007, "Raja Cherukuri" <raja-AT-transerainc.com> wrote:
I am trying to connect to server that responds with XML. Here is the error I see: Traceback (most recent call last): File "<string>", line 1, in ? TypeError: unsubscriptable object
Again, try printing response: print response also, print type(response) That should be revealing. This is not a Boost.Python problem. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
David Abrahams
-
Raja Cherukuri