def __init__(self, sock, wsgi_app, environ):
self.socket = sock
self.wsgi_app = wsgi_app
# Copy the class environ into self.
self.environ = self.environ.copy()
self.environ.update(environ)
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
self.rfile = SSL_fileobject(sock, "rb", self.rbufsize)
self.rfile.ssl_timeout = timeout
self.wfile = SSL_fileobject(sock, "wb", -1)
self.wfile.ssl_timeout = timeout
else:
self.rfile = CP_fileobject(sock, "rb", self.rbufsize)
self.wfile = CP_fileobject(sock, "wb", -1)
# Wrap wsgi.input but not HTTPConnection.rfile itself.
# We're also not setting maxlen yet; we'll do that separately
# for headers and body for each iteration of self.communicate
# (if maxlen is 0 the wrapper doesn't check length).
self.environ["wsgi.input"] = SizeCheckWrapper(self.rfile, 0)
python类ConnectionType()的实例源码
def __init__(self, sock, wsgi_app, environ):
self.socket = sock
self.wsgi_app = wsgi_app
# Copy the class environ into self.
self.environ = self.environ.copy()
self.environ.update(environ)
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
self.rfile = SSL_fileobject(sock, "rb", self.rbufsize)
self.rfile.ssl_timeout = timeout
self.wfile = SSL_fileobject(sock, "wb", -1)
self.wfile.ssl_timeout = timeout
else:
self.rfile = CP_fileobject(sock, "rb", self.rbufsize)
self.wfile = CP_fileobject(sock, "wb", -1)
# Wrap wsgi.input but not HTTPConnection.rfile itself.
# We're also not setting maxlen yet; we'll do that separately
# for headers and body for each iteration of self.communicate
# (if maxlen is 0 the wrapper doesn't check length).
self.environ["wsgi.input"] = SizeCheckWrapper(self.rfile, 0)
def test_type(self):
"""
L{Connection} and L{ConnectionType} refer to the same type object and
can be used to create instances of that type.
"""
self.assertIdentical(Connection, ConnectionType)
ctx = Context(TLSv1_METHOD)
self.assertConsistentType(Connection, 'Connection', ctx, None)
def makefile(self, sock, mode='r', bufsize=-1):
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
f = SSL_fileobject(sock, mode, bufsize)
f.ssl_timeout = timeout
return f
else:
return wsgiserver.CP_fileobject(sock, mode, bufsize)
def makefile(self, sock, mode='r', bufsize=-1):
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
f = SSL_fileobject(sock, mode, bufsize)
f.ssl_timeout = timeout
return f
else:
return wsgiserver.CP_fileobject(sock, mode, bufsize)
def stop(self, timeout=5):
# Must shut down threads here so the code that calls
# this method can know when all threads are stopped.
for worker in self._threads:
self._queue.put(_SHUTDOWNREQUEST)
# Don't join currentThread (when stop is called inside a request).
current = threading.currentThread()
while self._threads:
worker = self._threads.pop()
if worker is not current and worker.isAlive():
try:
if timeout is None or timeout < 0:
worker.join()
else:
worker.join(timeout)
if worker.isAlive():
# We exhausted the timeout.
# Forcibly shut down the socket.
c = worker.conn
if c and not c.rfile.closed:
if SSL and isinstance(c.socket, SSL.ConnectionType):
# pyOpenSSL.socket.shutdown takes no args
c.socket.shutdown()
else:
c.socket.shutdown(socket.SHUT_RD)
worker.join()
except (AssertionError,
# Ignore repeated Ctrl-C.
# See http://www.cherrypy.org/ticket/691.
KeyboardInterrupt), exc1:
pass
def test_type(self):
"""
L{Connection} and L{ConnectionType} refer to the same type object and
can be used to create instances of that type.
"""
self.assertIdentical(Connection, ConnectionType)
ctx = Context(TLSv1_METHOD)
self.assertConsistentType(Connection, 'Connection', ctx, None)
def makefile(self, sock, mode='r', bufsize=-1):
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
f = SSL_fileobject(sock, mode, bufsize)
f.ssl_timeout = timeout
return f
else:
return wsgiserver.CP_fileobject(sock, mode, bufsize)
def makefile(self, sock, mode='r', bufsize=-1):
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
f = SSL_fileobject(sock, mode, bufsize)
f.ssl_timeout = timeout
return f
else:
return wsgiserver.CP_fileobject(sock, mode, bufsize)
def makefile(self, sock, mode='r', bufsize=-1):
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
f = SSL_fileobject(sock, mode, bufsize)
f.ssl_timeout = timeout
return f
else:
return wsgiserver.CP_fileobject(sock, mode, bufsize)
def makefile(self, sock, mode='r', bufsize=-1):
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
f = SSL_fileobject(sock, mode, bufsize)
f.ssl_timeout = timeout
return f
else:
return wsgiserver.CP_fileobject(sock, mode, bufsize)
def __init__(self, sock, addr, server):
self.socket = sock
self.addr = addr
self.server = server
# Copy the class environ into self.
self.environ = self.environ.copy()
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
self.rfile = SSL_fileobject(sock, "r", self.rbufsize)
self.rfile.ssl_timeout = timeout
self.sendall = _ssl_wrap_method(sock.sendall)
self.environ["wsgi.url_scheme"] = "https"
self.environ["HTTPS"] = "on"
sslenv = getattr(server, "ssl_environ", None)
if sslenv:
self.environ.update(sslenv)
else:
self.rfile = sock.makefile("r", self.rbufsize)
self.sendall = sock.sendall
self.environ.update({"wsgi.input": self.rfile,
"SERVER_NAME": self.server.server_name,
})
if isinstance(self.server.bind_addr, basestring):
# AF_UNIX. This isn't really allowed by WSGI, which doesn't
# address unix domain sockets. But it's better than nothing.
self.environ["SERVER_PORT"] = ""
else:
self.environ["SERVER_PORT"] = str(self.server.bind_addr[1])
# optional values
# Until we do DNS lookups, omit REMOTE_HOST
self.environ["REMOTE_ADDR"] = self.addr[0]
self.environ["REMOTE_PORT"] = str(self.addr[1])
def __init__(self, sock, addr, server):
self.socket = sock
self.addr = addr
self.server = server
# Copy the class environ into self.
self.environ = self.environ.copy()
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
self.rfile = SSL_fileobject(sock, "r", self.rbufsize)
self.rfile.ssl_timeout = timeout
self.sendall = _ssl_wrap_method(sock.sendall)
self.environ["wsgi.url_scheme"] = "https"
self.environ["HTTPS"] = "on"
sslenv = getattr(server, "ssl_environ", None)
if sslenv:
self.environ.update(sslenv)
else:
self.rfile = sock.makefile("r", self.rbufsize)
self.sendall = sock.sendall
self.environ.update({"wsgi.input": self.rfile,
"SERVER_NAME": self.server.server_name,
})
if isinstance(self.server.bind_addr, basestring):
# AF_UNIX. This isn't really allowed by WSGI, which doesn't
# address unix domain sockets. But it's better than nothing.
self.environ["SERVER_PORT"] = ""
else:
self.environ["SERVER_PORT"] = str(self.server.bind_addr[1])
# optional values
# Until we do DNS lookups, omit REMOTE_HOST
self.environ["REMOTE_ADDR"] = self.addr[0]
self.environ["REMOTE_PORT"] = str(self.addr[1])
def makefile(self, sock, mode='r', bufsize=-1):
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
f = SSL_fileobject(sock, mode, bufsize)
f.ssl_timeout = timeout
return f
else:
return wsgiserver.CP_fileobject(sock, mode, bufsize)
def stop(self, timeout=5):
# Must shut down threads here so the code that calls
# this method can know when all threads are stopped.
for worker in self._threads:
self._queue.put(_SHUTDOWNREQUEST)
# Don't join currentThread (when stop is called inside a request).
current = threading.currentThread()
while self._threads:
worker = self._threads.pop()
if worker is not current and worker.isAlive():
try:
if timeout is None or timeout < 0:
worker.join()
else:
worker.join(timeout)
if worker.isAlive():
# We exhausted the timeout.
# Forcibly shut down the socket.
c = worker.conn
if c and not c.rfile.closed:
if SSL and isinstance(c.socket, SSL.ConnectionType):
# pyOpenSSL.socket.shutdown takes no args
c.socket.shutdown()
else:
c.socket.shutdown(socket.SHUT_RD)
worker.join()
except (AssertionError,
# Ignore repeated Ctrl-C.
# See http://www.cherrypy.org/ticket/691.
KeyboardInterrupt), exc1:
pass
def test_type(self):
"""
L{Connection} and L{ConnectionType} refer to the same type object and
can be used to create instances of that type.
"""
self.assertIdentical(Connection, ConnectionType)
ctx = Context(TLSv1_METHOD)
self.assertConsistentType(Connection, 'Connection', ctx, None)
def makefile(self, sock, mode='r', bufsize=-1):
if SSL and isinstance(sock, SSL.ConnectionType):
timeout = sock.gettimeout()
f = SSL_fileobject(sock, mode, bufsize)
f.ssl_timeout = timeout
return f
else:
return wsgiserver.CP_fileobject(sock, mode, bufsize)