Ejemplo n.º 1
0
def listen_conn(sock):
    global numconns
    global conns
    st = time.time()
    # a forever loop until we interrupt it or
    # an error occurs
    while True:
        sock.listen(50)
        print "socket is listening"

        if len(conns) > 0 and (time.time() - st) > 0.01:
            conns.pop()
            st = time.time()

        if (time.time() - st) > 2 and len(conns) == numconns:
            conns = []
        # Establish connection with client.
        c, addr = sock.accept()

        print 'Got connection from', addr

        conns.append(c)

        numconns = len(conns)
        strconns = str(numconns)
        if numconns < 10:
            strconns = '0' + strconns

        flags = 'NumConns:' + strconns
        if len(conns) > numjobs:
            flags += ',SB'
            print 'Show Some Mercy on Server!!'
            print flags
            c.send(str(flags).encode())
            c.close()
        else:
            print 'Got connection from', addr
            flags += ',NB'

            resp = ProcessRequest('')
            filename = resp.handleRequest()
            c.send(str(flags).encode())

            print 'Flags sent'

            if os.path.exists(filename):
                print 'Before Reading file data'
                length = os.path.getsize(filename)
                print length
                # flags = '127.0.0.1,NumConns:' + str(len(conns))
                #c.send(str(length))  # has to be 4 bytes
                with open(filename, 'rb') as file:
                    #print 'After opening file'
                    d = file.read(4096)
                    #print 'Kya horaha hai', filename
                    while d:
                        #print 'Before sending file'
                        c.send(d)
                        #print 'Just sent file data'
                        d = file.read(4096)
Ejemplo n.º 2
0
def listen_conn(sock):
    global numconns
    global conns
    st = time.time()
    # a forever loop until we interrupt it or
    # an error occurs
    while True:
        sock.listen(50)
        print("socket is listening")

        if len(conns) > 0 and (time.time() - st) > 0.01:
            conns.pop()
            st = time.time()

        if (time.time() - st) > 2 and len(conns) == numconns:
            conns = []
        # Establish connection with client.
        c, addr = sock.accept()

        print('Got connection from', addr)

        conns.append(c)

        numconns = len(conns)
        strconns = str(numconns)
        if numconns < 10:
            strconns = '0' + strconns

        flags = 'NumConns:' + strconns
        if len(conns) > numjobs:
            flags += ',SB'
            print('Show Some Mercy on Server!!')
            print(flags)
            c.send(str(flags).encode())
            c.close()
        else:
            print('Got connection from', addr)
            flags += ',NB'

            resp = ProcessRequest('')
            filename = resp.handleRequest()
            c.send(str(flags).encode())

            if os.path.exists(filename):
                length = os.path.getsize(filename)
                print(length)
                # flags = '127.0.0.1,NumConns:' + str(len(conns))
                # c.send(str(length).encode())  # has to be 4 bytes
                with open(filename, 'rb') as file:
                    d = file.read()
                if d:
                    c.sendall(d)