Feb-22-2019, 11:25 PM
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):
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
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