本文整理汇总了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