Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Making HEAD/PUT/DELETE requests with urllib2?

Reply
Thread Tools

Making HEAD/PUT/DELETE requests with urllib2?

 
 
Phillip B Oldham
Guest
Posts: n/a
 
      06-13-2008
In my attempt to learn python in a weekend, I've fallen foul at line
10 of my second scripting attempt. Basically I'm writing a simple
spider, but currently I'm unable to find any documentation on making
HEAD requests using the urllib2 library to test whether a file exists
on a remote webserver.

I've checked the docs on urllib2 from docs.python.org, and unless I'm
missing something there doesn't seem to be a way to do *any* request
other than a GET and POST.

Surely this can't be correct? If so, we're all going to have a hell of
a time creating RESTful web apps.

Any help on the matter would be greatly appreciated.
 
Reply With Quote
 
 
 
 
Jeff McNeil
Guest
Posts: n/a
 
      06-14-2008
The only time I've ever pulled a HEAD request I've used the httplib
module directly. Ought to be able to do it like so:

Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import httplib
>>> c = httplib.HTTPConnection('www.google.com')
>>> c.request('HEAD', '/')
>>> r = c.getresponse()
>>> r.getheader('server')

'gws'
>>> r.status

200
>>>


I don't honestly know if there's a way to do it via urllib(2), though.





On Fri, Jun 13, 2008 at 7:40 PM, Phillip B Oldham
<(E-Mail Removed)> wrote:
> In my attempt to learn python in a weekend, I've fallen foul at line
> 10 of my second scripting attempt. Basically I'm writing a simple
> spider, but currently I'm unable to find any documentation on making
> HEAD requests using the urllib2 library to test whether a file exists
> on a remote webserver.
>
> I've checked the docs on urllib2 from docs.python.org, and unless I'm
> missing something there doesn't seem to be a way to do *any* request
> other than a GET and POST.
>
> Surely this can't be correct? If so, we're all going to have a hell of
> a time creating RESTful web apps.
>
> Any help on the matter would be greatly appreciated.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
 
Phillip B Oldham
Guest
Posts: n/a
 
      06-14-2008
Thanks for the info. That's working like a charm. Looks as though I'll
be able to handle all request types with that object.

I got a little worried then that the python dev's had missed something
truly important!
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      06-14-2008
Phillip B Oldham schrieb:
> Thanks for the info. That's working like a charm. Looks as though I'll
> be able to handle all request types with that object.
>
> I got a little worried then that the python dev's had missed something
> truly important!


I've done that in urrlib2 like this:

class MyRequest(urllib2.Request):
def get_method(self):
if alternate_http_method is not None:
return alternate_http_method
return urllib2.Request.get_method(self)

THe alternate_http_method is part of a class-closure, but of course you
could do that with an instance variable as well.

I then use it like this:

req = MyRequest()
handlers = []
if USE_PROXY:
handlers.append(urllib2.ProxyHandler({'http' : PROXY}))
req = self._create_request(url, connector, urlparams,
queryparams, alternate_http_method)
opener = urllib2.build_opener(*handlers)



Diez
 
Reply With Quote
 
 
 
Reply

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
Making 10 POST requests from ASP.NET asynchronously Mike C# ASP .Net Web Services 13 11-17-2006 03:59 AM
Making 10 POST requests from ASP.NET asynchronously Mike C# ASP .Net 14 11-17-2006 03:59 AM
Making HTTP requests using Twisted rzimerman Python 4 07-11-2006 06:03 PM
web requests and mobile requests Fernando Arámburu ASP .Net 1 04-08-2005 07:13 PM
Making multiple HTTP requests using same HttpConnection object in j2me Ravi Ambros Wallau Java 0 10-25-2004 06:50 PM



Advertisments