Exemplo n.º 1
0
    def start_server():
        """
        Function responsible for running the connections and server bootstrap
        """

        # Binds a socket to a specific port
        server_socket = Connection.start_socket(hostname=Server.get_hostname(),
                                                port=Server.get_port())
        # Listens to the port waiting for clients, for each client, setups a new thread for interaction
        Connection.accept_connections(server_socket,
                                      Administration.server_functionalities)
        # Closes the socket when ending everything
        server_socket.close()
Exemplo n.º 2
0
def test_body_is_not_read_as_a_whole_because_content_length_is_shorter_for_longer_message(
):
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 58
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 60\r\n\r\ntesting a somewhat longer body that is not read in its entirety'
    connection.update()
    assert connection.request_body == b't'

    connection.update()
    assert connection.request_body == b'testing a somewhat longer body that is not read in its enti'

    connection.update()
    assert connection.request_body == b'testing a somewhat longer body that is not read in its entir'
Exemplo n.º 3
0
def test_request_received_callback_is_called_after_a_longer_body_is_read():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 58
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 60\r\n\r\ntesting a somewhat longer body that is not read in its xxxxx'
    connection.update()
    connection.update()
    connection.update()
    received_callback.assert_called_with(connection)
Exemplo n.º 4
0
def test_update_reads_longer_request_body_after_headers_received():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 56
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 105\r\n\r\nthis is a very long request body that will be read in two parts because it is longer than the buffer size'
    connection.update()
    connection.update()
    connection.update()

    assert connection.request_body == b'this is a very long request body that will be read in two parts because it is longer than the buffer size'
Exemplo n.º 5
0
def test_part_of_the_body_read_when_headers_and_rest_with_another_update_call(
):
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 57
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 4\r\n\r\ntest'
    connection.update()
    assert connection.request_body == b't'

    connection.update()
    assert connection.request_body == b'test'
Exemplo n.º 6
0
def test_connection_receives_first_4_bytes_of_longer_request():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.receive(Mock(), BUFFER_SIZE)
    fake_socket.send_buffer = b'longer test request'
    connection.update()

    assert connection.recv_buffer == b'long'
Exemplo n.º 7
0
def test_update_sends_whole_response_to_client():
    connection = Connection(ADDR, fake_socket, Mock())
    response = b'test'
    connection.send(response)
    connection.update()

    assert fake_socket.recv_buffer == response
Exemplo n.º 8
0
def test_connection_updates_raises_error_if_socket_does_not_send_anything():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.send_buffer = b''
    connection.state = Connection.SENDING_RESPONSE

    with pytest.raises(Exception, match='Socket connection broken'):
        connection.update()
Exemplo n.º 9
0
def test_update_empties_send_buffer_after_sending_whole_response():
    connection = Connection(ADDR, fake_socket, Mock())
    response = b'test'
    connection.send(response)
    connection.update()

    assert not connection.send_buffer
Exemplo n.º 10
0
def test_stores_received_request_in_recv_buffer():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.receive(Mock(), BUFFER_SIZE)
    request = fake_socket.send_buffer
    connection.update()

    assert connection.recv_buffer == request
