"""Manage HTTP servers with CherryPy."""importwarningsimportcherrypyfromcherrypy.libimportattributes# We import * because we want to export check_port# et al as attributes of this module.fromcherrypy.process.serversimport*classServer(ServerAdapter):"""An adapter for an HTTP server. You can set attributes (like socket_host and socket_port) on *this* object (which is probably cherrypy.server), and call quickstart. For example: cherrypy.server.socket_port = 80 cherrypy.quickstart() """socket_port=8080_socket_host='127.0.0.1'def_get_socket_host(self):returnself._socket_hostdef_set_socket_host(self,value):ifvalue=='':raiseValueError("The empty string ('') is not an allowed value. ""Use '0.0.0.0' instead to listen on all active ""interfaces (INADDR_ANY).")self._socket_host=valuesocket_host=property(_get_socket_host,_set_socket_host,doc="""The hostname or IP address on which to listen for connections. 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 '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.""")socket_file=Nonesocket_queue_size=5socket_timeout=10shutdown_timeout=5protocol_version='HTTP/1.1'reverse_dns=Falsethread_pool=10thread_pool_max=-1max_request_header_size=500*1024max_request_body_size=100*1024*1024instance=Nonessl_context=Nonessl_certificate=Nonessl_certificate_chain=Nonessl_private_key=Nonessl_module='pyopenssl'nodelay=Truewsgi_version=(1,0)def__init__(self):self.bus=cherrypy.engineself.httpserver=Noneself.interrupt=Noneself.running=Falsedefhttpserver_from_self(self,httpserver=None):"""Return a (httpserver, bind_addr) pair based on self attributes."""ifhttpserverisNone:httpserver=self.instanceifhttpserverisNone:fromcherrypyimport_cpwsgi_serverhttpserver=_cpwsgi_server.CPWSGIServer(self)ifisinstance(httpserver,basestring):# Is anyone using this? Can I add an arg?httpserver=attributes(httpserver)(self)returnhttpserver,self.bind_addrdefstart(self):"""Start the HTTP server."""ifnotself.httpserver:self.httpserver,self.bind_addr=self.httpserver_from_self()ServerAdapter.start(self)start.priority=75def_get_bind_addr(self):ifself.socket_file:returnself.socket_fileifself.socket_hostisNoneandself.socket_portisNone:returnNonereturn(self.socket_host,self.socket_port)def_set_bind_addr(self,value):ifvalueisNone:self.socket_file=Noneself.socket_host=Noneself.socket_port=Noneelifisinstance(value,basestring):self.socket_file=valueself.socket_host=Noneself.socket_port=Noneelse:try:self.socket_host,self.socket_port=valueself.socket_file=NoneexceptValueError:raiseValueError("bind_addr must be a (host, port) tuple ""(for TCP sockets) or a string (for Unix ""domain sockets), not %r"%value)bind_addr=property(_get_bind_addr,_set_bind_addr)defbase(self):"""Return the base (scheme://host[:port] or sock file) for this server."""ifself.socket_file:returnself.socket_filehost=self.socket_hostifhostin('0.0.0.0','::'):# 0.0.0.0 is INADDR_ANY and :: is IN6ADDR_ANY.# Look up the host name, which should be the# safest thing to spit out in a URL.importsockethost=socket.gethostname()port=self.socket_portifself.ssl_certificate:scheme="https"ifport!=443:host+=":%s"%portelse:scheme="http"ifport!=80:host+=":%s"%portreturn"%s://%s"%(scheme,host)