diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index 0e705075..7b9ed76f 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -154,7 +154,7 @@ class Arbiter(object): self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds) - listeners_str = ",".join([str(l) for l in self.LISTENERS]) + listeners_str = ",".join([str(lnr) for lnr in self.LISTENERS]) self.log.debug("Arbiter booted") self.log.info("Listening at: %s (%s)", listeners_str, self.pid) self.log.info("Using worker: %s", self.cfg.worker_class_str) @@ -421,7 +421,7 @@ class Arbiter(object): environ['LISTEN_FDS'] = str(len(self.LISTENERS)) else: environ['GUNICORN_FD'] = ','.join( - str(l.fileno()) for l in self.LISTENERS) + str(lnr.fileno()) for lnr in self.LISTENERS) os.chdir(self.START_CTX['cwd']) @@ -454,11 +454,11 @@ class Arbiter(object): # do we need to change listener ? if old_address != self.cfg.address: # close all listeners - for l in self.LISTENERS: - l.close() + for lnr in self.LISTENERS: + lnr.close() # init new listeners self.LISTENERS = sock.create_sockets(self.cfg, self.log) - listeners_str = ",".join([str(l) for l in self.LISTENERS]) + listeners_str = ",".join([str(lnr) for lnr in self.LISTENERS]) self.log.info("Listening at: %s", listeners_str) # do some actions on reload diff --git a/gunicorn/config.py b/gunicorn/config.py index bc24b700..2d7d8be6 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -448,7 +448,7 @@ def validate_callable(arity): raise TypeError(str(e)) except AttributeError: raise TypeError("Can not load '%s' from '%s'" - "" % (obj_name, mod_name)) + "" % (obj_name, mod_name)) if not callable(val): raise TypeError("Value is not callable: %s" % val) if arity != -1 and arity != util.get_arity(val): @@ -563,6 +563,7 @@ class ConfigFile(Setting): prefix. """ + class WSGIApp(Setting): name = "wsgi_app" section = "Config File" @@ -575,6 +576,7 @@ class WSGIApp(Setting): .. versionadded:: 20.1.0 """ + class Bind(Setting): name = "bind" action = "append" @@ -1273,69 +1275,70 @@ class ForwardedAllowIPS(Setting): By default, the value of the ``FORWARDED_ALLOW_IPS`` environment variable. If it is not defined, the default is ``"127.0.0.1"``. - + .. note:: - + The interplay between the request headers, the value of ``forwarded_allow_ips``, and the value of - ``secure_scheme_headers`` is complex. Various scenarios are documented below to further elaborate. In each case, we - have a request from the remote address 134.213.44.18, and the default value of ``secure_scheme_headers``: - + ``secure_scheme_headers`` is complex. Various scenarios are documented below to further elaborate. + In each case, we have a request from the remote address 134.213.44.18, and the default value of + ``secure_scheme_headers``: + .. code:: - + secure_scheme_headers = { 'X-FORWARDED-PROTOCOL': 'ssl', 'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-SSL': 'on' } - - - .. list-table:: + + + .. list-table:: :header-rows: 1 :align: center :widths: auto - + * - ``forwarded-allow-ips`` - Secure Request Headers - Result - Explanation - * - .. code:: - + * - .. code:: + ["127.0.0.1"] - .. code:: - + X-Forwarded-Proto: https - - .. code:: - + - .. code:: + wsgi.url_scheme = "http" - IP address was not allowed - * - .. code:: - + * - .. code:: + "*" - - - .. code:: - + - .. code:: + wsgi.url_scheme = "http" - IP address allowed, but no secure headers provided - * - .. code:: - + * - .. code:: + "*" - .. code:: - + X-Forwarded-Proto: https - - .. code:: - + - .. code:: + wsgi.url_scheme = "https" - IP address allowed, one request header matched - * - .. code:: - + * - .. code:: + ["134.213.44.18"] - .. code:: - + X-Forwarded-Ssl: on X-Forwarded-Proto: http - ``InvalidSchemeHeaders()`` raised - IP address allowed, but the two secure headers disagreed on if HTTPS was used - + """ @@ -1617,6 +1620,7 @@ class StatsdHost(Setting): .. versionadded:: 19.1 """ + # Datadog Statsd (dogstatsd) tags. https://docs.datadoghq.com/developers/dogstatsd/ class DogstatsdTags(Setting): name = "dogstatsd_tags" @@ -1632,6 +1636,7 @@ class DogstatsdTags(Setting): .. versionadded:: 20 """ + class StatsdPrefix(Setting): name = "statsd_prefix" section = "Logging" diff --git a/gunicorn/debug.py b/gunicorn/debug.py index 996fe1b4..a492df9e 100644 --- a/gunicorn/debug.py +++ b/gunicorn/debug.py @@ -28,7 +28,7 @@ class Spew(object): if '__file__' in frame.f_globals: filename = frame.f_globals['__file__'] if (filename.endswith('.pyc') or - filename.endswith('.pyo')): + filename.endswith('.pyo')): filename = filename[:-1] name = frame.f_globals['__name__'] line = linecache.getline(filename, lineno) diff --git a/gunicorn/glogging.py b/gunicorn/glogging.py index fec25a10..70d99270 100644 --- a/gunicorn/glogging.py +++ b/gunicorn/glogging.py @@ -44,46 +44,45 @@ SYSLOG_FACILITIES = { "local7": 23 } - CONFIG_DEFAULTS = dict( - version=1, - disable_existing_loggers=False, + version=1, + disable_existing_loggers=False, - root={"level": "INFO", "handlers": ["console"]}, - loggers={ - "gunicorn.error": { - "level": "INFO", - "handlers": ["error_console"], - "propagate": True, - "qualname": "gunicorn.error" - }, + root={"level": "INFO", "handlers": ["console"]}, + loggers={ + "gunicorn.error": { + "level": "INFO", + "handlers": ["error_console"], + "propagate": True, + "qualname": "gunicorn.error" + }, - "gunicorn.access": { - "level": "INFO", - "handlers": ["console"], - "propagate": True, - "qualname": "gunicorn.access" - } - }, - handlers={ - "console": { - "class": "logging.StreamHandler", - "formatter": "generic", - "stream": "ext://sys.stdout" - }, - "error_console": { - "class": "logging.StreamHandler", - "formatter": "generic", - "stream": "ext://sys.stderr" - }, - }, - formatters={ - "generic": { - "format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s", - "datefmt": "[%Y-%m-%d %H:%M:%S %z]", - "class": "logging.Formatter" - } + "gunicorn.access": { + "level": "INFO", + "handlers": ["console"], + "propagate": True, + "qualname": "gunicorn.access" } + }, + handlers={ + "console": { + "class": "logging.StreamHandler", + "formatter": "generic", + "stream": "ext://sys.stdout" + }, + "error_console": { + "class": "logging.StreamHandler", + "formatter": "generic", + "stream": "ext://sys.stderr" + }, + }, + formatters={ + "generic": { + "format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s", + "datefmt": "[%Y-%m-%d %H:%M:%S %z]", + "class": "logging.Formatter" + } + } ) @@ -299,7 +298,7 @@ class Logger(object): 'a': environ.get('HTTP_USER_AGENT', '-'), 'T': request_time.seconds, 'D': (request_time.seconds * 1000000) + request_time.microseconds, - 'M': (request_time.seconds * 1000) + int(request_time.microseconds/1000), + 'M': (request_time.seconds * 1000) + int(request_time.microseconds / 1000), 'L': "%d.%06d" % (request_time.seconds, request_time.microseconds), 'p': "<%s>" % os.getpid() } @@ -437,7 +436,7 @@ class Logger(object): # finally setup the syslog handler h = logging.handlers.SysLogHandler(address=addr, - facility=facility, socktype=socktype) + facility=facility, socktype=socktype) h.setFormatter(fmt) h._gunicorn = True diff --git a/gunicorn/http/message.py b/gunicorn/http/message.py index 0041cadc..b600ceab 100644 --- a/gunicorn/http/message.py +++ b/gunicorn/http/message.py @@ -41,7 +41,7 @@ class Message(object): # set headers limits self.limit_request_fields = cfg.limit_request_fields if (self.limit_request_fields <= 0 - or self.limit_request_fields > MAX_HEADERS): + or self.limit_request_fields > MAX_HEADERS): self.limit_request_fields = MAX_HEADERS self.limit_request_field_size = cfg.limit_request_field_size if self.limit_request_field_size < 0: @@ -71,7 +71,7 @@ class Message(object): secure_scheme_headers = {} if ('*' in cfg.forwarded_allow_ips or not isinstance(self.peer_addr, tuple) - or self.peer_addr[0] in cfg.forwarded_allow_ips): + or self.peer_addr[0] in cfg.forwarded_allow_ips): secure_scheme_headers = cfg.secure_scheme_headers # Parse headers into key/value pairs paying attention @@ -173,7 +173,7 @@ class Request(Message): # get max request line size self.limit_request_line = cfg.limit_request_line if (self.limit_request_line < 0 - or self.limit_request_line >= MAX_REQUEST_LINE): + or self.limit_request_line >= MAX_REQUEST_LINE): self.limit_request_line = MAX_REQUEST_LINE self.req_number = req_number @@ -276,7 +276,7 @@ class Request(Message): # check in allow list if ("*" not in self.cfg.proxy_allow_ips and isinstance(self.peer_addr, tuple) and - self.peer_addr[0] not in self.cfg.proxy_allow_ips): + self.peer_addr[0] not in self.cfg.proxy_allow_ips): raise ForbiddenProxyRequest(self.peer_addr[0]) def parse_proxy_protocol(self, line): diff --git a/gunicorn/sock.py b/gunicorn/sock.py index d4586770..da5705d1 100644 --- a/gunicorn/sock.py +++ b/gunicorn/sock.py @@ -39,7 +39,7 @@ class BaseSocket(object): def set_options(self, sock, bound=False): sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if (self.conf.reuse_port - and hasattr(socket, 'SO_REUSEPORT')): # pragma: no cover + and hasattr(socket, 'SO_REUSEPORT')): # pragma: no cover try: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) except socket.error as err: diff --git a/gunicorn/systemd.py b/gunicorn/systemd.py index cea48220..5bc1a744 100644 --- a/gunicorn/systemd.py +++ b/gunicorn/systemd.py @@ -58,7 +58,6 @@ def sd_notify(state, logger, unset_environment=False): child processes. """ - addr = os.environ.get('NOTIFY_SOCKET') if addr is None: # not run in a service, just a noop @@ -69,7 +68,7 @@ def sd_notify(state, logger, unset_environment=False): addr = '\0' + addr[1:] sock.connect(addr) sock.sendall(state.encode('utf-8')) - except: + except Exception: logger.debug("Exception while invoking sd_notify()", exc_info=True) finally: if unset_environment: diff --git a/gunicorn/util.py b/gunicorn/util.py index c2681154..b7cdd4e2 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -97,7 +97,7 @@ def load_class(uri, default="gunicorn.workers.sync.SyncWorker", try: mod = importlib.import_module('.'.join(components)) - except: + except Exception: exc = traceback.format_exc() msg = "class uri %r invalid or not found: \n\n[%s]" raise RuntimeError(msg % (uri, exc)) diff --git a/gunicorn/workers/geventlet.py b/gunicorn/workers/geventlet.py index ea82f3d6..4daf6d1c 100644 --- a/gunicorn/workers/geventlet.py +++ b/gunicorn/workers/geventlet.py @@ -66,7 +66,6 @@ def _eventlet_socket_sendfile(self, file, offset=0, count=None): file.seek(offset + total_sent) - def _eventlet_serve(sock, handle, concurrency): """ Serve requests forever. diff --git a/gunicorn/workers/ggevent.py b/gunicorn/workers/ggevent.py index 3941814f..0a844db3 100644 --- a/gunicorn/workers/ggevent.py +++ b/gunicorn/workers/ggevent.py @@ -41,7 +41,7 @@ class GeventWorker(AsyncWorker): sockets = [] for s in self.sockets: sockets.append(socket.socket(s.FAMILY, socket.SOCK_STREAM, - fileno=s.sock.fileno())) + fileno=s.sock.fileno())) self.sockets = sockets def notify(self): diff --git a/gunicorn/workers/gtornado.py b/gunicorn/workers/gtornado.py index 9dd3d7bc..2383c0d3 100644 --- a/gunicorn/workers/gtornado.py +++ b/gunicorn/workers/gtornado.py @@ -108,7 +108,7 @@ class TornadoWorker(Worker): if tornado.version_info[0] < 6: if not isinstance(app, tornado.web.Application) or \ - isinstance(app, tornado.wsgi.WSGIApplication): + isinstance(app, tornado.wsgi.WSGIApplication): app = WSGIContainer(app) elif not isinstance(app, WSGIContainer): app = WSGIContainer(app)