urllib2 - basic authentication and the put and delete methods

Discussion in 'Python' started by news.corp.adobe.com, Jul 8, 2005.

  1. I need to perform basic authentication and I also need to be able to use the
    put and delete methods to copy files / delete files on a server.

    Here is how I successfully perform basic authentication to read a page:
    ------------------------------------------------
    import urllib2, os, base64

    os.system("cls")

    theurl = 'http://myurl.com:9090/ESG/en_US/GLtest/'
    username = 'myusername'
    password = 'mypassword'

    req = urllib2.Request(theurl)

    realm = 'Apache Tomcat/4.1.27'

    base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
    authheader = "Basic %s" % base64string
    req.add_header("Authorization", authheader)

    handle = urllib2.urlopen(req)

    thepage = handle.read()

    print thepage
    ------------------------------------------------

    But despite much searching, I have yet to discover how to then use PUT and
    DELETE to copy files / delete files on the server.
     
    news.corp.adobe.com, Jul 8, 2005
    #1
    1. Advertising

  2. news.corp.adobe.com

    deelan Guest

    news.corp.adobe.com wrote:
    (...)
    >
    > But despite much searching, I have yet to discover how to then use PUT and
    > DELETE to copy files / delete files on the server.


    i believe you have to use httplib for that:
    <http://docs.python.org/lib/module-httplib.html>

    examples here:
    <http://docs.python.org/lib/httplib-examples.html>

    basically you pass auth headers whie
    using PUT/DELETE verbs:

    ## untested! ##

    headers = {
    "Authorization": authheader # your base 64 str
    }
    conn.request("PUT", "/your/url/here", body, headers)

    HTH,
    deelan.

    --
    deelan, #1 fan of adriana lima!
    <http://www.deelan.com/>
     
    deelan, Jul 8, 2005
    #2
    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. Josef Cihal
    Replies:
    0
    Views:
    761
    Josef Cihal
    Sep 5, 2005
  2. Alejandro Dubrovsky

    urllib2 through basic auth'ed proxy

    Alejandro Dubrovsky, Mar 29, 2006, in forum: Python
    Replies:
    6
    Views:
    2,393
    Alejandro Dubrovsky
    Apr 3, 2006
  3. Phillip B Oldham

    Making HEAD/PUT/DELETE requests with urllib2?

    Phillip B Oldham, Jun 14, 2008, in forum: Python
    Replies:
    3
    Views:
    555
    Diez B. Roggisch
    Jun 14, 2008
  4. Roy Smith
    Replies:
    5
    Views:
    302
    Dave Angel
    Dec 30, 2011
  5. Kenneth McDonald
    Replies:
    5
    Views:
    323
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page