Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to run a python script from within a python plugin. The script runs a CGIHTTPServer in a separate thread. It works as expected when executed from the command line and i can issue requests from the browser but when run from the plugin as an import the browser never loads the issued request. Any advice?

Here is my server code:

A stoppable, threadable version of Jurgen's origin server

import CGIHTTPServer
import BaseHTTPServer
import httplib
import threading
import sys

class RequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
    cgi_directories = ['/scripts']

    def do_QUIT (self):
        """Send 200 OK response, and set server.stop to True"""
        self.server.stop = True

class StoppableHttpServer(BaseHTTPServer.HTTPServer):
    """A http server that reacts to self.stop flag"""

    def serve_forever (self):
        """Handle one request at a time until stopped."""
        self.stop = False
        while not self.stop:

class ServerInThread(threading.Thread):
    """A class designed to start a stoppable HttpServer in a separate thread."""

    def __init__(self):
        """Initialize the port to use for the server and start the thread"""
        self.port = 3030

    def run(self):
        """Method that is called when the start() method of an instance is called"""
        server = StoppableHttpServer(("", self.port), RequestHandler)

def stop_server(port):
    """send QUIT request to http server"""
    conn = httplib.HTTPConnection("localhost:%d" % port)
    conn.request("QUIT", "/")

And this is what my qgis plugin does. It has 2 button with the following methods:

from syncServer import *

# other initialization code

# Method to start the server
def startServer(self):
    """Start sync server in a seperate thread"""
    # check if server instance exists
    if not hasattr(self, 'threaded_server') or self.threaded_server is None:"Starting new server thread")
        self.threaded_server = ServerInThread()"Starting server")
        self.threaded_server.start()"Serving at port %d", self.threaded_server.port)
    else:"A server thread already exists")

# Method to stop the server
def stopServer(self):
    """Stop sync server via http request"""
    # check if server instance exists
    if hasattr(self, 'threaded_server') and self.threaded_server is not None:"Stopping server")
        stop_server(self.threaded_server.port)"Stopped server")
        self.threaded_server = None
    else:"No server thread exists")

Thanks in advance.

share|improve this question
We might need to see a little bit of the code. – Nathan W Nov 30 '12 at 13:35
What is the best way for me to share my code with you? – robert Nov 30 '12 at 13:55
You can edit you question and post it or using something like – Nathan W Nov 30 '12 at 14:00
Where are you calling startServer? What errors do you get? – Nathan W Nov 30 '12 at 14:16
startServer is called by my button. I know the call works because it writes out messages to my log file. It doesn't seem to throw errors; well if it does it doesn't complain out loud. And if I try and run something else on the same port that the qgis plugin starts the server, it says it in use. So it is doing something but not what I am expecting. – robert Nov 30 '12 at 14:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.