В настоящее время я использую Apache Thrift для проекта API.
Я начал с небольшого тестового проекта, чтобы понять, как работает Thrift.Итак, я хотел бы использовать Python-сервер с клиентом javascript на основе браузера.
Я полностью прочитал документацию, но у меня все еще есть проблема, которую я не могу решить :(:
Выходная информация сервера:
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 -
test.thrift
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()
}
server.py
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()
index.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>