当前位置: 首页>>代码示例>>Python>>正文


Python SpooledTemporaryFile.readline方法代码示例

本文整理汇总了Python中tempfile.SpooledTemporaryFile.readline方法的典型用法代码示例。如果您正苦于以下问题:Python SpooledTemporaryFile.readline方法的具体用法?Python SpooledTemporaryFile.readline怎么用?Python SpooledTemporaryFile.readline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tempfile.SpooledTemporaryFile的用法示例。


在下文中一共展示了SpooledTemporaryFile.readline方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: InputStream

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import readline [as 别名]
class InputStream(object):
    """
    FCGI_STDIN or FCGI_DATA stream.
    Uses temporary file to store received data once max_mem bytes
    have been received.
    """
    def __init__(self, max_mem=1024):
        self._file = SpooledTemporaryFile(max_mem)
        self._eof_received = Event()

    def feed(self, data):
        if self._eof_received.is_set():
            raise IOError('Feeding file beyond EOF mark')
        if not data:  # EOF mark
            self._file.seek(0)
            self._eof_received.set()
        else:
            self._file.write(data)

    def __iter__(self):
        self._eof_received.wait()
        return iter(self._file)

    def read(self, size=-1):
        self._eof_received.wait()
        return self._file.read(size)

    def readline(self, size=-1):
        self._eof_received.wait()
        return self._file.readline(size)

    def readlines(self, sizehint=0):
        self._eof_received.wait()
        return self._file.readlines(sizehint)

    @property
    def eof_received(self):
        return self._eof_received.is_set()
开发者ID:florian-steffen,项目名称:gevent-fastcgi,代码行数:40,代码来源:base.py

示例2: Request

# 需要导入模块: from tempfile import SpooledTemporaryFile [as 别名]
# 或者: from tempfile.SpooledTemporaryFile import readline [as 别名]
class Request(object):
    def __init__(self, ip='', sock=None):
        self.ip = ip
        self.method = '-'
        self.path = '-'
        self.url = '-'
        self.protocol = '-'
        self.body = ''

        self._sock = sock
        self._input = SpooledTemporaryFile(max_size=1024 * 1024 * 2)
        self._output = Queue.Queue()
        self._bytes = 0
        self._content_length = 0
        self._header_data = ''

        self.headers = ImmutableMultiDict()
        self.cookies = []
        self.files = []
        self.get = ImmutableMultiDict()
        self.post = ImmutableMultiDict()

    def _parse(self):
        if self._input:
            self._input.seek(0)
            self.method, self.path, self.protocol = self._input.readline().split(' ')
            self.url, separator, querystring = self.path.partition('?')

            # Parse headers and cookies
            self.headers, self.cookies = self._parse_headers_and_cookies()

            # Parse POST and FILES
            parser = MultiPartParser(self)
            self.post, self.files = parser._parse_post_and_files()

            # Parse GET
            self.get = self._parse_request_data(querystring)

    def _has_more_data(self):
        return self._message_bytes < self._content_length

    @property
    def _message_bytes(self):
        return self._bytes - self._get_message_start()

    def _get_message_start(self):
        try:
            return self._header_data.index('\r\n\r\n') + 4
        except:
            return 0

    def _write(self, data):
        self._bytes += len(data)
        self._input.write(data)

        # Only accummulate up to 16kb of possible header data
        if len(self._header_data) < 2 ** 14:
            self._header_data += data
            match = re.search('content\-length: ([0-9]+)', self._header_data.lower())
            if match:
                # TODO: does not apply for HEAD requests
                self._content_length = int(match.group(1))

    def _parse_headers_and_cookies(self):
        self._input.seek(0)
        headers = []
        cookies = []
        # Skip first line
        self._input.readline()
        while True:
            header = self._input.readline().strip()
            if not header:
                # Newline, which means end of HTTP headers.
                break
            field = header.split(': ')[0]
            try:
                value = header[header.index(': ') + 2:]
            except:
                continue
            fl = field.lower()
            if fl == 'x-real-ip' or fl == 'x-forwarded-for':
                self.ip = value.split(',')[0].strip()
            elif fl == 'cookie':
                for cpair in value.split(';'):
                    try:
                        cname, cvalue = cpair.strip().split('=')
                    except ValueError:
                        pass
                    else:
                        cookies.append(Cookie(name=cname, value=cvalue))
            else:
                headers.append((field, value))
        return (ImmutableMultiDict(headers), cookies)

    def _parse_request_data(self, data):
        values = []
        for pair in data.split('&'):
            try:
                k, v = pair.split('=')
            except ValueError:
#.........这里部分代码省略.........
开发者ID:bwind,项目名称:propeller,代码行数:103,代码来源:request.py


注:本文中的tempfile.SpooledTemporaryFile.readline方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。