Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BasicUrlLibTransport(HessianTransport):
- """ Transport handler that uses urllib2.
- Basic authentication scheme is used. """
- def __init__(self, uri, credentials):
- HessianTransport.__init__(self, uri, credentials)
- # print "init:uri:", uri, "; cred:", self._credentials # debug
- if (self._credentials != None):
- # TODO Make tests for authorization
- pman = urllib2.HTTPPasswordMgrWithDefaultRealm()
- auth_handler = urllib2.HTTPBasicAuthHandler(pman)
- auth_handler.add_password(None, # default realm
- uri,
- self._credentials["username"],
- self._credentials["password"])
- # TODO Add digest authorization handler here?
- # HTTPDigestAuthHandler
- self._opener = urllib2.build_opener(auth_handler)
- # Following code allows sending authorization information in advance,
- # so single TCP request will suffice. Without it we'll rely on HTTP's
- # authorization required response and authorization handlers.
- # self._opener.addheaders["Authorization"] = "Basic %s" % base64.encodestring(
- # "%s:%s" % (credentials["username"],
- # credentials["password"])).rstrip()
- else:
- self._opener = urllib2.build_opener()
- @@ def request(self, outstream):
- @@ outstream.flush()
- @@ req_data = outstream.read()
- @@ # print "request", map(lambda x : "%02x" % ord(x), req_data[:50]), "\n\t:", req_data[:50] # debug
- @@ r = urllib2.Request(self._uri, req_data)
- @@ r.add_header("Content-Length", len(req_data))
- @@ r.add_header("User-agent", "HessianPy/%s" % __version__)
- @@ r.add_header("Content-type", "application/octet-stream")
- @@
- @@ response = self._opener.open(r)
- @@ result = StringIO(response.read())
- @@ response.close()
- @@ return result
Add Comment
Please, Sign In to add comment