From 213580ea7808b3d35e12dcaead03cb443b8a5e49 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Mon, 16 Jan 2017 15:45:29 -0800 Subject: [PATCH 1/3] cache the remote IP property --- sanic/request.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sanic/request.py b/sanic/request.py index 52ec469b..fbd41696 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -65,6 +65,7 @@ class Request(dict): self.parsed_files = None self.parsed_args = None self._cookies = None + self._ip = None @property def json(self): @@ -142,7 +143,11 @@ class Request(dict): @property def ip(self): - return self.transport.get_extra_info('peername') + if self._ip is None: + self._ip = self.transport.get_extra_info('peername') + return self._ip + else: + return self._ip File = namedtuple('File', ['type', 'body', 'name']) From 5c344f7efa7baaa951f1a4703fa7c739285a3e10 Mon Sep 17 00:00:00 2001 From: Eli Uriegas Date: Mon, 16 Jan 2017 17:51:56 -0600 Subject: [PATCH 2/3] Remove redundant else --- sanic/request.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index fbd41696..7f3b87eb 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -145,9 +145,7 @@ class Request(dict): def ip(self): if self._ip is None: self._ip = self.transport.get_extra_info('peername') - return self._ip - else: - return self._ip + return self._ip File = namedtuple('File', ['type', 'body', 'name']) From 9bc69f7de96f030c95bc4d1861bf25d2ec96e294 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Mon, 16 Jan 2017 17:21:57 -0800 Subject: [PATCH 3/3] use hasattr --- sanic/request.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index 7f3b87eb..26176687 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -65,7 +65,6 @@ class Request(dict): self.parsed_files = None self.parsed_args = None self._cookies = None - self._ip = None @property def json(self): @@ -143,7 +142,7 @@ class Request(dict): @property def ip(self): - if self._ip is None: + if not hasattr(self, '_ip'): self._ip = self.transport.get_extra_info('peername') return self._ip