Exemplo n.º 1
0
class RpsServer(object):
    def __init__(self, externalEvent, contexts, port=8080):
        self.port = port
        self.externalEvent = externalEvent
        self.contexts = contexts

    def serve_forever(self):
        try:
            self.running = True
            self.listener = HttpListener()
            prefix = 'http://localhost:%i/' % self.port
            self.listener.Prefixes.Add(prefix)
            try:
                print 'starting listener', prefix
                self.listener.Start()
                print 'started listener'
            except HttpListenerException as ex:
                print 'HttpListenerException:', ex
                return
            waiting = False
            while self.running:
                if not waiting:
                    print 'not waiting'
                    context = self.listener.BeginGetContext(
                        AsyncCallback(self.handleRequest), self.listener)
                    print 'after BeginGetContext'
                waiting = not context.AsyncWaitHandle.WaitOne(100)
        except:
            traceback.print_exc()

    def stop(self):
        print 'stop()'
        self.running = False
        self.listener.Stop()
        self.listener.Close()

    def handleRequest(self, result):
        '''
        pass the request to the RevitEventHandler
        '''
        print "in handleRequest"
        try:
            listener = result.AsyncState
            if not listener.IsListening:
                return
            try:
                context = listener.EndGetContext(result)
            except:
                # Catch the exception when the thread has been aborted
                self.stop()
                return
            self.contexts.append(context)
            self.externalEvent.Raise()
            print 'raised external event'
        except:
            traceback.print_exc()
Exemplo n.º 2
0
    def serve(self):
        listener = HttpListener()
        prefix = 'http://*:%s/' % str(self.port)
        listener.Prefixes.Add(prefix)
        try:
            listener.Start()
        except HttpListenerException:
            raise ServerException("Error starting server")

        print "Listening on port %s" % str(self.port)

        while True:
            result = listener.BeginGetContext(
                AsyncCallback(self.handleRequest), listener)
            result.AsyncWaitHandle.WaitOne()
Exemplo n.º 3
0
    def serveforever(self):
        self.failed = False
        listener = HttpListener()
        
        prefix = 'http://%s:%s/' % (self.host, str(self.port))
        listener.Prefixes.Add(prefix)
        
        try:
            listener.Start()
        except HttpListenerException:
            self.failed = True
            return

        while True:
            result = listener.BeginGetContext(AsyncCallback(self.handleRequest), listener)
            result.AsyncWaitHandle.WaitOne()
Exemplo n.º 4
0
 def serve_forever(self):
     try:
         self.running = True
         self.listener = HttpListener()
         prefix = 'http://localhost:%i/' % self.port
         self.listener.Prefixes.Add(prefix)
         try:
             print 'starting listener', prefix
             self.listener.Start()
             print 'started listener'
         except HttpListenerException as ex:
             print 'HttpListenerException:', ex
             return
         waiting = False
         while self.running:
             if not waiting:
                 context = self.listener.BeginGetContext(
                     AsyncCallback(self.handleRequest), self.listener)
             waiting = not context.AsyncWaitHandle.WaitOne(100)
     except:
         traceback.print_exc()
Exemplo n.º 5
0
from System import Array, Byte
from System.Net import HttpListener
from System.IO import Path, FileStream, FileMode, File

root = "c:/apache-tomcat-6.0.18/webapps/docs"

bytes = Array.CreateInstance(Byte, 1024)

listener = HttpListener()
listener.Prefixes.Add("http://*:8000/")


def process(context):
    filename = context.Request.Url.AbsolutePath
    if not filename:
        filename = "index.html"
    if filename[0] == '/':
        filename = filename[1:]
    print(filename)
    filename = Path.Combine(root, filename)
    print(filename)
    if not File.Exists(filename):
        context.Response.Abort()
        return
    input = FileStream(filename, FileMode.Open)
    bytes = Array.CreateInstance(Byte, 1024 * 16)
    nbytes = input.Read(bytes, 0, bytes.Length)
    while nbytes > 0:
        context.Response.OutputStream.Write(bytes, 0, nbytes)
        nbytes = input.Read(bytes, 0, bytes.Length)
    input.Close()
Exemplo n.º 6
0
                    else:
                        task.Result.Response.StatusCode = Convert.ToInt32(
                            HttpStatusCode.Forbidden)

                except Exception, e:
                    Trace.WriteLine(e.clsException.Message)
                    Trace.WriteLine(e.clsException.StackTrace)

                finally:
                    task.Result.Response.Close()

            return None

        try:
            httpListener = HttpListener()
            httpListener.Prefixes.Add(
                String.Format(
                    "http://localhost:{0}/",
                    UInt32.Parse("B0B", NumberStyles.HexNumber).ToString(
                        CultureInfo.InvariantCulture)))  # localhost:2827
            httpListener.Start()
            httpListener.GetContextAsync().ContinueWith[List[Entry]](
                Func[Task[HttpListenerContext], List[Entry]](onDispatch),
                TaskContinuationOptions.LongRunning).ContinueWith(
                    Action[Task[List[Entry]]](onCompleted), context)

        except Exception, e:
            Trace.WriteLine(e.clsException.Message)
            Trace.WriteLine(e.clsException.StackTrace)