21 Stuff to help add an http control and query interface to a program. 41 from .
import servicediscovery
54 def __init__(self, host, port, bottle_app, verbose = False):
61 self.
httpd = bottle.WSGIRefServer(host = self.
host, port = self.
port)
66 print >>sys.stderr,
"waiting for http server to start ..." 67 while self.
httpd.port == 0:
72 print >>sys.stderr,
"started http server on http://%s:%d" % (self.
httpd.host, self.
httpd.port)
75 def __exit__(self, exc_type, exc_value, traceback):
77 print >>sys.stderr,
"stopping http server on http://%s:%d ..." % (self.
httpd.host, self.
httpd.port),
80 except Exception
as e:
81 result =
"failed: %s" % str(e)
85 print >>sys.stderr, result
86 print >>sys.stderr,
"killing http server thread ...",
90 print >>sys.stderr,
"timeout" if self.
httpd_thread.is_alive()
else "done" 95 Utility to start, advertise, track and shutdown http servers on all 96 interfaces. De-advertise and shutdown the servers by deleting this 97 object. Do not allow the object to be garbage collected until you 98 wish the servers to be shutdown. 102 >>> # save return value in a variable to prevent garbage collection 103 >>> servers = HTTPServers(port = 12345) 107 >>> # shutdown servers by deleting object 110 If port = 0 (the default) a port will be assigned randomly. 111 bottle_app should be a Bottle instance. If bottle_app is None (the 112 default) then the current default Bottle application is used. 114 def __init__(self, port = 0, bottle_app = None, service_name = "www", service_domain = None, service_properties = None, verbose = False):
115 if bottle_app
is None:
116 bottle_app = bottle.default_app()
119 for (ignored, ignored, ignored, ignored, (host, port))
in socket.getaddrinfo(
None, port, socket.AF_INET, socket.SOCK_STREAM, 0, socket.AI_NUMERICHOST | socket.AI_PASSIVE):
120 httpd =
HTTPDServer(host, port, bottle_app, verbose = verbose).__enter__()
122 print >>sys.stderr,
"advertising http server \"%s\" on http://%s:%d ..." % (service_name, httpd.host, httpd.port),
124 sname = service_name,
125 sdomain = service_domain,
127 properties = service_properties,
131 print >>sys.stderr,
"done (%s)" %
".".join((service.sname, service.sdomain))
132 self.append((httpd, service))
134 raise ValueError(
"unable to start servers%s" % (
" on port %d" % port
if port != 0
else ""))
139 print >>sys.stderr,
"de-advertising http server(s) ...",
142 except Exception
as e:
144 print >>sys.stderr,
"failed: %s" % str(e)
147 print >>sys.stderr,
"done" 150 self.pop()[0].__exit__(
None,
None,
None)
151 except Exception
as e:
153 print >>sys.stderr,
"failed: %s" % str(e)