I have recently implemented a system where clients connect to an RPC server (RPyC in my case), run a webserver on the RPC server, and close the webserver when they're done with it.
To do this I wrote a ServerThread class which wraps a SimpleHTTPServer, runs as a thread, and can be signalled to stop. The ServerThread class doesn't use the server_forever() method (which is just "while 1: self.handle_request()"), instead it has a while loop which checks a flag which is signalled from outside. We need to set a timeout for the handle_request() call. The first thing I tried was to use Python's socket object's new 'set_timeout' method, but I found it caused mysterious errors to occur on my WindowsXP machine :( Instead I call select() on the server's listening socket to check for incoming requests, and only then call handle_request(). select()'s timeout works :) # This is the heart of the code - the request handling loop: while not self.close_flag.isSet(): # Use select() to check if there is an incoming request r,w,e = select.select([self.server.socket], [], [], self.timeout) if r: self.server.handle_request() # The stop method should be called to stop the request handling loop def stop(self, wait=False): self.close_flag.set() if wait: while self.isAlive(): # isAlive() is inherited from threading.Thread time.sleep(self.timeout/10.0) (in my case, self.stop_flag is a threading.Event object) Good luck! jbrewer wrote: > I'm currently implementing an XML-RPC service in Python where binary > data is sent to the server via URLs. However, some clients that need > to access the server may not have access to a web server, and I need to > find a solution. I came up with the idea of embedding a simple HTTP > server in the XML-RPC clients so that files can be sent in the > following way: > > 1. Start an HTTP server on the client using e.g SImpleHTTPServer on a > user allowed port > 2. Call XML-RPC server with the URL to the file on the client to > upload > 3. Get XML-RPC server response, then shut down HTTP server on client > > Does this sound reasonable? I know that XML-RPC can send binary data, > but I presume that the URL method would be faster because the XML > parser could skip reading the binary file (is it base64 encoded as a > string like in SOAP?). > > Anyway, I'm unsure of how to implement this in Python. In particular, > how do I shut down a web server once I've started it with > server_forever()? Should I run the server in a separate thread or run > it asynchronously? Since I'm only uploading one file I don't really > need to handle multiple clients; I just need to be able to shut the > server down once remote call has finished. > > Any help would be appreciated since I'm new to network programming. > > Jeremy -- http://mail.python.org/mailman/listinfo/python-list