gunicorn/gunicorn/util.py
2010-01-17 03:30:57 +01:00

107 lines
3.3 KiB
Python

# -*- coding: utf-8 -
#
# 2009 (c) Benoit Chesneau <benoitc@e-engura.com>
# 2009 (c) Paul J. Davis <paul.joseph.davis@gmail.com>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import errno
import select
import socket
import time
timeout_default = object()
CHUNK_SIZE = (16 * 1024)
MAX_BODY = 1024 * (80 + 32)
weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
monthname = [None,
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
def read_partial(sock, length):
while True:
try:
ret = select.select([sock.fileno()], [], [], 2.0)
if ret[0]: break
except socket.error, e:
if e[0] == errno.EINTR:
break
raise
data = sock.recv(length)
return data
def write(sock, data):
for i in xrange(2):
print i
try:
return sock.send(data)
except socket.error:
if i == 2:
print "raise"
raise
def write_nonblock(sock, data):
while True:
try:
ret = select.select([], [sock.fileno()], [], 2.0)
if ret[1]: break
except socket.error, e:
if e[0] == errno.EINTR:
break
raise
sock.send(data)
def import_app(module):
parts = module.rsplit(":", 1)
if len(parts) == 1:
module, obj = module, "application"
else:
module, obj = parts[0], parts[1]
mod = __import__(module)
parts = module.split(".")
for p in parts[1:]:
mod = getattr(mod, p, None)
if mod is None:
raise ImportError("Failed to import: %s" % module)
app = getattr(mod, obj, None)
if app is None:
raise ImportError("Failed to find application object: %r" % obj)
if not callable(app):
raise TypeError("Application object must be callable.")
return app
def http_date(timestamp=None):
"""Return the current date and time formatted for a message header."""
if timestamp is None:
timestamp = time.time()
year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
weekdayname[wd],
day, monthname[month], year,
hh, mm, ss)
return s