Python Forum
PUT with Basic Auth and urllib2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PUT with Basic Auth and urllib2
#1
Hello Pythonforum:

Unfortunately I am in a situation where I cannot install the requests library, and to top it off I am stuck using Python 2.6 on Redhat.

For this particular piece of code that I am writing, there are many places where an HTTP request must be made, some with Basic Auth and some without.

On this Redhat server, the only available options are urllib and urllib 2. I considered just using the subprocess module and a very long curl command, but I would rather get it with urllib2 if possible.

So I basically wrote my own wrapper class for urllib2 to make my life easier. All my methods are working with the exception of one... PUT with Basic Authentication.

Since urllib2 has no defined PUT method, I was able to piece together a working PUT method with various online sources/forums. This is what is working (I think it's actually overriding the POST method with the lambda function... not completely sure):

def put(self, url, type, data):
        content_type = self.__post_type(type)
        opener = urllib2.build_opener(urllib2.HTTPHandler)
        req = urllib2.Request(url, data=json.dumps(data))
        req.add_header('Content-Type', content_type)
        req.get_method = lambda: 'PUT'
        request = opener.open(req)
        self.status_code = request.getcode()
        return request.read()
That one works... and here is one of the other working methods... This one uses Basic Auth and has the same form as all the other methods (GET, GET with Basic Auth, and POST).

    def post_auth(self, url, u, p, type, data):
        content_type = self.__post_type(type)
        content = {'Content-Type': content_type}
        auth_header = encodestring("%s:%s" % (u, p))[:-1]
        req = urllib2.Request(url, data, content)
        req.add_header("Authorization", "Basic %s" % auth_header)
        request = urllib2.urlopen(req)
        self.status_code = request.getcode()
        return request.read()
Now here's the PUT method with Basic Auth... This is the one that isn't working:

def put_auth(self, url, u, p, type, data):
        content_type = self.__post_type(type)
        opener = urllib2.build_opener(urllib2.HTTPHandler)
        req = urllib2.Request(url, data=json.dumps(data))
        auth_header = encodestring("%s:%s" % (u, p))[:-1]
        req.add_header('Content-Type', content_type)
        req.add_header('Authorization', 'Basic ' + auth_header)
        req.get_method = lambda: 'PUT'
        request = opener.open(req)
        self.status_code = request.getcode()
        return request.read()
btw encodestring() is the old b64 method but it works on this server/Python version.

Any ideas on how to PUT with Basic Auth and urllib2? I would also accept urllib as well... but not urllib3 or requests module unfortunately.


EDIT* Nevermind that method is working... I was accidentally confusing a couple variables and didn't notice until entering it into this post hahaha
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  auth - URL SUN 2 1,478 May-20-2022, 07:50 AM
Last Post: SUN
  IBM Watson: Handshake status 403 Forbidden or No section: 'auth' groschat 1 2,756 May-07-2021, 03:44 PM
Last Post: jefsummers
  ImportError: No module named pydrive.auth lknights1987 5 5,841 Jun-23-2020, 06:40 PM
Last Post: snippsat
  Passing basic auth data between views nikos 0 1,862 Feb-13-2019, 09:32 PM
Last Post: nikos
  google-auth requirements.txt strange behavior randalpinto 3 3,669 Dec-21-2018, 02:03 AM
Last Post: Larz60+
  "Raise SMTPException("SMTP AUTH extension not supported by server.") NajeebUllah 3 7,919 Mar-16-2018, 09:45 PM
Last Post: nilamo
  social-auth-app-django wheel and dependables "six" metalray 2 2,886 Nov-27-2017, 08:16 AM
Last Post: metalray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020