diff --git a/gunicorn/httpresponse.py b/gunicorn/httpresponse.py new file mode 100644 index 00000000..a7f57814 --- /dev/null +++ b/gunicorn/httpresponse.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2008,2009 Benoit Chesneau +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at# +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +class HTTPResponse(object): + + def __init__(self, req, data): + self.req = req + self.data = data + self.headers = self.req.response_headers or {} + self.fp = req.fp + + def write(self, data): + self.fp.write(data) + + def send(self): + if not self.data: return + for chunk in self.data: + self.write(chunk) + + + \ No newline at end of file diff --git a/gunicorn/socketserver.py b/gunicorn/socketserver.py new file mode 100644 index 00000000..cea4963b --- /dev/null +++ b/gunicorn/socketserver.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2008,2009 Benoit Chesneau +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at# +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import socket + +class Socket(socket.socket): + def accept_nonblock(self): + sock, addr = self.accept() + sock.setblocking(0) + return (sock, addr) + + +class TCPServer(Socket): + """class for server-side TCP sockets. + This is wrapper around socket.socket class""" + + def __init__(self, address, **opts): + self.address = address + self.backlog = opts.get('backlog', 1024) + self.timeout = opts.get('timeout', 300) + self.reuseaddr = opts.get('reuseaddr', True) + self.nodelay = opts.get('nodelay', True) + self.recbuf = opts.get('recbuf', 8192) + + socket.socket.__init__(self, socket.AF_INET, socket.SOCK_STREAM) + + if self.reuseaddr: + self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + if self.nodelay: + self.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + + if self.recbuf: + self.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, + self.recbuf) + + self.settimeout(self.timeout) + self.bind(address) + self.listen() + + def listen(self): + super(TCPServer, self).listen(self.backlog) \ No newline at end of file