本文整理匯總了Python中gunicorn.http.wsgi.base_environ方法的典型用法代碼示例。如果您正苦於以下問題:Python wsgi.base_environ方法的具體用法?Python wsgi.base_environ怎麽用?Python wsgi.base_environ使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gunicorn.http.wsgi
的用法示例。
在下文中一共展示了wsgi.base_environ方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: environ_from_request
# 需要導入模塊: from gunicorn.http import wsgi [as 別名]
# 或者: from gunicorn.http.wsgi import base_environ [as 別名]
def environ_from_request(cfg, req, sockname, body):
target = req.target
fragment = None
query = None
target = urllib.parse.unquote_to_bytes(target)
if b'#' in target:
target, fragment = target.split(b'#', 1)
if b'?' in target:
print(repr(target))
target, query = target.split(b'?', 1)
environ = base_environ(cfg)
environ.update({
'PATH_INFO': target.decode('utf8'),
'QUERY_STRING': query.decode('utf8') if query else '',
'REQUEST_METHOD': req.method.decode('ascii'),
'SCRIPT_NAME': os.environ.get('SCRIPT_NAME', ''),
'SERVER_NAME': sockname[0],
'SERVER_PORT': str(sockname[1]),
'SERVER_PROTOCOL': 'HTTP/%s' % req.http_version.decode('ascii'),
'wsgi.input': io.BytesIO(body),
'wsgi.url_scheme': 'https' if cfg.is_ssl else 'http',
})
for k, v in req.headers:
print(repr(k), repr(v))
if k == b'host':
environ['HOST'] = v.decode('ascii')
key = 'HTTP_' + k.decode('ascii').upper().replace('-', '_')
if key in environ:
v = "%s,%s" % (environ[key], v.decode('ascii'))
environ[key] = v.decode('ascii')
return environ
示例2: run
# 需要導入模塊: from gunicorn.http import wsgi [as 別名]
# 或者: from gunicorn.http.wsgi import base_environ [as 別名]
def run(self):
servers = []
ssl_args = {}
if self.cfg.is_ssl:
ssl_args = dict(server_side=True, **self.cfg.ssl_options)
for s in self.sockets:
s.setblocking(1)
pool = Pool(self.worker_connections)
if self.server_class is not None:
environ = base_environ(self.cfg)
environ.update({
"wsgi.multithread": True,
"SERVER_SOFTWARE": VERSION,
})
server = self.server_class(
s, application=self.wsgi, spawn=pool, log=self.log,
handler_class=self.wsgi_handler, environ=environ,
**ssl_args)
else:
hfun = partial(self.handle, s)
server = StreamServer(s, handle=hfun, spawn=pool, **ssl_args)
server.start()
servers.append(server)
while self.alive:
self.notify()
gevent.sleep(1.0)
try:
# Stop accepting requests
for server in servers:
if hasattr(server, 'close'): # gevent 1.0
server.close()
if hasattr(server, 'kill'): # gevent < 1.0
server.kill()
# Handle current requests until graceful_timeout
ts = time.time()
while time.time() - ts <= self.cfg.graceful_timeout:
accepting = 0
for server in servers:
if server.pool.free_count() != server.pool.size:
accepting += 1
# if no server is accepting a connection, we can exit
if not accepting:
return
self.notify()
gevent.sleep(1.0)
# Force kill all active the handlers
self.log.warning("Worker graceful timeout (pid:%s)" % self.pid)
[server.stop(timeout=1) for server in servers]
except:
pass