def get_sync(self, url, data, type=None, content_type="application/x-www-form-urlencoded"): try: data = urlencode(data) except: # data is probably a string to be send directly. pass headers = {"Content-Type": content_type} if type and type.upper() not in ("POST", "GET"): from restlib import RestfulRequest #@UnresolvedImport req = RestfulRequest(url, data=data, method=type.upper()) else: req = urllib2.Request(url, data, headers=headers) opener = urllib2.build_opener(self) eventlet.greenthread.sleep() try: f = opener.open(req, data=data) if f.code is None or str(f.code)[0] == "2": dispatcher.send(UrlGetter.HTTP_RESULT, self, result=f.read(), source=url, code=f.code) else: e = urllib2.HTTPError( url, f.code, "A code %s HTTP error has occurred when trying to send to target %s" % (f.code, url), req.headers, f) dispatcher.send(UrlGetter.HTTP_ERROR, self, exception=e) # TODO: make sure we're supposed to listen to URLErrors except (urllib2.URLError, ValueError), e: dispatcher.send(UrlGetter.URL_ERROR, self, exception=e, url=url)
def test_scout_http_error(self, mock_urlopen): mock_urlopen.side_effect = urllib2.HTTPError( self.url, 404, "Internal error", None, None) url, content, status = self.scout_instance.scout( ("127.0.0.1", "8080")) self.assertEqual(url, self.url) self.assertTrue(isinstance(content, urllib2.HTTPError)) self.assertEqual(status, 404)