代码之家  ›  专栏  ›  技术社区  ›  graille

Apache-Thrift:带Javascript客户端的Python服务器

  •  2
  • graille  · 技术社区  · 6 年前

    为了了解节俭是如何运作的,我从一个小测试项目开始。所以,我想使用一个带有基于浏览器的javascript客户端的python服务器。

    Error with google chrome inspector

    服务器输出:

    127.0.0.1 - - [04/Dec/2018 17:04:34] code 501, message Unsupported method ('OPTIONS')
    127.0.0.1 - - [04/Dec/2018 17:04:34] "OPTIONS / HTTP/1.1" 501 -
    

    节俭

    enum Modulation
    {
      BPSK
      QPSK
      APSK16
      APSK32
    }
    
    struct Coordinate {
      1: i32 X,
      2: i32 Y
    }
    
    struct Constellation {
      1: string name,
      2: i32 timestamp,
      3: list<Coordinate> coordinates
    }
    
    service Dealer
    {
      Constellation getConstellation()
    
      Modulation getModulation()
      void setModulation(1: Modulation new_modulation)
      i32 getTimestamp()
    }
    

    import glob
    import sys
    sys.path.append('gen-py')
    
    from TApp import Dealer
    from TApp.ttypes import *
    
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import *
    from thrift.server import TServer, THttpServer
    
    import time
    
    class DealerHandler:
        def __init__(self):
            self.modulation = Modulation.BPSK
    
        def getConstellation(self):
            c = Constellation()
            c.name = "Test"
            c.timestamp = self.getTimestamp()
            c.coordinates = []
    
            return c
    
        def getModulation(self):
            return self.modulation
    
        def setModulation(self, new_modulation):
            self.modulation = new_modulation
    
        def getTimestamp(self):
            return int(str(time.time()).split('.')[0])
    
    
    if __name__ == '__main__':
        handler = DealerHandler()
        processor = Dealer.Processor(handler)
    
        port = 9090
        host = '127.0.0.1'
    
        # HTTP SERVER
        tfactory = TTransport.TBufferedTransportFactory()
        pfactory = TJSONProtocol.TJSONProtocolFactory()
        # List of all protocols : https://thrift-tutorial.readthedocs.io/en/latest/thrift-stack.html
    
        #server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
        server = THttpServer.THttpServer(processor, (host, port), pfactory)
        server.serve()
    

    索引.html

    <!DOCTYPE html>
    <html>
    <head>
    <title>Test</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    
    </head>
    
    <body>
      <div id="timestamp" style="margin:auto; width:70%; padding:15px; text-align:center; border:1px solid black;">
        No Value
      </div>
      <br />
      <button type="button" name="button" class="btn btn-info" style="width:100%" onclick="update_timestamp()">Start connection</button>
    </body>
    
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"
                  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
                  crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" charset="utf-8"></script>
    
    
    <script src="thrift.js" charset="utf-8"></script>
    <script src="test_types.js" charset="utf-8"></script>
    <script src="Dealer.js" charset="utf-8"></script>
    
    <script type="text/javascript">
      function update_timestamp() {
        var transport = new Thrift.TXHRTransport("http://localhost:9090");
        var protocol  = new Thrift.TJSONProtocol(transport);
    
        var client = new DealerClient(protocol);
        var val = client.getTimestamp();
    
        document.getElementById("timestamp").innerHTML = val;
    }
    </script>
    
    </html>
    
    0 回复  |  直到 6 年前