commit
01042c1d98
|
@ -75,6 +75,10 @@ The following variables are accessible as properties on `Request` objects:
|
||||||
|
|
||||||
- `ip` (str) - IP address of the requester.
|
- `ip` (str) - IP address of the requester.
|
||||||
|
|
||||||
|
- `port` (str) - Port address of the requester.
|
||||||
|
|
||||||
|
- `socket` (tuple) - (IP, port) of the requester.
|
||||||
|
|
||||||
- `app` - a reference to the Sanic application object that is handling this request. This is useful when inside blueprints or other handlers in modules that do not have access to the global `app` object.
|
- `app` - a reference to the Sanic application object that is handling this request. This is useful when inside blueprints or other handlers in modules that do not have access to the global `app` object.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
|
@ -574,7 +574,7 @@ class Sanic:
|
||||||
try:
|
try:
|
||||||
response = await self._run_response_middleware(request,
|
response = await self._run_response_middleware(request,
|
||||||
response)
|
response)
|
||||||
except:
|
except BaseException:
|
||||||
error_logger.exception(
|
error_logger.exception(
|
||||||
'Exception occured in one of response middleware handlers'
|
'Exception occured in one of response middleware handlers'
|
||||||
)
|
)
|
||||||
|
@ -642,7 +642,7 @@ class Sanic:
|
||||||
serve(**server_settings)
|
serve(**server_settings)
|
||||||
else:
|
else:
|
||||||
serve_multiple(server_settings, workers)
|
serve_multiple(server_settings, workers)
|
||||||
except:
|
except BaseException:
|
||||||
error_logger.exception(
|
error_logger.exception(
|
||||||
'Experienced exception while trying to serve')
|
'Experienced exception while trying to serve')
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -46,7 +46,8 @@ class Request(dict):
|
||||||
__slots__ = (
|
__slots__ = (
|
||||||
'app', 'headers', 'version', 'method', '_cookies', 'transport',
|
'app', 'headers', 'version', 'method', '_cookies', 'transport',
|
||||||
'body', 'parsed_json', 'parsed_args', 'parsed_form', 'parsed_files',
|
'body', 'parsed_json', 'parsed_args', 'parsed_form', 'parsed_files',
|
||||||
'_ip', '_parsed_url', 'uri_template', 'stream', '_remote_addr'
|
'_ip', '_parsed_url', 'uri_template', 'stream', '_remote_addr',
|
||||||
|
'_socket', '_port'
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, url_bytes, headers, version, method, transport):
|
def __init__(self, url_bytes, headers, version, method, transport):
|
||||||
|
@ -167,11 +168,27 @@ class Request(dict):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ip(self):
|
def ip(self):
|
||||||
if not hasattr(self, '_ip'):
|
if not hasattr(self, '_socket'):
|
||||||
self._ip = (self.transport.get_extra_info('peername') or
|
self._get_address()
|
||||||
(None, None))
|
|
||||||
return self._ip
|
return self._ip
|
||||||
|
|
||||||
|
@property
|
||||||
|
def port(self):
|
||||||
|
if not hasattr(self, '_socket'):
|
||||||
|
self._get_address()
|
||||||
|
return self._port
|
||||||
|
|
||||||
|
@property
|
||||||
|
def socket(self):
|
||||||
|
if not hasattr(self, '_socket'):
|
||||||
|
self._get_socket()
|
||||||
|
return self._socket
|
||||||
|
|
||||||
|
def _get_address(self):
|
||||||
|
self._socket = (self.transport.get_extra_info('peername') or
|
||||||
|
(None, None))
|
||||||
|
self._ip, self._port = self._socket
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def remote_addr(self):
|
def remote_addr(self):
|
||||||
"""Attempt to return the original client ip based on X-Forwarded-For.
|
"""Attempt to return the original client ip based on X-Forwarded-For.
|
||||||
|
|
|
@ -3,7 +3,7 @@ from os import path
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from ujson import dumps as json_dumps
|
from ujson import dumps as json_dumps
|
||||||
except:
|
except BaseException:
|
||||||
from json import dumps as json_dumps
|
from json import dumps as json_dumps
|
||||||
|
|
||||||
from aiofiles import open as open_async
|
from aiofiles import open as open_async
|
||||||
|
|
|
@ -588,7 +588,7 @@ def serve(host, port, request_handler, error_handler, before_start=None,
|
||||||
|
|
||||||
try:
|
try:
|
||||||
http_server = loop.run_until_complete(server_coroutine)
|
http_server = loop.run_until_complete(server_coroutine)
|
||||||
except:
|
except BaseException:
|
||||||
logger.exception("Unable to start server")
|
logger.exception("Unable to start server")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -76,14 +76,14 @@ class SanicTestClient:
|
||||||
try:
|
try:
|
||||||
request, response = results
|
request, response = results
|
||||||
return request, response
|
return request, response
|
||||||
except:
|
except BaseException:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Request and response object expected, got ({})".format(
|
"Request and response object expected, got ({})".format(
|
||||||
results))
|
results))
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
return results[-1]
|
return results[-1]
|
||||||
except:
|
except BaseException:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Request object expected, got ({})".format(results))
|
"Request object expected, got ({})".format(results))
|
||||||
|
|
||||||
|
|
|
@ -74,13 +74,13 @@ class GunicornWorker(base.Worker):
|
||||||
trigger_events(self._server_settings.get('before_stop', []),
|
trigger_events(self._server_settings.get('before_stop', []),
|
||||||
self.loop)
|
self.loop)
|
||||||
self.loop.run_until_complete(self.close())
|
self.loop.run_until_complete(self.close())
|
||||||
except:
|
except BaseException:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
trigger_events(self._server_settings.get('after_stop', []),
|
trigger_events(self._server_settings.get('after_stop', []),
|
||||||
self.loop)
|
self.loop)
|
||||||
except:
|
except BaseException:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
finally:
|
finally:
|
||||||
self.loop.close()
|
self.loop.close()
|
||||||
|
|
|
@ -27,6 +27,16 @@ def test_sync():
|
||||||
|
|
||||||
assert response.text == 'Hello'
|
assert response.text == 'Hello'
|
||||||
|
|
||||||
|
def test_remote_address():
|
||||||
|
app = Sanic('test_text')
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def handler(request):
|
||||||
|
return text("{}".format(request.ip))
|
||||||
|
|
||||||
|
request, response = app.test_client.get('/')
|
||||||
|
|
||||||
|
assert response.text == '127.0.0.1'
|
||||||
|
|
||||||
def test_text():
|
def test_text():
|
||||||
app = Sanic('test_text')
|
app = Sanic('test_text')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user