olemis

olemis

Feb 15th, 2010
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. class BasicUrlLibTransport(HessianTransport):
  2.     """ Transport handler that uses urllib2.
  3.    Basic authentication scheme is used. """
  4.    
  5.     def __init__(self, uri, credentials):
  6.         HessianTransport.__init__(self, uri, credentials)
  7.         # print "init:uri:", uri, "; cred:", self._credentials # debug
  8.                
  9.         if (self._credentials != None):            
  10.             # TODO Make tests for authorization
  11.            
  12.             pman = urllib2.HTTPPasswordMgrWithDefaultRealm()
  13.             auth_handler = urllib2.HTTPBasicAuthHandler(pman)            
  14.             auth_handler.add_password(None, # default realm
  15.                                       uri,
  16.                                       self._credentials["username"],
  17.                                       self._credentials["password"])
  18.            
  19.             # TODO Add digest authorization handler here?
  20.             # HTTPDigestAuthHandler
  21.            
  22.             self._opener = urllib2.build_opener(auth_handler)
  23.            
  24.             # Following code allows sending authorization information in advance,
  25.             # so single TCP request will suffice. Without it we'll rely on HTTP's
  26.             # authorization required response and authorization handlers.            
  27.             # self._opener.addheaders["Authorization"] = "Basic %s" % base64.encodestring(
  28.             #    "%s:%s" % (credentials["username"],
  29.             #               credentials["password"])).rstrip()
  30.         else:
  31.             self._opener = urllib2.build_opener()        
  32.    
  33.  
  34. @@    def request(self, outstream):
  35. @@        outstream.flush()
  36. @@        req_data = outstream.read()        
  37. @@        # print "request", map(lambda x : "%02x" % ord(x), req_data[:50]), "\n\t:", req_data[:50] # debug        
  38. @@        r = urllib2.Request(self._uri, req_data)
  39. @@        r.add_header("Content-Length", len(req_data))
  40. @@        r.add_header("User-agent", "HessianPy/%s" % __version__)
  41. @@        r.add_header("Content-type", "application/octet-stream")
  42. @@        
  43. @@        response = self._opener.open(r)
  44. @@        result = StringIO(response.read())
  45. @@        response.close()
  46. @@        return result        
  47.  
Add Comment
Please, Sign In to add comment