Simplify request ip and port retrieval logic
This change also ensures that cases where transport stream is already closed is handled gracefully.
This commit is contained in:
parent
5cc12fd945
commit
c3b31a6fb0
|
@ -1,6 +1,5 @@
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import socket
|
|
||||||
from cgi import parse_header
|
from cgi import parse_header
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from http.cookies import SimpleCookie
|
from http.cookies import SimpleCookie
|
||||||
|
@ -192,18 +191,10 @@ class Request(dict):
|
||||||
return self._socket
|
return self._socket
|
||||||
|
|
||||||
def _get_address(self):
|
def _get_address(self):
|
||||||
sock = self.transport.get_extra_info('socket')
|
self._socket = self.transport.get_extra_info('peername') or \
|
||||||
|
(None, None)
|
||||||
if sock.family == socket.AF_INET:
|
self._ip = self._socket[0]
|
||||||
self._socket = (self.transport.get_extra_info('peername') or
|
self._port = self._socket[1]
|
||||||
(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)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def remote_addr(self):
|
def remote_addr(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user