示例#1
0
文件: echo.py 项目: dhain/greennet
def echo(sock):
    with closing(sock):
        bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
        while True:
            data = greennet.recv(sock, bufsize)
            if not data:
                break
            greennet.sendall(sock, data)
示例#2
0
文件: serve.py 项目: dhain/webskewer
 def write(self, data):
     if self.headers is None:
         raise AssertionError('write() before start_response()')
     elif not self.headers_sent:
         self._write_headers()
     self.bytes_sent += len(data)
     if self.chunked:
         data = '%x\r\n' % (len(data),) + data + '\r\n'
     greennet.sendall(self.sock, data)
示例#3
0
文件: serve.py 项目: dhain/webskewer
 def _write_headers(self):
     self.headers_sent = True
     if self.environ['webskewer.http_version'] > (0,9):
         missing = set(['server', 'date', 'content-length'])
         headers = []
         for header in self.headers:
             lower = header[0].lower()
             if lower in missing:
                 missing.remove(lower)
             headers.append('%s: %s\r\n' % header)
         add_headers = []
         if 'content-length' in missing and not self.status.startswith('304'):
             if self.environ['webskewer.http_version'] > (1,0):
                 add_headers.append('Transfer-Encoding: chunked\r\n')
                 self.chunked = True
             else:
                 self.environ['HTTP_CONNECTION'] = 'close'
         if 'date' in missing:
             add_headers.append('Date: %s\r\n' % (now_1123(),))
         if 'server' in missing:
             add_headers.append('Server: webskewer/%d.%d\r\n' % __version__)
         out = ''.join(['HTTP/1.1 %s\r\n' % (self.status,)] +
                       add_headers + headers + ['\r\n'])
         greennet.sendall(self.sock, out)
示例#4
0
文件: serve.py 项目: dhain/webskewer
 def handle(self):
     resp = None
     try:
         resp = self.application(self.environ, self.start_response)
         for data in resp:
             if data:
                 self.write(data)
         if not self.headers_sent:
             self._write_headers()
     except Exception:
         print >> sys.stderr, log_exc(self.environ['REMOTE_ADDR'])
         if self.headers_sent:
             return True # signal caller to close connection
         self.application = ServerError()
         return self.handle()
     finally:
         if hasattr(resp, 'close'):
             resp.close()
     if self.chunked:
         greennet.sendall(self.sock, '0\r\n\r\n')
     if not 'webskewer.bad_request' in self.environ:
         print log_req(self.environ, self.status, self.headers, self.bytes_sent)
     for _ in self.environ['wsgi.input']:
         pass