def query(host, port, uri, params, timeout=20): try: with Timeout.push(timeout): cnn = HTTPConnection() cnn.connect((str(host), int(port))) try: request = cnn.post(str(uri), urlencode(params)) request.add_header("Content-type", "application/x-www-form-urlencoded") request.add_header("Connection", "close") response = cnn.perform(request) if response.status_code != 200: raise HTTPError("Error downloading http://%s:%s%s: %s" % (host, port, uri, response.status)) body = response.body if response.get_header("Content-type") == "application/json": body = json.loads(body) return body finally: cnn.close() except IOError as e: if e.errno == 111: raise HTTPConnectionRefused("Connection refused during downloading http://%s:%s%s" % (host, port, uri)) else: raise HTTPError("Error downloading http://%s:%s%s: %s" % (host, port, uri, e)) except TimeoutError: raise HTTPError("Timeout downloading http://%s:%s%s" % (host, port, uri))
def do_query_service(self, service_id, svc, url, timeout, verbose=False, *args, **kwargs): try: with Timeout.push(timeout): cnn = HTTPConnection() addr = (svc.get("addr").encode("utf-8"), svc.get("port")) try: cnn.connect(addr) except IOError as e: raise ClusterError("Error connecting to service %s(%s:%s): %s" % (service_id, addr[0], addr[1], e)) params = { "args": json.dumps(args), "kwargs": json.dumps(kwargs) } try: uri = utf2str("/service/call/%s%s" % (service_id, url)) request = cnn.post(uri, urlencode(params)) request.add_header("Content-type", "application/x-www-form-urlencoded") request.add_header("Connection", "close") if verbose: self.debug("Query http://%s:%s%s", addr[0], addr[1], uri) response = cnn.perform(request) if response.status_code != 200: raise ClusterError("Service %s (%s:%d) returned status %d for URL %s" % (service_id, addr[0], addr[1], response.status_code, uri)) res = json.loads(response.body) if res.get("error"): raise ClusterError(u"Service %s returned error: %s" % (service_id, res["error"])) return res.get("retval") finally: cnn.close() except TimeoutError: raise ClusterError("Timeout querying %s of service %s" % (url, service_id))
def query(host, port, uri, params, timeout=20): try: with Timeout.push(timeout): cnn = HTTPConnection() cnn.connect((str(host), int(port))) try: request = cnn.post(str(uri), urlencode(params)) request.add_header("Content-type", "application/x-www-form-urlencoded") request.add_header("Connection", "close") response = cnn.perform(request) if response.status_code != 200: raise HTTPError("Error downloading http://%s:%s%s: %s" % (host, port, uri, response.status)) body = response.body if response.get_header("Content-type") == "application/json": body = json.loads(body) return body finally: cnn.close() except IOError as e: if e.errno == 111: raise HTTPConnectionRefused( "Connection refused during downloading http://%s:%s%s" % (host, port, uri)) else: raise HTTPError("Error downloading http://%s:%s%s: %s" % (host, port, uri, e)) except TimeoutError: raise HTTPError("Timeout downloading http://%s:%s%s" % (host, port, uri))
def testHTTPPost(self): cnn = HTTPConnection() try: cnn.connect(('localhost', SERVER_PORT)) for i in [1, 2, 4, 8, 16, 32, 100, 1000, 10000, 100000, 200000]: post_data = 'test post data' * i request = cnn.post('/post', post_data, host = 'testhost.nl') response = cnn.perform(request) self.assertEquals('ok', response.body) self.assertTrue('HTTP_CONTENT_LENGTH' in self.saver.environ) self.assertEquals(len(post_data), int(self.saver.environ['HTTP_CONTENT_LENGTH'])) self.assertEquals(post_data, self.saver.body) self.assertEquals('testhost.nl', self.saver.environ['HTTP_HOST']) finally: cnn.close()
def do_query_service(self, service_id, svc, url, timeout, verbose=False, *args, **kwargs): try: with Timeout.push(timeout): cnn = HTTPConnection() addr = (svc.get("addr").encode("utf-8"), svc.get("port")) try: cnn.connect(addr) except IOError as e: raise ClusterError( "Error connecting to service %s(%s:%s): %s" % (service_id, addr[0], addr[1], e)) params = { "args": json.dumps(args), "kwargs": json.dumps(kwargs) } try: uri = utf2str("/service/call/%s%s" % (service_id, url)) request = cnn.post(uri, urlencode(params)) request.add_header("Content-type", "application/x-www-form-urlencoded") request.add_header("Connection", "close") if verbose: self.debug("Query http://%s:%s%s", addr[0], addr[1], uri) response = cnn.perform(request) if response.status_code != 200: raise ClusterError( "Service %s (%s:%d) returned status %d for URL %s" % (service_id, addr[0], addr[1], response.status_code, uri)) res = json.loads(response.body) if res.get("error"): raise ClusterError(u"Service %s returned error: %s" % (service_id, res["error"])) return res.get("retval") finally: cnn.close() except TimeoutError: raise ClusterError("Timeout querying %s of service %s" % (url, service_id))
def testHTTPPost(self): cnn = HTTPConnection() try: cnn.connect(('localhost', SERVER_PORT)) for i in [1, 2, 4, 8, 16, 32, 100, 1000, 10000, 100000, 200000]: post_data = 'test post data' * i request = cnn.post('/post', post_data, host='testhost.nl') response = cnn.perform(request) self.assertEquals('ok', response.body) self.assertTrue('HTTP_CONTENT_LENGTH' in self.saver.environ) self.assertEquals( len(post_data), int(self.saver.environ['HTTP_CONTENT_LENGTH'])) self.assertEquals(post_data, self.saver.body) self.assertEquals('testhost.nl', self.saver.environ['HTTP_HOST']) finally: cnn.close()
def comment_post(target, id, body): content = "body=%s" % (urlencode(body)) if len(content) > 1024 * 1024: store_error(target, "request_too_large") return cnn = HTTPConnection() try: cnn.connect((target["host"], 80)) try: start = time.time() req = cnn.post("/posts/%s/comments" % id, content) req.add_header("Content-type", "application/x-www-form-urlencoded") req.add_header("Content-length", len(content)) res = cnn.perform(req) if res.status_code != 302: store_error(target, "create_comment: %d" % res.status_code) print "comment len=%d, time=%f" % (len(content), time.time() - start) finally: cnn.close() except IOError as e: store_error(target, e)