[ Pobierz całość w formacie PDF ]
automatically, shutting down the bus in preparation for process exit.
Drop privileges Use this Engine Plugin to start your CherryPy site as root (for example, to listen on a privileged
port like 80) and then reduce privileges to something more restricted.
This priority of this plugin s start listener is slightly higher than the priority forserver.startin order to facilitate
the most common use: starting on a low port (which requires root) and then dropping to another user.
Example:
DropPrivileges(cherrypy.engine, uid=1000, gid=1000).subscribe()
7.14. cherrypy.process 157
CherryPy Documentation, Release 3.2.4
Classes
classcherrypy.process.plugins.DropPrivileges(bus, umask=None, uid=None, gid=None)
Bases:cherrypy.process.plugins.SimplePlugin
Drop privileges. uid/gid arguments not available on Windows.
Special thanks to Gavin Baker
gid
The gid under which to run. Availability: Unix.
uid
The uid under which to run. Availability: Unix.
umask
The default permission mode for newly created files and directories.
Usually expressed in octal format, for example,0644. Availability: Unix, Windows.
Run as a daemon CherryPy allows you to easily decouple the current process from the parent environment, using
the traditional double-fork:
from cherrypy.process.plugins import Daemonizer
d = Daemonizer(cherrypy.engine)
d.subscribe()
Note: This Engine Plugin is only available on Unix and similar systems which provide fork().
If a startup error occurs in the forked children, the return code from the parent process will still be 0. Errors in the
initial daemonizing process still return proper exit codes, but errors after the fork won t. Therefore, if you use this
plugin to daemonize, don t use the return code as an accurate indicator of whether the process fully started. In fact,
that return code only indicates if the process successfully finished the first fork.
The plugin takes optional arguments to redirect standard streams:stdin,stdout, andstderr. By default, these
are all redirected to/dev/null, but you re free to send them to log files or elsewhere.
Warning: You should be careful to not start any threads before this plugin runs. The plugin will warn if you do
so, because ...the effects of calling functions that require certain resources between the call to fork() and the call
to an exec function are undefined . (ref). It is for this reason that the Server plugin runs at priority 75 (it starts
worker threads), which is later than the default priority of 65 for the Daemonizer.
Classes
classcherrypy.process.plugins.Daemonizer(bus, stdin= /dev/null , stdout= /dev/null ,
stderr= /dev/null )
Bases:cherrypy.process.plugins.SimplePlugin
Daemonize the running script.
Use this with a Web Site Process Bus via:
Daemonizer(bus).subscribe()
When this component finishes, the process is completely decoupled from the parent environment. Please note
that when this component is used, the return code from the parent process will still be 0 if a startup error occurs
in the forked children. Errors in the initial daemonizing process still return proper exit codes. Therefore, if you
use this plugin to daemonize, don t use the return code as an accurate indicator of whether the process fully
started. In fact, that return code only indicates if the process succesfully finished the first fork.
158 Chapter 7. Reference Manual
CherryPy Documentation, Release 3.2.4
PID files The PIDFile Engine Plugin is pretty straightforward: it writes the process id to a file on start, and deletes
the file on exit. You must provide a pidfile argument, preferably an absolute path:
PIDFile(cherrypy.engine, /var/run/myapp.pid ).subscribe()
Classes
classcherrypy.process.plugins.PIDFile(bus, pidfile)
Bases:cherrypy.process.plugins.SimplePlugin
Maintain a PID file via a WSPBus.
7.15 cherrypy.wsgiserver
7.15.1 cherrypy.wsgiserver WSGI Server
HTTP
classcherrypy.wsgiserver.HTTPRequest(server, conn)
An HTTP Request (and response).
A single HTTP connection may consist of multiple request/response pairs.
chunked_write= False
If True, output will be encoded with the chunked transfer-coding.
This value is set automatically inside send_headers.
close_connection= False
Signals the calling Connection that the request should close. This does not imply an error! The client
and/or server may each request that the connection be closed.
conn= None
The HTTPConnection object on which this request connected.
inheaders= {}
A dict of request headers.
outheaders= []
A list of header tuples to write in the response.
parse_request()
Parse the next HTTP request start-line and message-headers.
parse_request_uri(uri)
Parse a Request-URI into (scheme, authority, path).
Note that Request-URI s must be one of:
Request-URI = "*" | absoluteURI | abs_path | authority
Therefore, a Request-URI which starts with a double forward-slash cannot be a net_path :
net_path = "//" authority [ abs_path ]
Instead, it must be interpreted as an abs_path with an empty first path segment:
7.15. cherrypy.wsgiserver 159
CherryPy Documentation, Release 3.2.4
abs_path = "/" path_segments
path_segments = segment "/" segment )
*(
segment =
*pchar *( ";" param )
param =
*pchar
read_request_headers()
Read self.rfile into self.inheaders. Return success.
ready= False
When True, the request has been parsed and is ready to begin generating the response. When False, signals
the calling Connection that the response should not be generated and the connection should close.
respond()
Call the gateway and write its iterable output.
send_headers()
Assert, process, and send the HTTP response message-headers.
You must set self.status, and self.outheaders before calling this.
server= None
The HTTPServer object which is receiving this request.
simple_response(status, msg= )
Write a simple response back to the client.
write(chunk)
Write unbuffered data to the client.
classcherrypy.wsgiserver.HTTPConnection(server, sock, makefile=
rypy.wsgiserver.wsgiserver2.CP_fileobject >)
An HTTP connection (active socket).
server: the Server object which received this connection. socket: the raw socket object (usually TCP) for this
connection. makefile: a fileobject class for reading from the socket.
RequestHandlerClass
alias ofHTTPRequest
close()
Close the socket underlying this connection.
communicate()
Read each request and respond appropriately.
classcherrypy.wsgiserver.HTTPServer(bind_addr, gateway, minthreads=10, maxthreads=-1,
server_name=None)
An HTTP server.
ConnectionClass
The class to use for handling HTTP connections.
alias ofHTTPConnection
bind(family, type, proto=0)
Create (or recreate) the actual socket object.
bind_addr
The interface on which to listen for connections.
For TCP sockets, a (host, port) tuple. Host values may be any IPv4 or IPv6 address, or any valid hostname.
The string localhost is a synonym for 127.0.0.1 (or ::1 , if your hosts file prefers IPv6). The string
160 Chapter 7. Reference Manual
CherryPy Documentation, Release 3.2.4
0.0.0.0 is a special IPv4 entry meaning any active interface (INADDR_ANY), and :: is the similar
IN6ADDR_ANY for IPv6. The empty string or None are not allowed.
For UNIX sockets, supply the filename as a string.
gateway= None
A Gateway instance.
interrupt
Set this to an Exception instance to interrupt the server.
[ Pobierz całość w formacie PDF ]