From c3b31a6fb0e9a1220bc1897e782080773802f2f3 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Mon, 8 Oct 2018 19:29:52 +0200 Subject: [PATCH] Simplify request ip and port retrieval logic This change also ensures that cases where transport stream is already closed is handled gracefully. --- sanic/request.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index c8b470d4..70240207 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -1,6 +1,5 @@ import sys import json -import socket from cgi import parse_header from collections import namedtuple from http.cookies import SimpleCookie @@ -192,18 +191,10 @@ class Request(dict): return self._socket def _get_address(self): - sock = self.transport.get_extra_info('socket') - - if sock.family == socket.AF_INET: - self._socket = (self.transport.get_extra_info('peername') or - (None, None)) - self._ip, self._port = self._socket - elif sock.family == socket.AF_INET6: - self._socket = (self.transport.get_extra_info('peername') or - (None, None, None, None)) - self._ip, self._port, *_ = self._socket - else: - self._ip, self._port = (None, None) + self._socket = self.transport.get_extra_info('peername') or \ + (None, None) + self._ip = self._socket[0] + self._port = self._socket[1] @property def remote_addr(self):