Esempio n. 1
0
async def handle(sock, data, remote):
    expression = str(data, encoding='UTF-8')
    try:
        result = process(expression)
        print('Resolved', expression.rstrip(), 'to', result)
        sock.sendto(bytes('= ' + str(result) + '\n', encoding='UTF-8'), remote)
    except Exception as error:
        result = str(error)
        sock.sendto(bytes('ERROR: ' + str(error) + '\n', encoding='UTF-8'),
                    remote)
    def handle(self):
        self.request.send(b'Please input a RPN expression\n> ')
        print('New connection, serving from {}'.format(
            threading.current_thread().native_id))

        while True:
            expression = str(self.request.recv(1024), 'UTF-8').strip()
            if expression == '': break
            try:
                result = process(expression)
                print('Resolved', expression.rstrip(), 'to', result)
                result = '= ' + str(result) + '\n> '
            except Exception as error:
                result = 'ERROR: ' + str(error) + '\n> '

            self.request.send(bytes(result, 'UTF-8'))

        self.request.send(b'See you later alligator\n')
import socket
from calculator import process

ADDRESS = ('', 8080)

with socket.create_server(ADDRESS) as server:
    server.listen(1)
    (connection, remote) = server.accept()
    with connection:
        connection.send(b'Please input a RPN expression\n> ')
        while True:
            data = connection.recv(1024)
            if not data: break
            expression = str(data, encoding='UTF-8')
            if expression == '': break
            try:
                result = process(expression)
                print('Resolved', expression.rstrip(), 'to', result)
                connection.send(
                    bytes('= ' + str(result) + '\n> ', encoding='UTF-8'))
            except Exception as error:
                result = str(error)
                connection.send(
                    bytes('ERROR: ' + str(error) + '\n> ', encoding='UTF-8'))

        connection.send(b'See you later alligator\n')
 def test_times_minus(self):
     self.assertEqual(process('2 3 * 4 -'), 2)
 def test_plus_minus(self):
     self.assertEqual(process('3 4 - 5 +'), 4)
 def test_only_operators(self):
     with self.assertRaises(Exception):
         process('+ - +')
 def test_only_operands(self):
     with self.assertRaises(Exception):
         process('2 3 3')