json.load()没有从连接套接字读取数据?

时间:2017-12-25 16:37:36

标签: python-2.7 sockets

在下面的代码中我想建立一个与套接字的连接然后写一些json字符串,然后通过makefile读取它。写作部分工作正常,但在阅读部分,代码无法加载json字符串?在下面的代码中,代码卡在此行conn_data = json.load(in_file)

客户端套接字

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

            # connect to directory server - runs on port 9001
            self.dir_host = 'localhost'  # directory server address
            self.dir_port = 9001         # directory server port

            conn_str = json.dumps({'command':'register','role':role})
            print "to be sent: ", conn_str

            s.connect((self.dir_host, self.dir_port))
            s.sendall(conn_str)  # send test string
            print "sent the bloody string chars: "
    #        s.shutdown(socket.SHUT_WR)
            print "shutdown socket for writing now going into recieve part (should be a loop right)"


            in_file = s.makefile('r',0)     # open the socket as a file object, read-only, unbuffered
            print "open socket"
            conn_data = json.load(in_file)  # we expect a json string so load as dict object
            print conn_data

服务器套接字

def start(self):
        #create an INET, STREAMing socket
        serv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        serv_sock.bind((self.host, self.port))
        serv_sock.listen(5) # and listen for connections

        while (1):
            print "listening ... "
            cli_sock, addr = serv_sock.accept()
            print "recieved connection from {0}", addr 
            self.handle_connection(cli_sock)
            print "handle running"


    def handle_connection(self, in_sock):
        in_file = in_sock.makefile('r',0) # open the socket as a file object, read-only, unbuffered
        cli_data = json.load(in_file) #

        print "read the request into json object for the connection ...", cli_data

        role = int(cli_data['role'])

        if cli_data['command'] == 'register':
            role_count = self.get_role_count(role)
            proc_num = str(role) + '.' + str(role_count)
            port = self.maxPort
            # now add the stuff to the process table
            self.PaxosProcs[role].append(self.PaxosProcess(proc_num, port))
            # get the info that the requesting process needs
            conn_data = self.get_connection_data(role)
            conn_data['proc_num'] = proc_num
            conn_data['port'] = port
            conn_info = json.dumps(conn_data)

            self.maxPort += 1
            self.increment_role_count(role)

        elif cli_data['command'] == 'update':
            # if proc is not in the table tell it to register by returning an error
            proc_num = cli_data['proc_num']

            if self.is_registered(proc_num, role):
                conn_info = json.dumps(self.get_connection_data(role))
            else:
                conn_info = json.dumps({'error':'unregistered'})

        elif cli_data['command'] == 'remove':
            proc_num = cli_data['proc_num']
            if self.remove(proc_num, role):
                conn_info = json.dumps({'info':'removed'})
            else:
                conn_info = json.dumps({'error':'unregistered'})

        in_sock.sendall(conn_info)
        print "result dispatched..."
        in_file.close()
        in_sock.shutdown(socket.SHUT_RDWR)
        in_sock.close()

0 个答案:

没有答案