Created
August 13, 2012 05:05
-
-
Save perchouli/3337107 to your computer and use it in GitHub Desktop.
Python avro rpc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
import avro.ipc as ipc | |
import avro.protocol as protocol | |
import avro.schema as schema | |
PROTOCOL_FILE = '/srv/http/duoshuo/configs/duofilter.avpr' | |
PROTOCOL = protocol.parse(open(PROTOCOL_FILE).read()) | |
class PostResponder(ipc.Responder): | |
def __init__(self): | |
ipc.Responder.__init__(self, PROTOCOL) | |
def invoke(self, msg, req): | |
if msg.name == 'send': | |
message = req['message'] | |
print message | |
return (message['action'] + ': post_id=' + message['post_id']) | |
else: | |
raise schema.AvroException("unexpected message:", msg.getname()) | |
class PostHandler(BaseHTTPRequestHandler): | |
def do_POST(self): | |
self.responder = PostResponder() | |
call_request_reader = ipc.FramedReader(self.rfile) | |
call_request = call_request_reader.read_framed_message() | |
resp_body = self.responder.respond(call_request) | |
print resp_body | |
self.send_response(200) | |
self.send_header('Content-Type', 'avro/binary') | |
self.end_headers() | |
resp_writer = ipc.FramedWriter(self.wfile) | |
resp_writer.write_framed_message(resp_body) | |
server_addr = ('127.0.0.1', 9090) | |
if __name__ == '__main__': | |
server = HTTPServer(server_addr, PostHandler) | |
server.allow_reuse_address = True | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment