matousc
matousc

Reputation: 3977

Stop SimpleHttpSever in jupyter notebook

I am using SimpleHTTPServer in jupyter notebook like this:

import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

It works good, but how can I stop it later in next input block?

Upvotes: 3

Views: 699

Answers (2)

L Martin
L Martin

Reputation: 1190

The answer is that you can't.

You're unable to reference previous cells (or the results of) inside of Jupyter Notebook - see this open issue for more details on the discussion.

This means you can't manipulate the object once you've used the serve_forever() function.

It may however be possible to rewrite serve_forever to fit your needs. Currently it will literally serve no matter what but adding a condition that allows you to connect and issue a 'shutdown' command would circumvent the need to call up the object later. You could just connect to the socket and issue a customised header that the TCP server would pick up and respond to.

As a quick example to start you on this path:

class StoppableRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):

    def serve_forever(self):
            while not self.stopped:
                self.handle_request()

    def not_forever(self):
        # Called from another function when a custom header is detected
        self.stopped = True
        self.server_close()

Upvotes: 2

mtt2p
mtt2p

Reputation: 1906

the server are running in background you need to search the PID an kill it like:

netstat -tulpn

Netstat out:

tcp    0   0 0.0.0.0:8888  0.0.0.0:*  LISTEN      12332/python

Kill PID with pkill or kill:

kill 12332

Upvotes: 0

Related Questions