def measure(host, port, dtrace, attendeeCount, samples): user = password = "******" root = "/" principal = "/" calendar = "event-creation-benchmark" authinfo = HTTPDigestAuthHandler() authinfo.add_password( realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # First set things up yield initialize(agent, host, port, user, password, root, principal, calendar) method = 'PUT' uri = 'http://%s:%d/calendars/__uids__/%s/%s/foo-%%d.ics' % ( host, port, user, calendar) headers = Headers({"content-type": ["text/calendar"]}) # An infinite stream of VEVENTs to PUT to the server. events = ((i, makeEvent(i, attendeeCount)) for i in count(2)) # Sample it a bunch of times samples = yield sample( dtrace, samples, agent, ((method, uri % (i,), headers, StringProducer(body)) for (i, body) in events).next) returnValue(samples)
def measure(host, port, dtrace, events, samples): user = password = "******" root = "/" principal = "/" calendar = "vfreebusy-benchmark" authinfo = HTTPDigestAuthHandler() authinfo.add_password( realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # First set things up account = yield initialize( agent, host, port, user, password, root, principal, calendar) base = "/calendars/users/%s/%s/foo-%%d.ics" % (user, calendar) for i, cal in enumerate(makeEvents(events)): yield account.writeData(base % (i,), cal, "text/calendar") method = 'POST' uri = 'http://%s:%d/calendars/__uids__/%s/outbox/' % (host, port, user) headers = Headers({"content-type": ["text/calendar"]}) body = StringProducer(vfreebusy) samples = yield sample( dtrace, samples, agent, lambda: (method, uri, headers, body)) returnValue(samples)
def measure(calendar, organizerSequence, events, host, port, dtrace, samples): """ Benchmark event creation. """ user = password = "******" % (organizerSequence,) root = "/" principal = "/" authinfo = HTTPDigestAuthHandler() authinfo.add_password( realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # First set things up yield initialize(agent, host, port, user, password, root, principal, calendar) method = 'PUT' uri = 'http://%s:%d/calendars/__uids__/%s/%s/foo-%%d.ics' % ( host, port, user, calendar) headers = Headers({"content-type": ["text/calendar"]}) # Sample it a bunch of times samples = yield sample( dtrace, samples, agent, ((method, uri % (i,), headers, StringProducer(body)) for (i, body) in events).next, CREATED) returnValue(samples)
def measure(host, port, dtrace, attendeeCount, samples, fieldName, replacer, eventPerSample=False): user = password = "******" root = "/" principal = "/" calendar = "event-%s-benchmark" % (fieldName,) authinfo = HTTPDigestAuthHandler() authinfo.add_password( realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Set up the calendar first yield initialize(agent, host, port, user, password, root, principal, calendar) if eventPerSample: # Create an event for each sample that will be taken, so that no event # is used for two different samples. f = _selfish_sample else: # Just create one event and re-use it for all samples. f = _generous_sample data = yield f( dtrace, replacer, agent, host, port, user, calendar, fieldName, attendeeCount, samples) returnValue(data)
def measure(host, port, dtrace, attendeeCount, samples): organizerSequence = 1 user = password = "******" % (organizerSequence,) root = "/" principal = "/" # Two calendars between which to move the event. fooCalendar = "event-move-foo-benchmark" barCalendar = "event-move-bar-benchmark" authinfo = HTTPDigestAuthHandler() authinfo.add_password(realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Set up the calendars first for calendar in [fooCalendar, barCalendar]: yield initialize(agent, host, port, user, password, root, principal, calendar) fooURI = "http://%s:%d/calendars/__uids__/%s/%s/some-event.ics" % (host, port, user, fooCalendar) barURI = "http://%s:%d/calendars/__uids__/%s/%s/some-event.ics" % (host, port, user, barCalendar) # Create the event that will move around headers = Headers({"content-type": ["text/calendar"]}) yield agent.request("PUT", fooURI, headers, StringProducer(makeEvent(1, organizerSequence, attendeeCount))) # Move it around sooo much source = cycle([fooURI, barURI]) dest = cycle([barURI, fooURI]) params = (("MOVE", source.next(), Headers({"destination": [dest.next()], "overwrite": ["F"]})) for i in count(1)) samples = yield sample(dtrace, samples, agent, params.next, CREATED) returnValue(samples)
def measure(host, port, dtrace, attendeeCount, samples, fieldName, replacer, eventPerSample=False): user = password = "******" root = "/" principal = "/" calendar = "event-%s-benchmark" % (fieldName, ) authinfo = HTTPDigestAuthHandler() authinfo.add_password(realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Set up the calendar first yield initialize(agent, host, port, user, password, root, principal, calendar) if eventPerSample: # Create an event for each sample that will be taken, so that no event # is used for two different samples. f = _selfish_sample else: # Just create one event and re-use it for all samples. f = _generous_sample data = yield f(dtrace, replacer, agent, host, port, user, calendar, fieldName, attendeeCount, samples) returnValue(data)
def measure(host, port, dtrace, attendees, samples): userNumber = 1 user = password = "******" % (userNumber,) root = "/" principal = "/" calendar = "vfreebusy-vary-attendees-benchmark" targets = range(2, attendees + 2) authinfo = HTTPDigestAuthHandler() # Set up authentication info for our own user and all the other users that # may need an event created on one of their calendars. for i in [userNumber] + targets: targetUser = "******" % (i,) for path in ["calendars/users/%s/" % (targetUser,), "calendars/__uids__/10000000-0000-0000-0000-000000000%03d/" % (i,)]: authinfo.add_password( realm="Test Realm", uri="http://%s:%d/%s" % (host, port, path), user=targetUser, passwd=targetUser) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Set up events on about half of the target accounts baseTime = datetime.now().replace(minute=45, second=0, microsecond=0) for i in targets[::2]: targetUser = "******" % (i,) account = CalDAVAccount( agent, "%s:%d" % (host, port), user=targetUser, password=password, root=root, principal=principal) cal = "/calendars/users/%s/%s/" % (targetUser, calendar) yield account.deleteResource(cal) yield account.makeCalendar(cal) yield account.writeData(cal + "foo.ics", makeEventNear(baseTime, i), "text/calendar") # And now issue the actual VFREEBUSY request method = 'POST' uri = 'http://%s:%d/calendars/__uids__/10000000-0000-0000-0000-000000000001/outbox/' % (host, port) headers = Headers({ "content-type": ["text/calendar"], "originator": ["mailto:%[email protected]" % (user,)], "recipient": [", ".join(["urn:x-uid:10000000-0000-0000-0000-000000000%03d" % (i,) for i in [userNumber] + targets])]}) body = StringProducer(VFREEBUSY % { "attendees": "".join([ "ATTENDEE:urn:x-uid:10000000-0000-0000-0000-000000000%03d\n" % (i,) for i in [userNumber] + targets]), "start": formatDate(baseTime.replace(hour=0, minute=0)) + 'Z', "end": formatDate( baseTime.replace(hour=0, minute=0) + timedelta(days=1)) + 'Z'}) samples = yield sample( dtrace, samples, agent, lambda: (method, uri, headers, body), OK) returnValue(samples)
class Client(object): """ An Exscriptd client that communicates via HTTP. """ def __init__(self, address, user, password): """ Constructor. Any operations performed with an instance of a client are directed to the server with the given address, using the given login data. @type address: str @param address: The base url of the server. @type user: str @param user: The login name on the server. @type password: str @param password: The password of the user. """ self.address = 'http://' + address self.handler = HTTPDigestAuthHandler() self.opener = build_opener(self.handler) self.handler.add_password(realm = default_realm, uri = self.address, user = user, passwd = password) def place_order(self, order): """ Sends the given order to the server, and updates the status of the order accordingly. @type order: Order @param order: The order that is placed. """ if order.status != 'new': msg = 'order status is "%s", should be "new"' % order.status raise ValueError(msg) if not order.is_valid(): raise ValueError('incomplete or invalid order') order.status = 'accepted' url = self.address + '/order/' xml = order.toxml() data = urlencode({'xml': xml}) try: result = self.opener.open(url, data) except HTTPError, e: if hasattr(e, 'read'): raise Exception(str(e) + ' with ' + e.read()) else: raise Exception(str(e)) if result.getcode() != 200: raise Exception(result.read()) order.id = json.loads(result.read())
def measure(host, port, dtrace, events, samples): user = password = "******" uid = "10000000-0000-0000-0000-000000000001" root = "/" principal = "/" calendar = "vfreebusy-benchmark" authinfo = HTTPDigestAuthHandler() authinfo.add_password(realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # First set things up account = yield initialize(agent, host, port, user, password, root, principal, calendar) base = "/calendars/users/%s/%s/foo-%%d.ics" % (user, calendar) baseTime = datetime.now().replace(hour=12, minute=15, second=0, microsecond=0) for i, cal in enumerate(makeEvents(baseTime, events)): yield account.writeData(base % (i, ), cal, "text/calendar") method = 'POST' uri = 'http://%s:%d/calendars/__uids__/%s/outbox/' % (host, port, user) headers = Headers({ "content-type": ["text/calendar"], "originator": ["mailto:%[email protected]" % (user, )], "recipient": [ "urn:x-uid:%s, urn:x-uid:10000000-0000-0000-0000-000000000002" % (uid, ) ] }) vfb = VFREEBUSY % { "attendees": "".join([ "ATTENDEE:urn:x-uid:%s\n" % (uid, ), "ATTENDEE:urn:x-uid:10000000-0000-0000-0000-000000000002\n" ]), "start": formatDate(baseTime.replace(hour=0, minute=0)) + 'Z', "end": formatDate(baseTime.replace(hour=0, minute=0) + timedelta(days=1)) + 'Z' } body = StringProducer(vfb.replace("\n", "\r\n")) samples = yield sample(dtrace, samples, agent, lambda: (method, uri, headers, body), OK) returnValue(samples)
def _createUser(self, number): record = self._records[number] user = record.uid authBasic = HTTPBasicAuthHandler(password_mgr=HTTPPasswordMgrWithDefaultRealm()) authBasic.add_password( realm=None, uri=self.servers[record.podID]["uri"], user=user.encode('utf-8'), passwd=record.password.encode('utf-8')) authDigest = HTTPDigestAuthHandler(passwd=HTTPPasswordMgrWithDefaultRealm()) authDigest.add_password( realm=None, uri=self.servers[record.podID]["uri"], user=user.encode('utf-8'), passwd=record.password.encode('utf-8')) return record, user, {"basic": authBasic, "digest": authDigest, }
def _createUser(self, number): record = self._records[number] user = record.uid authBasic = HTTPBasicAuthHandler() authBasic.add_password( realm="Test Realm", uri=self.server, user=user.encode('utf-8'), passwd=record.password.encode('utf-8')) authDigest = HTTPDigestAuthHandler() authDigest.add_password( realm="Test Realm", uri=self.server, user=user.encode('utf-8'), passwd=record.password.encode('utf-8')) return user, {"basic": authBasic, "digest": authDigest, }
def check_password(self, username, password): self.log.debug("Trac.ini authentication_url = '%s'" % self.auth_url) # Nothing to do, if URL is absolute. if self.auth_url.startswith('http://') or \ self.auth_url.startswith('https://'): authUrl = self.auth_url # Handle server-relative URLs. elif self.auth_url.startswith('/'): # Prepend the Trac server component. pr = urlparse(self.env.abs_href()) href = Href(pr[0] + '://' + pr[1]) authUrl = href(self.auth_url) elif '/' in self.auth_url: # URLs with path like 'common/authFile' or 'site/authFile'. authUrl = self.env.abs_href.chrome(self.auth_url) else: # Bare file name option value like 'authFile'. authUrl = self.env.abs_href.chrome('common', self.auth_url) self.log.debug("Final auth_url = '%s'" % authUrl) acctmgr = HTTPPasswordMgrWithDefaultRealm() acctmgr.add_password(None, authUrl, username, password) try: build_opener(HTTPBasicAuthHandler(acctmgr), HTTPDigestAuthHandler(acctmgr)).open(authUrl) except IOError, e: if hasattr(e, 'code') and e.code == 404: self.log.debug("""HttpAuthStore page not found; we are authenticated nonetheless""") return True if hasattr(e, 'code') and e.code == 401: self.log.debug("HttpAuthStore authentication failed") return None
def measure(host, port, dtrace, events, samples): user = password = "******" uid = "10000000-0000-0000-0000-000000000001" root = "/" principal = "/" calendar = "vfreebusy-benchmark" authinfo = HTTPDigestAuthHandler() authinfo.add_password( realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # First set things up account = yield initialize( agent, host, port, user, password, root, principal, calendar) base = "/calendars/users/%s/%s/foo-%%d.ics" % (user, calendar) baseTime = datetime.now().replace(hour=12, minute=15, second=0, microsecond=0) for i, cal in enumerate(makeEvents(baseTime, events)): yield account.writeData(base % (i,), cal, "text/calendar") method = 'POST' uri = 'http://%s:%d/calendars/__uids__/%s/outbox/' % (host, port, user) headers = Headers({ "content-type": ["text/calendar"], "originator": ["mailto:%[email protected]" % (user,)], "recipient": ["urn:x-uid:%s, urn:x-uid:10000000-0000-0000-0000-000000000002" % (uid,)]}) vfb = VFREEBUSY % { "attendees": "".join([ "ATTENDEE:urn:x-uid:%s\n" % (uid,), "ATTENDEE:urn:x-uid:10000000-0000-0000-0000-000000000002\n"]), "start": formatDate(baseTime.replace(hour=0, minute=0)) + 'Z', "end": formatDate( baseTime.replace(hour=0, minute=0) + timedelta(days=1)) + 'Z'} body = StringProducer(vfb.replace("\n", "\r\n")) samples = yield sample( dtrace, samples, agent, lambda: (method, uri, headers, body), OK) returnValue(samples)
def measure(host, port, dtrace, attendeeCount, samples): organizerSequence = 1 user = password = "******" % (organizerSequence, ) root = "/" principal = "/" # Two calendars between which to move the event. fooCalendar = "event-move-foo-benchmark" barCalendar = "event-move-bar-benchmark" authinfo = HTTPDigestAuthHandler() authinfo.add_password(realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Set up the calendars first for calendar in [fooCalendar, barCalendar]: yield initialize(agent, host, port, user, password, root, principal, calendar) fooURI = 'http://%s:%d/calendars/__uids__/%s/%s/some-event.ics' % ( host, port, user, fooCalendar) barURI = 'http://%s:%d/calendars/__uids__/%s/%s/some-event.ics' % ( host, port, user, barCalendar) # Create the event that will move around headers = Headers({"content-type": ["text/calendar"]}) yield agent.request( 'PUT', fooURI, headers, StringProducer(makeEvent(1, organizerSequence, attendeeCount))) # Move it around sooo much source = cycle([fooURI, barURI]) dest = cycle([barURI, fooURI]) params = (('MOVE', source.next(), Headers({ "destination": [dest.next()], "overwrite": ["F"] })) for i in count(1)) samples = yield sample(dtrace, samples, agent, params.next, CREATED) returnValue(samples)
def check_password(self, user, password): mgr = HTTPPasswordMgrWithDefaultRealm() mgr.add_password(None, self.auth_url, user, password) try: build_opener(HTTPBasicAuthHandler(mgr), HTTPDigestAuthHandler(mgr)).open(self.auth_url) except IOError: return False else: return True
def measure(host, port, dtrace, numCalendars, samples): # There's already the "calendar" calendar numCalendars -= 1 user = password = "******" root = "/" principal = "/" authinfo = HTTPDigestAuthHandler() authinfo.add_password(realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Create the number of calendars necessary account = CalDAVAccount(agent, "%s:%d" % (host, port), user=user, password=password, root=root, principal=principal) cal = "/calendars/users/%s/propfind-%%d/" % (user, ) for i in range(numCalendars): yield account.makeCalendar(cal % (i, )) body = StringProducer(PROPFIND) params = (('PROPFIND', 'http://%s:%d/calendars/__uids__/%s/' % (host, port, user), Headers({ "depth": ["1"], "content-type": ["text/xml"] }), body) for i in count(1)) samples = yield sample(dtrace, samples, agent, params.next, MULTI_STATUS) # Delete the calendars we created to leave the server in roughly # the same state as we found it. for i in range(numCalendars): yield account.deleteResource(cal % (i, )) returnValue(samples)
def __init__(self, address, user, password): """ Constructor. Any operations performed with an instance of a client are directed to the server with the given address, using the given login data. @type address: str @param address: The base url of the server. @type user: str @param user: The login name on the server. @type password: str @param password: The password of the user. """ self.address = 'http://' + address self.handler = HTTPDigestAuthHandler() self.opener = build_opener(self.handler) self.handler.add_password(realm=default_realm, uri=self.address, user=user, passwd=password)
def measure(host, port, dtrace, numEvents, samples): user = password = "******" root = "/" principal = "/" uri = "http://%s:%d/" % (host, port) authinfo = HTTPDigestAuthHandler() authinfo.add_password(realm="Test Realm", uri=uri, user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Create the number of calendars necessary account = CalDAVAccount(agent, "%s:%d" % (host, port), user=user, password=password, root=root, principal=principal) cal = "calendars/users/%s/find-events/" % (user, ) yield account.makeCalendar("/" + cal) # Create the indicated number of events on the calendar yield uploadEvents(numEvents, agent, uri, cal) body = StringProducer(PROPFIND) params = (('PROPFIND', '%scalendars/__uids__/%s/find-events/' % (uri, user), Headers({ "depth": ["1"], "content-type": ["text/xml"] }), body) for i in count(1)) samples = yield sample(dtrace, samples, agent, params.next, MULTI_STATUS) # Delete the calendar we created to leave the server in roughly # the same state as we found it. yield account.deleteResource("/" + cal) returnValue(samples)
def measure(host, port, dtrace, attendeeCount, samples): organizerSequence = 1 user = password = "******" % (organizerSequence,) root = "/" principal = "/" calendar = "event-deletion-benchmark" authinfo = HTTPDigestAuthHandler() authinfo.add_password( realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Set up the calendar first yield initialize(agent, host, port, user, password, root, principal, calendar) # An infinite stream of VEVENTs to PUT to the server. events = ((i, makeEvent(i, organizerSequence, attendeeCount)) for i in count(2)) # Create enough events to delete uri = 'http://%s:%d/calendars/__uids__/%s/%s/foo-%%d.ics' % ( host, port, user, calendar) headers = Headers({"content-type": ["text/calendar"]}) urls = [] for i, body in events: urls.append(uri % (i,)) yield agent.request( 'PUT', urls[-1], headers, StringProducer(body)) if len(urls) == samples: break # Now delete them all samples = yield sample( dtrace, samples, agent, (('DELETE', url) for url in urls).next, NO_CONTENT) returnValue(samples)
def measure(host, port, dtrace, numEvents, samples): user = password = "******" root = "/" principal = "/" uri = "http://%s:%d/" % (host, port) authinfo = HTTPDigestAuthHandler() authinfo.add_password( realm="Test Realm", uri=uri, user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Create the number of calendars necessary account = CalDAVAccount( agent, "%s:%d" % (host, port), user=user, password=password, root=root, principal=principal) cal = "calendars/users/%s/find-events/" % (user,) yield account.makeCalendar("/" + cal) # Create the indicated number of events on the calendar yield uploadEvents(numEvents, agent, uri, cal) body = StringProducer(PROPFIND) params = ( ('PROPFIND', '%scalendars/__uids__/%s/find-events/' % (uri, user), Headers({"depth": ["1"], "content-type": ["text/xml"]}), body) for i in count(1)) samples = yield sample(dtrace, samples, agent, params.next, MULTI_STATUS) # Delete the calendar we created to leave the server in roughly # the same state as we found it. yield account.deleteResource("/" + cal) returnValue(samples)
def measure(host, port, dtrace, numCalendars, samples): # There's already the "calendar" calendar numCalendars -= 1 user = password = "******" root = "/" principal = "/" authinfo = HTTPDigestAuthHandler() authinfo.add_password( realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Create the number of calendars necessary account = CalDAVAccount( agent, "%s:%d" % (host, port), user=user, password=password, root=root, principal=principal) cal = "/calendars/users/%s/propfind-%%d/" % (user,) for i in range(numCalendars): yield account.makeCalendar(cal % (i,)) body = StringProducer(PROPFIND) params = ( ('PROPFIND', 'http://%s:%d/calendars/__uids__/%s/' % (host, port, user), Headers({"depth": ["1"], "content-type": ["text/xml"]}), body) for i in count(1)) samples = yield sample(dtrace, samples, agent, params.next, MULTI_STATUS) # Delete the calendars we created to leave the server in roughly # the same state as we found it. for i in range(numCalendars): yield account.deleteResource(cal % (i,)) returnValue(samples)
def SendLogin(username, password): """Authenticate with HTTP Digest Authentication, then receive user ID""" # Use HTTP digest authentication try: authen = HTTPDigestAuthHandler() authen.add_password( realm=consts.REALM, uri=consts.URL_REQ_LOGIN, user=username.encode('utf-8'), #passwd = md5(password.encode('utf-8')+username.encode('utf-8')\ #+consts.REALM).hexdigest()) passwd=md5(password.encode('utf-8')).hexdigest()) opener = build_opener(authen) opener.addheaders = [('User-Agent', consts.USER_AGENT)] install_opener(opener) # Take return value to be user ID, limit return value to 32 characters for protection answer = opener.open(consts.URL_REQ_LOGIN).read( 1048576) # Bound read to 128kB except IOError, e: return ParseWebError(e)
def measure(host, port, dtrace, attendeeCount, samples): user = password = "******" root = "/" principal = "/" calendar = "report-principal" authinfo = HTTPDigestAuthHandler() authinfo.add_password( realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Set up the calendar first yield initialize(agent, host, port, user, password, root, principal, calendar) url = 'http://%s:%d/principals/' % (host, port) headers = Headers({"content-type": ["text/xml"]}) samples = yield sample( dtrace, samples, agent, lambda: ('REPORT', url, headers, StringProducer(body))) returnValue(samples)
def measure(host, port, dtrace, attendeeCount, samples): user = password = "******" root = "/" principal = "/" calendar = "report-principal" authinfo = HTTPDigestAuthHandler() authinfo.add_password(realm="Test Realm", uri="http://%s:%d/" % (host, port), user=user, passwd=password) agent = AuthHandlerAgent(Agent(reactor), authinfo) # Set up the calendar first yield initialize(agent, host, port, user, password, root, principal, calendar) url = 'http://%s:%d/principals/' % (host, port) headers = Headers({"content-type": ["text/xml"]}) samples = yield sample( dtrace, samples, agent, lambda: ('REPORT', url, headers, StringProducer(body))) returnValue(samples)
def _build_opener(self, params, base_url): handlers = poster.streaminghttp.get_handlers() user = params.get('user', '') if user: realm = params.get('realm', '') password = params.get('password', '') scheme = params.get('scheme', 'digest') password_mgr = HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(realm, base_url, user, password) if scheme == 'basic': handler = HTTPBasicAuthHandler(password_mgr) elif scheme == 'digest': handler = HTTPDigestAuthHandler(password_mgr) else: raise ValueError('Unknown auth type "%s"' % scheme) handlers = handlers + [handler] return build_opener(*handlers)
def __init__(self, address, user, password): """ Constructor. Any operations performed with an instance of a client are directed to the server with the given address, using the given login data. @type address: str @param address: The base url of the server. @type user: str @param user: The login name on the server. @type password: str @param password: The password of the user. """ self.address = 'http://' + address self.handler = HTTPDigestAuthHandler() self.opener = build_opener(self.handler) self.handler.add_password(realm = default_realm, uri = self.address, user = user, passwd = password)
def _request(url, post=False, user=None, passwd=None, allow_TLSv1=True, **kwargs): url_values = urlencode(kwargs) if url_values: url += '?' + url_values logger.debug('Accessing URL %s' % url) url_args = {'timeout': g_timeout} try: url_args['context'] = ssl.SSLContext( ssl.PROTOCOL_TLSv1 if allow_TLSv1 else ssl.PROTOCOL_SSLv2) except AttributeError: try: url_args['context'] = ssl.create_default_context() except AttributeError: pass opener = None req = Request(url) if post: logger.debug('POST data: \n%s' % post) req.data = post req.add_header('Accept', '*/*') itry = 0 while True: itry += 1 try: urlopen_ = opener.open if opener else urlopen while True: try: resp = urlopen_(req, **url_args) break except TypeError: del url_args['context'] # context not avail before 3.4.3 if resp.getcode() == 204: raise EmptyResult(url) return resp except HTTPError as e: if e.code == 413: raise RequestEntityTooLarge(url) elif e.code == 401: headers = getattr(e, 'headers', e.hdrs) realm = get_realm_from_auth_header(headers) if itry == 1 and user is not None: auth_handler = HTTPDigestAuthHandler() auth_handler.add_password(realm=realm, uri=url, user=user, passwd=passwd or '') opener = build_opener(auth_handler) continue else: logger.error('authentication failed for realm "%s" when ' 'accessing url "%s"' % (realm, url)) raise e else: logger.error('error content returned by server:\n%s' % e.read()) raise e break
if 'digest' in challenges: key = 'digest' elif 'basic' in challenges: key = 'basic' else: key = None if key: self._challenged[self._authKey(method, uri)] = challenges[key] return self._respondToChallenge(challenges[key], method, uri, headers, bodyProducer) return response if __name__ == '__main__': from urllib2 import HTTPDigestAuthHandler handler = HTTPDigestAuthHandler() handler.add_password( realm="Test Realm", uri="http://localhost:8008/", user="******", passwd="user01") from twisted.web.client import Agent from twisted.internet import reactor from twisted.python.log import err agent = AuthHandlerAgent(Agent(reactor), handler) d = agent.request( 'DELETE', 'http://localhost:8008/calendars/users/user01/monkeys3/') def deleted(response): print(response.code) print(response.headers)
def _request(url, post=False, user=None, passwd=None, allow_TLSv1=False, **kwargs): timeout = float(kwargs.pop('timeout', g_timeout)) url_values = urlencode(kwargs) if url_values: url += '?' + url_values logger.debug('Accessing URL %s' % url) url_args = { 'timeout': timeout } if allow_TLSv1: url_args['context'] = ssl.SSLContext(ssl.PROTOCOL_TLSv1) opener = None req = Request(url) if post: if isinstance(post, str): post = post.encode('utf8') logger.debug('POST data: \n%s' % post.decode('utf8')) req.data = post req.add_header('Accept', '*/*') itry = 0 while True: itry += 1 try: urlopen_ = opener.open if opener else urlopen while True: try: resp = urlopen_(req, **url_args) break except TypeError: del url_args['context'] # context not avail before 3.4.3 logger.debug('Response: %s' % resp.getcode()) if resp.getcode() == 204: raise EmptyResult(url) return resp except HTTPError as e: if e.code == 413: raise RequestEntityTooLarge(url) elif e.code == 401: headers = getattr(e, 'headers', e.hdrs) realm = get_realm_from_auth_header(headers) if itry == 1 and user is not None: auth_handler = HTTPDigestAuthHandler() auth_handler.add_password( realm=realm, uri=url, user=user, passwd=passwd or '') opener = build_opener(auth_handler) continue else: logger.error( 'authentication failed for realm "%s" when ' 'accessing url "%s"' % (realm, url)) raise DownloadError('Original error was: %s' % str(e)) else: logger.error( 'error content returned by server:\n%s' % e.read()) raise DownloadError('Original error was: %s' % str(e)) break
def setup_auth(user, passwd): '''Sets up the login for SnowDS WebDAV''' auth_handler = HTTPDigestAuthHandler() auth_handler.add_password(realm=SNOW_REALM, uri=SNOW_DATA_URL, user=user, passwd=passwd) opener = build_opener(auth_handler) install_opener(opener)
if 'digest' in challenges: key = 'digest' elif 'basic' in challenges: key = 'basic' else: key = None if key: self._challenged[self._authKey(method, uri)] = challenges[key] return self._respondToChallenge(challenges[key], method, uri, headers, bodyProducer) return response if __name__ == '__main__': from urllib2 import HTTPDigestAuthHandler handler = HTTPDigestAuthHandler() handler.add_password(realm="Test Realm", uri="http://localhost:8008/", user="******", passwd="user01") from twisted.web.client import Agent from twisted.internet import reactor from twisted.python.log import err agent = AuthHandlerAgent(Agent(reactor), handler) d = agent.request( 'DELETE', 'http://localhost:8008/calendars/users/user01/monkeys3/') def deleted(response): print(response.code) print(response.headers)