Exemplo n.º 11
0
def test_body_is_read_only_the_amount_of_content_length():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 100
    connection.receive(Mock(), buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\nHost: www.w3.org\r\naccept: text/html\r\ncontent-length: 3\r\n\r\ntest'
    connection.update()

    assert connection.request_body == b'tes'
Exemplo n.º 12
0
def test_no_request_body_read_without_content_length_header():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 78
    connection.receive(Mock(), buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\nHost: www.w3.org\r\naccept: text/html\r\n\r\ntest'
    connection.update()

    assert connection.request_body == None
Exemplo n.º 13
0
def test_connection_is_closed_after_the_whole_response_is_sent():
    close_callback = Mock()
    connection = Connection(ADDR, fake_socket, close_callback)
    response = b'test'
    connection.send(response)
    connection.update()

    close_callback.assert_called_with(connection.socket)
Exemplo n.º 14
0
def test_connection_is_closed_if_nothing_received_from_the_client():
    close_callback = Mock()
    connection = Connection(ADDR, fake_socket, close_callback)
    connection.receive(Mock(), BUFFER_SIZE)
    fake_socket.send_buffer = b''
    connection.update()

    close_callback.assert_called_with(connection.socket)
Exemplo n.º 15
0
def test_connection_receives_longer_request_in_two_chunks():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.receive(Mock(), BUFFER_SIZE)
    fake_socket.send_buffer = b'longer test request'
    connection.update()
    connection.update()

    assert connection.recv_buffer == b'longer t'
Exemplo n.º 16
0
def test_update_called_twice_sends_the_whole_response():
    connection = Connection(ADDR, fake_socket, Mock())
    response = b'testing'
    connection.send(response)
    connection.update()
    connection.update()

    assert connection.send_buffer == b''
    assert fake_socket.recv_buffer == b'testing'
Exemplo n.º 17
0
def test_update_removes_beginning_of_response_from_send_buffer_with_longer_response(
):
    connection = Connection(ADDR, fake_socket, Mock())
    response = b'test longer response'
    connection.send(response)
    connection.update()

    assert connection.send_buffer == b' longer response'
    assert fake_socket.recv_buffer == b'test'
Exemplo n.º 18
0
def test_request_received_callback_is_called_when_no_body():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 57
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 60\r\n\r\n'
    connection.update()
    connection.update()
    received_callback.assert_called_with(connection)
Exemplo n.º 19
0
def test_request_received_callback_called_after_startline_and_headers_read_but_body_is_not(
):
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 56
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 4\r\n\r\ntest'
    connection.update()

    assert connection.state == Connection.RECEIVING_BODY
Exemplo n.º 20
0
def test_received_callback_is_called_with_connection_as_parameter_after_whole_request_is_received(
):
    connection = Connection(ADDR, fake_socket, Mock())
    received_callback = Mock()
    connection.receive(received_callback, 70)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\n\r\n'
    connection.update()
    request = Request('GET', '/path/to/example.com', 'HTTP/1.1', None, None)

    received_callback.assert_called_with(connection, request)
Exemplo n.º 21
0
def test_update_reads_request_body_after_headers_received():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 56
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 4\r\n\r\ntest'
    connection.update()
    connection.update()

    assert connection.request_body == b'test'
Exemplo n.º 22
0
def test_parsed_request_is_stored_in_the_connection():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 100
    connection.receive(Mock(), buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\nHost: www.w3.org\r\naccept: text/html\r\n\r\n'
    connection.update()

    assert connection.parsed_request.method == 'GET'
    assert connection.parsed_request.uri == '/path/to/example.com'
    assert connection.parsed_request.http_version == 'HTTP/1.1'
    assert connection.parsed_request.headers == {
        'host': 'www.w3.org',
        'accept': 'text/html'
    }
Exemplo n.º 23
0
def test_send_changes_connection_state_as_sending_response():
    connection = Connection(ADDR, fake_socket, Mock())
    response = b'test'
    connection.send(response)

    assert connection.state == Connection.SENDING_RESPONSE
Exemplo n.º 24
0
def test_connection_stores_buffer_size_when_starting_to_receive_request():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.receive(Mock(), BUFFER_SIZE)

    assert connection.buffer_size == BUFFER_SIZE
Exemplo n.º 25
0
def test_cannot_send_after_connection_is_closed():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.close()
    with pytest.raises(Exception, match='Connection closed'):
        connection.send(b'')
Exemplo n.º 26
0
def test_state_is_changed_to_closed_when_close_is_called():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.close()

    assert connection.state == Connection.CLOSED
Exemplo n.º 27
0
def test_send_stores_response_to_send_buffer():
    connection = Connection(ADDR, fake_socket, Mock())
    response = b'test'
    connection.send(response)

    assert connection.send_buffer == response
Exemplo n.º 28
0
from server.connection import Connection
from math import sqrt

con = Connection()  #database Connection


class Util:
    def __init__(self):
        pass

    def getLatLong(self, location):
        sql = 'select lat,lng from places where name = "{}"'.format(location)
        # print(sql)
        cur = con.retrive(sql)
        for row in cur:
            pass
        return row[0], row[1]

    def findClosestLocation(self, location, cluster):
        x1, y1 = self.getLatLong(
            location)  #current location lat #current location long
        min = 9999999
        closeLocation = ""
        for i in cluster:
            if i == location:
                continue
            x2, y2 = self.getLatLong(
                i
            )  # i'th locations lat from cluster # i'th locations long from cluster
            dis = self.getDistance(x1, y1, x2, y2)
            if (dis < min):
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.vq import kmeans2, whiten
from server.connection import Connection
import json

con = Connection()


class Clustering:
    def __init__(self):
        pass

    def createClusters(self, locations):

        loca = list()

        for l in locations:
            sql = 'select Lat,lng from places where name = "{}"'.format(l)
            # print(sql)
            cur = con.retrive(sql)
            # print(cur)
            # print(l)
            loca.append(cur[0])

        # print(loca)

        coordinates = np.array(loca)
        # print(coordinates)

        x, self.y = kmeans2(whiten(coordinates), 2, iter=50)
Exemplo n.º 30
0
def test_connection_changes_state_to_receiving_request_when_receive_called():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.receive(Mock(), BUFFER_SIZE)

    assert connection.state == Connection.RECEIVING_REQUEST