Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Curl and python httplib?

Thread Tools

Re: Curl and python httplib?

Chris Rebert
Posts: n/a
On Wed, Mar 27, 2013 at 7:54 PM, 小**PHP <> wrote:
> Guys,
> I take a project that need send request to Hadoop by curl.
> But now, the curl and pycurl can't satisfy my project. So i need use the
> powerful httplib.

I would say that `requests`
( ) is generally preferable
to httplib these days.

> But failed.
> my curl request:
> curl -i -X PUT "http://localhost:50070/webhdfs/v1/levi/7?op=CREATE"
> my return:
> Content-Type: application/octet-stream
> Location:
> Content-Length: 0
> Server: Jetty(6.1.26)
> Now, i change the curl request to httplib:
> import httplib
> import urllib
> params=urllib.urlencode({"@op":"CREATE","@user.nam e":"levi"})
> headers={"Content-type": "application/x-www-form-urlencoded","Accept":
> "text/plain"}
> conn=httplib.HTTPConnection("localhost:50070")
> conn.request("PUT","/webhdfs/v1/levi/7.txt",params,headers)
> response=conn.getresponse()
> print response.status, response.reason
> print data
> conn.close()
> But it failed:
> #print response.status, response.reason
> 500 Internal Server Error
> #print data
> '{"RemoteException":{"exception":"WebApplicationEx ception","javaClassName":" onException","message":null}}'
> Who knows why? It's OK when i use curl, so where is the problem in httplib
> method?
> Or some other reasons?

It's rather hard to say when neither the base URL, nor querystring
parameters, nor request body are the same between your curl example
and your httplib example, making them rather incomparable.
Part of the problem may be that you are passing what may have been
intended as querystring parameters (`params`) as the `body` argument
to HTTPConnection.request().
In any case, I would suggest trying to use the `requests` library instead.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Curl and python httplib? Mark Lawrence Python 2 04-01-2013 10:07 PM
CURL problem and System call in Paralles PBA pakalk Perl Misc 7 03-28-2009 07:49 PM
Page Curl Javascript wtd G HTML 10 06-07-2007 09:34 PM
curl and popen2 Python 1 02-02-2005 05:57 AM
cURL in ASP.NET Chris ASP .Net 1 12-19-2004 04:59 PM