Пример #1
0
 def test_SSL(self):
     s = nexpect.spawn(('localhost', 9001), withSSL=True)
     s.sendline('SSL_TEST')
     data = s.expect('encrypted')
     shouldMatch = 'This is in response to the SSL request and should be encrypted'
     self.assertEqual(shouldMatch, data)
     
     s.sendline('exit')
     s.shutdown()
Пример #2
0
 def client_handler(self, client, info):
     print 'New connection from %s.' % str(info)
     try:
         client = nexpect.spawn(client)
         while True:
             try:
                 message = client.expectnl() #FIXME and use JSON. We can decipher chat messages from PyDA messages
                 user,data = message.split(':')
                 print '%s : %s' % (user, data)
             except nexpect.TimeoutException: 
                 pass
     except Exception as e:
         client.shutdown()
         print 'Connection from %s closed.' % str(info)
Пример #3
0
 def client_handler(self, client, info):
     print 'New connection from %s.' % str(info)
     try:
         client = nexpect.spawn(client)
         while True:
             try:
                 message = client.expectnl(
                 )  #FIXME and use JSON. We can decipher chat messages from PyDA messages
                 user, data = message.split(':')
                 print '%s : %s' % (user, data)
             except nexpect.TimeoutException:
                 pass
     except Exception as e:
         client.shutdown()
         print 'Connection from %s closed.' % str(info)
Пример #4
0
    def test_createModuleWithSocket(self):
        time.sleep(1) # Make sure the server has had time to set up

        sock = socket.socket()
        sock.connect(('localhost',9000))
        s = nexpect.nexpect(sock)
        self.assertEqual(s.timeout, 30)
        s.sendline('exit')
        s.shutdown()
        
        s = nexpect.spawn(('localhost',9000))
        self.assertEqual(s.timeout, 30)
        s.sendline('exit')
        s.shutdown()
        
        self.assertRaises(TypeError, nexpect.spawn, ('123'))
Пример #5
0
def handle(sock, conninfo):
    n = nexpect.spawn(sock)

    try:
        n.sendline(
            "Welcome to the sum solver! We will send you a random length list of integers and you need to sum the list and send back the answer. The list will be formatted as START,1,2,3,END. You must send the answer back fast or else you will lose."
        )
        n.sendline()

        for i in xrange(1, 101):
            listLength = (i + 3) * 2 + random.randint(0, i)  # Calculate a length for the list
            toSolve = []  # Create the list
            for j in xrange(listLength):  # Iterate through the length and append a number between -i and i
                toSolve.append(random.randint(-i, i))
            ans = sum(x for x in toSolve)  # Calculate the answer

            n.sendline("START," + "".join(str(x) + "," for x in toSolve) + "END")  # Send the list to the client
            try:
                data = n.expect(("\n", "\r\n"), timeout=2, incl=False)[
                    0
                ]  # Wait for the answer (followed by a '\n' or a '\r\n') but we don't care which regex we match
            except nexpect.TimeoutException as e:
                n.sendline("Timed out! Play faster!!!")  # If it times out, we cut off the connection
                n.shutdown()  # and gracefully close the socket and object
                return
            if int(data) == ans:
                n.sendline("Correct! Here's another.")  # They got it correct
                continue
            else:
                n.sendline("Wrong! Got " + data + " but expected " + str(ans))  # They got it wrong! Shutdown the socket
                n.shutdown()
                return
        n.sendline(
            "You have solved all of my challenges! FLAG_0123456789"
        )  # They solved all of the challenges! Give them the flag.
        n.shutdown()
        print str(conninfo) + " solved all of the challenges"
    except:
        n.shutdown()  # Something bad happened - shutdown!
        return
Пример #6
0
    def test_expect(self):
        s = nexpect.spawn(('localhost',9000))
        s.sendline('test_single_expect')
        returned = s.expect('B.B')
        shouldMatch = 'This is only a test.\n\n BOB'
        self.assertEqual(returned, shouldMatch)
        
        s.sendline('test_multi_expect')
        returned,index = s.expect(['B.B','Steve'])
        shouldMatch = ('This is only a test.\n\n Steve',1)
        self.assertEqual(shouldMatch[0], returned)
        self.assertEqual(shouldMatch[1], index)
        
        s.sendline('Timeout')
        with self.assertRaises(nexpect.TimeoutException):
            s.expect('timeout',timeout=1)

        s.sendline('bad_regex')
        with self.assertRaises(nexpect.TimeoutException):
            s.expect('BOB',timeout=1)    # Because we are expecting BOB and it will not be coming

        s.sendline('exit')
        s.shutdown()
Пример #7
0
import nexpect

host = 'localhost'  # Define the host and port
port = 9000

n = nexpect.spawn((host, port)) # Create an nexpect object with the host, port tuple instead of a socket object
n.expect('lose.')   # Wait for the end of the intro before entering the game loop

while True:                         # We don't know how many puzzles we will need to solve so just keep solving until something different happens
    n.expect('START,', timeout=3)   # By setting a timeout, we will guarantee that we will print out any unexpected data that comes to us because it won't match out regex
    lst = n.expect(',END', incl=False).split(',')   # Get the list out of the socket
    ans = sum(int(x) for x in lst)  # Sum the list
    print 'Sending answer: ' + str(ans) # Print some fun data
    n.sendline(ans) # Send the answer and continue the loop