THRIFT-195. python: Add a simple THttpServer

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@739638 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David Reiss 2009-01-31 21:59:32 +00:00
parent 1fe17774eb
commit f78ec2bc66
4 changed files with 87 additions and 17 deletions

View File

@ -0,0 +1,50 @@
# Copyright (c) 2006- Facebook
# Distributed under the Thrift Software License
#
# See accompanying file LICENSE or visit the Thrift site at:
# http://developers.facebook.com/thrift/
import BaseHTTPServer
from thrift.server import TServer
from thrift.transport import TTransport
class THttpServer(TServer.TServer):
"""A simple HTTP-based Thrift server
This class is not very performant, but it is useful (for example) for
acting as a mock version of an Apache-based PHP Thrift endpoint."""
def __init__(self, processor, server_address,
inputProtocolFactory, outputProtocolFactory = None):
"""Set up protocol factories and HTTP server.
See BaseHTTPServer for server_address.
See TServer for protocol factories."""
if outputProtocolFactory is None:
outputProtocolFactory = inputProtocolFactory
TServer.TServer.__init__(self, processor, None, None, None,
inputProtocolFactory, outputProtocolFactory)
thttpserver = self
class RequestHander(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(self):
# Don't care about the request path.
self.send_response(200)
self.send_header("content-type", "application/x-thrift")
self.end_headers()
itrans = TTransport.TFileObjectTransport(self.rfile)
otrans = TTransport.TFileObjectTransport(self.wfile)
iprot = thttpserver.inputProtocolFactory.getProtocol(itrans)
oprot = thttpserver.outputProtocolFactory.getProtocol(otrans)
thttpserver.processor.process(iprot, oprot)
otrans.flush()
self.httpd = BaseHTTPServer.HTTPServer(server_address, RequestHander)
def serve(self):
self.httpd.serve_forever()

View File

@ -19,6 +19,8 @@ def runTest(server_class):
argv = [sys.executable, relfile("TestClient.py")]
if server_class in FRAMED:
argv.append('--framed')
if server_class == 'THttpServer':
argv.append('--http=/')
ret = subprocess.call(argv)
if ret != 0:
raise Exception("subprocess failed")
@ -29,5 +31,11 @@ def runTest(server_class):
# wait for shutdown
time.sleep(1)
map(runTest, ["TForkingServer", "TThreadPoolServer",
"TThreadedServer", "TSimpleServer", "TNonblockingServer"])
map(runTest, [
"TSimpleServer",
"TThreadedServer",
"TThreadPoolServer",
"TForkingServer",
"TNonblockingServer",
"THttpServer",
])

View File

@ -8,6 +8,7 @@ from ThriftTest import ThriftTest
from ThriftTest.ttypes import *
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol
import unittest
import time
@ -15,13 +16,15 @@ from optparse import OptionParser
parser = OptionParser()
parser.set_defaults(framed=False, verbose=1, host='localhost', port=9090)
parser.set_defaults(framed=False, http_path=None, verbose=1, host='localhost', port=9090)
parser.add_option("--port", type="int", dest="port",
help="connect to server at port")
parser.add_option("--host", type="string", dest="host",
help="connect to server")
parser.add_option("--framed", action="store_true", dest="framed",
help="use framed transport")
parser.add_option("--http", dest="http_path",
help="Use the HTTP transport with the specified path")
parser.add_option('-v', '--verbose', action="store_const",
dest="verbose", const=2,
help="verbose output")
@ -33,13 +36,17 @@ options, args = parser.parse_args()
class AbstractTest(unittest.TestCase):
def setUp(self):
socket = TSocket.TSocket(options.host, options.port)
# Frame or buffer depending upon args
if options.framed:
self.transport = TTransport.TFramedTransport(socket)
if options.http_path:
self.transport = THttpClient.THttpClient(
options.host, options.port, options.http_path)
else:
self.transport = TTransport.TBufferedTransport(socket)
socket = TSocket.TSocket(options.host, options.port)
# frame or buffer depending upon args
if options.framed:
self.transport = TTransport.TFramedTransport(socket)
else:
self.transport = TTransport.TBufferedTransport(socket)
self.transport.open()

View File

@ -9,7 +9,7 @@ from ThriftTest.ttypes import *
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer, TNonblockingServer
from thrift.server import TServer, TNonblockingServer, THttpServer
class TestHandler:
@ -77,15 +77,20 @@ class TestHandler:
def testTypedef(self, thing):
return thing
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
handler = TestHandler()
processor = ThriftTest.Processor(handler)
transport = TSocket.TServerSocket(9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
if sys.argv[1] == "TNonblockingServer":
server = TNonblockingServer.TNonblockingServer(processor, transport)
if sys.argv[1] == "THttpServer":
server = THttpServer.THttpServer(processor, ('', 9090), pfactory)
else:
ServerClass = getattr(TServer, sys.argv[1])
server = ServerClass(processor, transport, tfactory, pfactory)
transport = TSocket.TServerSocket(9090)
tfactory = TTransport.TBufferedTransportFactory()
if sys.argv[1] == "TNonblockingServer":
server = TNonblockingServer.TNonblockingServer(processor, transport)
else:
ServerClass = getattr(TServer, sys.argv[1])
server = ServerClass(processor, transport, tfactory, pfactory)
server.serve()