Making HEAD/PUT/DELETE requests with urllib2?

Discussion in 'Python' started by Phillip B Oldham, Jun 14, 2008.

  1. 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.
    Phillip B Oldham, Jun 14, 2008
    #1
    1. Advertising

  2. Phillip B Oldham

    Jeff McNeil Guest

    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
    <> 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
    >
    Jeff McNeil, Jun 14, 2008
    #2
    1. Advertising

  3. 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!
    Phillip B Oldham, Jun 14, 2008
    #3
  4. 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
    Diez B. Roggisch, Jun 14, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian W
    Replies:
    10
    Views:
    763
    Brian W
    Jul 2, 2003
  2. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    722
    Sherif ElMetainy
    Jul 10, 2004
  3. Karl
    Replies:
    0
    Views:
    393
  4. news.corp.adobe.com
    Replies:
    1
    Views:
    1,497
    deelan
    Jul 8, 2005
  5. Jiho Han

    Write into <HEAD></HEAD> section?

    Jiho Han, Jan 14, 2004, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    229
    Sam Fields
    Jan 16, 2004
Loading...

Share This Page