Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(4)  |  Call(2)  |  Derive(0)  |  Import(2)
Parse data and return (function name, argument dict, command size)
or (None, None, data) if there's not enough data for a complete command.

        def parse_command(data, response=True):
    """Parse data and return (function name, argument dict, command size)
    or (None, None, data) if there's not enough data for a complete command.
    """
    data_len = len(data)

    if data_len < 4:
        return None, None, 0

    if response:
        expected_magic = "\x00RES"
    else:
        expected_magic = "\x00REQ"

    magic = data[:4]
    if magic == expected_magic:
        if data_len >= 12:
            magic, typ, cmd_len = struct.unpack("!4sLL", data[0:12])
            if data_len < 12 + cmd_len:
                return None, None, 0
        else:
            return None, None, 0
    elif txt_command_re.match(data):
        if '\n' in data:
            cmd, data = data.split('\n', 1)
            return cmd.strip(), data, len(cmd)+1
        return None, None, 0
    else:
        raise ProtocolError("Malformed Magic")


    msg_spec = COMMANDS.get(typ, None)
    if not msg_spec:
        raise ProtocolError("Unknown message received: %d" % typ)

    nargs = len(msg_spec[1])
    data = (nargs > 0) and data[12:12 + cmd_len].split('\x00', nargs-1) or []
    if len(data) != nargs:
        raise ProtocolError("Received wrong number of arguments to %s" % msg_spec[0])

    kwargs = dict(
        ((msg_spec[1][i], data[i]) for i in range(nargs))
    )

    return msg_spec[0], kwargs, 12 + cmd_len
        


src/d/a/danga-gearman-2.0.0/dangagearman/server.py   danga-gearman(Download)
import socket
from collections import deque
from dangagearman.protocol import DEFAULT_PORT, ProtocolError, parse_command, pack_command
 
class GearmanServerClient(asyncore.dispatcher):
        while True:
            try:
                func, args, cmd_len = parse_command(self.in_buffer, response=False)
            except ProtocolError, exc:
                logging.error("[%s] ProtocolError: %s" % (self.addr, str(exc)))

src/d/a/danga-gearman-2.0.0/dangagearman/connection.py   danga-gearman(Download)
import socket, struct, select, errno, logging
from time import time
 
from dangagearman.protocol import DEFAULT_PORT, pack_command, parse_command
 
        commands = []
        while len(self.in_buffer) >= 12:
            func, args, cmd_len = parse_command(self.in_buffer)
            if not cmd_len:
                break