Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Curl and python httplib?

219 views
Skip to first unread message

小学园PHP

unread,
Mar 27, 2013, 10:54:43 PM3/27/13
to python-list
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.
But failed.

my curl request:

my return:
HTTP/1.1 307 TEMPORARY_REDIRECT
Content-Type: application/octet-stream
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.name":"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
data=response.read()
print data
conn.close()

But it failed:
#print response.status, response.reason
500 Internal Server Error
#print data
'{"RemoteException":{"exception":"WebApplicationException","javaClassName":"javax.ws.rs.WebApplicationException","message":null}}'

Who knows why? It's OK when i use curl, so where is the problem in httplib method?
Or some other reasons?
Who can help me change the curl request to httplib edition?

TIA
Levi

Chris Rebert

unread,
Mar 29, 2013, 12:58:19 AM3/29/13
to python-list
On Wed, Mar 27, 2013 at 7:54 PM, 小学园PHP <xxy...@qq.com> 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`
(http://docs.python-requests.org/en/latest/ ) is generally preferable
to httplib these days.
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.

Cheers,
Chris

小学园PHP

unread,
Mar 30, 2013, 1:08:10 AM3/30/13
to python-list
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.
Who can help me change the curl request to httplib edition?

TIA
Levi

Mark Lawrence

unread,
Apr 1, 2013, 3:33:58 PM4/1/13
to pytho...@python.org
On 30/03/2013 05:08, Сѧ԰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.
> But failed.
>
> *my curl request:*
> *my return:*
> HTTP/1.1 307 TEMPORARY_REDIRECT
> Content-Type: application/octet-stream
> Location:
> http://58.53.211.47:50075/webhdfs/v1/levi/7?op=CREATE&overwrite=false
> 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.name":"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
> data=response.read()
> print data
> conn.close()
>
> *But it failed:*
> #print response.status, response.reason
> 500 Internal Server Error
> #print data
> '{"RemoteException":{"exception":"WebApplicationException","javaClassName":"javax.ws.rs.WebApplicationException","message":null}}'
>
> Who knows why? It's OK when i use curl, so where is the problem in
> httplib method?
> Or some other reasons?
> Who can help me change the curl request to httplib edition?
>
> TIA
> Levi
>
>

Try the requests module https://pypi.python.org/pypi/requests/ as if I
can successfuly use it anybody can :)

--
If you're using GoogleCrap™ please read this
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence

vasudevram

unread,
Apr 1, 2013, 6:07:40 PM4/1/13
to pytho...@python.org
Or try httplib2

GIYF

vasudevram

unread,
Apr 1, 2013, 6:07:40 PM4/1/13
to comp.lan...@googlegroups.com, pytho...@python.org
On Tuesday, April 2, 2013 1:03:58 AM UTC+5:30, Mark Lawrence wrote:
Or try httplib2

GIYF

0 new messages