예제 #1
0
파일: worker.py 프로젝트: allanderek/pyPEPA
    def handle(self, socket, address):
        if address[0] not in clients:
            clients.add(address[0])
        print ('New connection from %s:%s' % address)

        # Receive header LEN(4), VERSION(1)
        header_bytes = socket.recv(5)
        header = pepa_prot.extract_header(header_bytes)

        print ("Length of data %i, protocol version %i" %(header[0], header[1]))

        if not pepa_prot.check_version(header[1]):
            print("Protocol mismatch")

        data_bytes = socket.recv(header[0])
        data = pepa_prot.extract_data(data_bytes)
        ret = {}
        try:
            print("Command : %s" % data["cmd"])
            ret["data"] = doit[ data["cmd"] ](data)
            ret["rid"] = data["rid"]
        except:
            print("Command not found")
            ret["status"] = 0
        socket.sendall( pepa_prot.prepare_to_send(ret))
        socket.close()
예제 #2
0
    def handle(self, socket, address):
        if address[0] not in clients:
            clients.add(address[0])
        print('New connection from %s:%s' % address)

        # Receive header LEN(4), VERSION(1)
        header_bytes = socket.recv(5)
        header = pepa_prot.extract_header(header_bytes)

        print("Length of data %i, protocol version %i" %
              (header[0], header[1]))

        if not pepa_prot.check_version(header[1]):
            print("Protocol mismatch")

        data_bytes = socket.recv(header[0])
        data = pepa_prot.extract_data(data_bytes)
        ret = {}
        try:
            print("Command : %s" % data["cmd"])
            ret["data"] = doit[data["cmd"]](data)
            ret["rid"] = data["rid"]
        except:
            print("Command not found")
            ret["status"] = 0
        socket.sendall(pepa_prot.prepare_to_send(ret))
        socket.close()
예제 #3
0
def send_recv( address, dat):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(1)
        sock.connect(address)
    except Exception as e:
        raise
    tos = pepa_prot.prepare_to_send(dat)
    sock.send(tos)
    header_bytes = sock.recv(5)
    header = pepa_prot.extract_header(header_bytes)
    if not pepa_prot.check_version(header[1]):
        print("Protocol mismatch")
        exit(1)
    #TODO: zmienic w petelke
    data_bytes = sock.recv(header[0])
    sock.shutdown(socket.SHUT_RDWR)
    sock.close()
    return pepa_prot.extract_data(data_bytes)