def create_from_zmq(zmq_message): destination, message_type, payload = zmq_message.split(b' ', 2) message_type = message_type.decode("utf8") destination = destination.decode("utf8") found = False for module in (orwell.messages.controller_pb2, orwell.messages.robot_pb2, orwell.messages.server_game_pb2, orwell.messages.server_web_pb2): if hasattr(module, message_type): pb_klass = getattr(module, message_type) found = True break if not found: raise Exception("Invalid message type: " + message_type) pb_message = pb_klass() pb_message.ParseFromString(payload) klass = getattr(sys.modules[__name__], "Capture" + message_type) capture = klass() capture._pb_message = pb_message capture.destination = destination capture.message = pb2dict(pb_message) logger = logging.getLogger(__name__) logger.debug("capture.message = " + str(capture.message)) return capture
def test_3(self): """Play with the underlying library (not really a test).""" message = pb_controller.Hello() name = "JAMBON" message.name = name dico = pb2dict(message) message = pb_controller.Input() left = 0.2 right = -0.5 weapon1 = False weapon2 = True message.move.left = left message.move.right = right message.fire.weapon1 = weapon1 message.fire.weapon2 = weapon2 dico = pb2dict(message) message2 = dict2pb(pb_controller.Input, dico) assert_equal(left, message2.move.left) assert_equal(right, message2.move.right) assert_equal(weapon1, message2.fire.weapon1) assert_equal(weapon2, message2.fire.weapon2)
def doHttpBidRequest(channel, url, body, header={}): module = __import__(channel) _url = urlparse(url) pb = module.createRequest() pbBody = pbjson.dict2pb(pb, body) binBody = pbBody.SerializeToString() httpClient = None responseBody = None try: headers = { "Content-type": "application/x-www-form-urlencoded", "Host": "iqiyi.adtree.cn" } httpClient = httplib.HTTPConnection(_url.hostname, _url.port, timeout=30) httpClient.request("POST", _url.path, binBody, headers) response = httpClient.getresponse() #print response.status #print response.reason responseBody = response.read() #print response.getheaders() finally: if httpClient: httpClient.close() response = module.createResponse()() response.ParseFromString(responseBody) return pbjson.pb2dict(response)
def __init__(self, payload, destination=None): self._message = self.PROTOBUF_CLASS() self._message.ParseFromString(payload) self.message = pb2dict(self._message) self.destination = destination