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()
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'
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)
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'
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'
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'
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
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()
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
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
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'
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
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)
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)
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'
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'
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'
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)
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
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)
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'
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' }
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
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
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'')
def test_state_is_changed_to_closed_when_close_is_called(): connection = Connection(ADDR, fake_socket, Mock()) connection.close() assert connection.state == Connection.CLOSED
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
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)
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