<commit_before>"""functions to build the server side of echo."""


from __future__ import unicode_literals
from sys import version_info
import socket


def server():
    """Set up the server side to listen."""
    server = socket.socket(
        socket.AF_INET,
        socket.SOCK_STREAM,
        socket.IPPROTO_TCP
    )
    address = ('127.0.0.1', 5017)
    server.bind(address)

    server.listen(1)
    print("Listening...")

    conn, addr = server.accept()

    while True:
        try:
            buffer_length = 8
            client_request = u''
            while client_request[-2:] != u"\r\n":
                part = conn.recv(buffer_length)
                client_request += part.decode('utf8')

            print(client_request)
            conn.sendall(response_ok().encode('utf8'))
            conn.sendall(client_request.encode('utf8'))
            conn, addr = server.accept()

        except KeyboardInterrupt:
            print('\nClosing echo server...')
            break

        # except StandardError:
        #     conn.sendall(server_bad_connection().encode('utf8'))
        #     break
    conn.close()
    server.close()


def response_ok():
    """Send a HTTP response to client."""
    message = 'HTTP/1.1 200 OK\n'
    if version_info[0] == 2:
        message = message.decode('utf-8')
    message += u"\r\n"
    return message


def response_error():
    """Send a HTTP response to client if bad request."""
    message = 'HTTP/1.1 500 Internal Server Error\n'
    if version_info[0] == 2:
        message = message.decode('utf-8')
    message += u"\r\n"
    return message


def parse_request(header):
    """Check header from client."""
    header = header.split(' ')


if __name__ == '__main__':
    server()
<commit_msg>refactored the response error code to take an exception input<commit_after>"""functions to build the server side of echo."""

from __future__ import unicode_literals
from sys import version_info
import socket

PORT_NUMBER = 5017


def server():
    """Set up the server side to listen."""
    server = socket.socket(
        socket.AF_INET,
        socket.SOCK_STREAM,
        socket.IPPROTO_TCP
    )
    address = ('127.0.0.1', PORT_NUMBER)
    server.bind(address)

    server.listen(1)
    print("Listening...")

    conn, addr = server.accept()

    while True:
        try:
            buffer_length = 8
            client_request = u''
            while client_request[-2:] != u"\r\n":
                part = conn.recv(buffer_length)
                client_request += part.decode('utf8')

            print(client_request)
            conn.sendall(response_ok().encode('utf8'))
            conn.sendall(client_request.encode('utf8'))
            conn, addr = server.accept()

        except KeyboardInterrupt:
            print('\nClosing echo server...')
            break

        # except StandardError:
        #     conn.sendall(server_bad_connection().encode('utf8'))
        #     break
    conn.close()
    server.close()


def response_ok():
    """Send a HTTP response to client."""
    message = 'HTTP/1.1 200 OK\n'
    if version_info[0] == 2:
        message = message.decode('utf-8')
    message += u"\r\n"
    return message


def response_error(exception):
    """Send a HTTP response to client if bad request."""
    error_codes = {
        '405': 'Method Not Allowed\n',
        '400': 'Bad Request\n',
        '505': 'HTTP Version Not Supported\n',
        '500': 'Internal Server Error\n'
    }
    if exception in error_codes:
        message = exception + ' ' + error_codes[exception]
        if version_info[0] == 2:
            message = message.decode('utf-8')
    message += u"\r\n"
    return message


def parse_request(header):
    """Check header from client."""
    header = header.split(' ', 4)

    try:
        method, uri, protocol, host = header

    except ValueError:
        raise SyntaxError
    if method is not 'GET':
        raise NameError
    elif protocol[:9] is not 'HTTP/1.1':
        raise ValueError
    elif '127.0.0.1' not in host and str(PORT_NUMBER) not in host:
        raise TypeError
    return uri


if __name__ == '__main__':
    server()

{'fraction_non_alphanumeric': 0.12418928657218352, 'fraction_numerical': 0.01993754503963488, 'mean_word_length': 2.1932515337423313, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3339185', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1208, 'n_words': 332}